Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(561)

Side by Side Diff: build/android/gyp/process_resources.py

Issue 2570313002: Use R.txt from AAR to generate R.java for it when building APK. (Closed)
Patch Set: Refine in-code comment Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 """Process Android resources to generate R.java, and prepare for packaging. 7 """Process Android resources to generate R.java, and prepare for packaging.
8 8
9 This will crunch images and generate v14 compatible resources 9 This will crunch images and generate v14 compatible resources
10 (see generate_v14_compatible_resources.py). 10 (see generate_v14_compatible_resources.py).
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 help='Resources from dependents.') 67 help='Resources from dependents.')
68 68
69 parser.add_option('--resource-zip-out', 69 parser.add_option('--resource-zip-out',
70 help='Path for output zipped resources.') 70 help='Path for output zipped resources.')
71 71
72 parser.add_option('--R-dir', 72 parser.add_option('--R-dir',
73 help='directory to hold generated R.java.') 73 help='directory to hold generated R.java.')
74 parser.add_option('--srcjar-out', 74 parser.add_option('--srcjar-out',
75 help='Path to srcjar to contain generated R.java.') 75 help='Path to srcjar to contain generated R.java.')
76 parser.add_option('--r-text-out', 76 parser.add_option('--r-text-out',
77 help='Path to store the R.txt file generated by appt.') 77 help='Path to store the generated R.txt file.')
78 parser.add_option(
79 '--r-text-in',
agrieve 2016/12/14 18:16:02 nit: please indent same as other add_option calls
mlopatkin 2016/12/19 14:44:42 Done.
80 help='Path to pre-existing R.txt for these resources. Resource names '
81 'from it will be used to generate R.java instead of aapt-generated '
82 'R.txt.')
78 83
79 parser.add_option('--proguard-file', 84 parser.add_option('--proguard-file',
80 help='Path to proguard.txt generated file') 85 help='Path to proguard.txt generated file')
81 86
82 parser.add_option( 87 parser.add_option(
83 '--v14-skip', 88 '--v14-skip',
84 action="store_true", 89 action="store_true",
85 help='Do not generate nor verify v14 resources') 90 help='Do not generate nor verify v14 resources')
86 91
87 parser.add_option( 92 parser.add_option(
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 for package, r_txt_file in zip(packages, r_txt_files): 167 for package, r_txt_file in zip(packages, r_txt_files):
163 if package in resources_by_package: 168 if package in resources_by_package:
164 raise Exception(('Package name "%s" appeared twice. All ' 169 raise Exception(('Package name "%s" appeared twice. All '
165 'android_resources() targets must use unique package ' 170 'android_resources() targets must use unique package '
166 'names, or no package name at all.') % package) 171 'names, or no package name at all.') % package)
167 resources_by_type = resources_by_package[package] 172 resources_by_type = resources_by_package[package]
168 # The sub-R.txt files have the wrong values at this point. Read them to 173 # The sub-R.txt files have the wrong values at this point. Read them to
169 # figure out which entries belong to them, but use the values from the 174 # figure out which entries belong to them, but use the values from the
170 # main R.txt file. 175 # main R.txt file.
171 for entry in _ParseTextSymbolsFile(r_txt_file): 176 for entry in _ParseTextSymbolsFile(r_txt_file):
172 entry = all_resources[(entry.resource_type, entry.name)] 177 entry = all_resources.get((entry.resource_type, entry.name))
173 resources_by_type[entry.resource_type].append(entry) 178 # For most cases missing entry here is an error. It means that some
179 # library claims to have or depend on a resource that isn't included into
180 # the APK. There is one notable exception: Google Play Services (GMS).
181 # GMS is shipped as a bunch of AARs. One of them - basement - contains
182 # R.txt with ids of all resources, but most of the resources are in the
183 # other AARs. However, all other AARs reference their resources via
184 # basement's R.java so the latter must contain all ids that are in its
185 # R.txt. Most targets depend on only a subset of GMS AARs so some
186 # resources are missing, which is okay because the code that references
187 # them is missing too. We can't get an id for a resource that isn't here
188 # so the only solution is to skip the resource entry entirely.
189 #
190 # We can verify that all entries referenced in the code were generated
191 # correctly by running Proguard on the APK: it will report missing
192 # fields.
193 if entry:
194 resources_by_type[entry.resource_type].append(entry)
174 195
175 for package, resources_by_type in resources_by_package.iteritems(): 196 for package, resources_by_type in resources_by_package.iteritems():
176 package_r_java_dir = os.path.join(srcjar_dir, *package.split('.')) 197 package_r_java_dir = os.path.join(srcjar_dir, *package.split('.'))
177 build_utils.MakeDirectory(package_r_java_dir) 198 build_utils.MakeDirectory(package_r_java_dir)
178 package_r_java_path = os.path.join(package_r_java_dir, 'R.java') 199 package_r_java_path = os.path.join(package_r_java_dir, 'R.java')
179 java_file_contents = _CreateRJavaFile( 200 java_file_contents = _CreateRJavaFile(
180 package, resources_by_type, shared_resources) 201 package, resources_by_type, shared_resources)
181 with open(package_r_java_path, 'w') as f: 202 with open(package_r_java_path, 'w') as f:
182 f.write(java_file_contents) 203 f.write(java_file_contents)
183 204
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 446
426 if options.proguard_file: 447 if options.proguard_file:
427 package_command += ['-G', options.proguard_file] 448 package_command += ['-G', options.proguard_file]
428 build_utils.CheckOutput(package_command, print_stderr=False) 449 build_utils.CheckOutput(package_command, print_stderr=False)
429 450
430 # When an empty res/ directory is passed, aapt does not write an R.txt. 451 # When an empty res/ directory is passed, aapt does not write an R.txt.
431 if not os.path.exists(r_txt_path): 452 if not os.path.exists(r_txt_path):
432 build_utils.Touch(r_txt_path) 453 build_utils.Touch(r_txt_path)
433 454
434 if not options.include_all_resources: 455 if not options.include_all_resources:
456 # --include-all-resources can only be specified for generating final R
457 # classes for APK. It makes no sense for APK to have pre-generated R.txt
458 # though, because aapt-generated already lists all available resources.
459 if options.r_text_in:
460 r_txt_path = options.r_text_in
461
435 packages = list(options.extra_res_packages) 462 packages = list(options.extra_res_packages)
436 r_txt_files = list(options.extra_r_text_files) 463 r_txt_files = list(options.extra_r_text_files)
437 464
438 cur_package = options.custom_package 465 cur_package = options.custom_package
439 if not options.custom_package: 466 if not options.custom_package:
440 cur_package = _ExtractPackageFromManifest(options.android_manifest) 467 cur_package = _ExtractPackageFromManifest(options.android_manifest)
441 468
442 # Don't create a .java file for the current resource target when: 469 # Don't create a .java file for the current resource target when:
443 # - no package name was provided (either by manifest or build rules), 470 # - no package name was provided (either by manifest or build rules),
444 # - there was already a dependent android_resources() with the same 471 # - there was already a dependent android_resources() with the same
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 options, 559 options,
533 input_paths=input_paths, 560 input_paths=input_paths,
534 input_strings=input_strings, 561 input_strings=input_strings,
535 output_paths=output_paths, 562 output_paths=output_paths,
536 # TODO(agrieve): Remove R_dir when it's no longer used (used only by GYP). 563 # TODO(agrieve): Remove R_dir when it's no longer used (used only by GYP).
537 force=options.R_dir) 564 force=options.R_dir)
538 565
539 566
540 if __name__ == '__main__': 567 if __name__ == '__main__':
541 main(sys.argv[1:]) 568 main(sys.argv[1:])
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698