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

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: Added workaround for aars with empty R.txt and no resources. 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
« no previous file with comments | « build/android/gyp/aar.py ('k') | build/config/android/internal_rules.gni » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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('--r-text-in',
79 help='Path to pre-existing R.txt for these resources. '
80 'Resource names from it will be used to generate R.java '
81 'instead of aapt-generated R.txt.')
78 82
79 parser.add_option('--proguard-file', 83 parser.add_option('--proguard-file',
80 help='Path to proguard.txt generated file') 84 help='Path to proguard.txt generated file')
81 85
82 parser.add_option( 86 parser.add_option(
83 '--v14-skip', 87 '--v14-skip',
84 action="store_true", 88 action="store_true",
85 help='Do not generate nor verify v14 resources') 89 help='Do not generate nor verify v14 resources')
86 90
87 parser.add_option( 91 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): 166 for package, r_txt_file in zip(packages, r_txt_files):
163 if package in resources_by_package: 167 if package in resources_by_package:
164 raise Exception(('Package name "%s" appeared twice. All ' 168 raise Exception(('Package name "%s" appeared twice. All '
165 'android_resources() targets must use unique package ' 169 'android_resources() targets must use unique package '
166 'names, or no package name at all.') % package) 170 'names, or no package name at all.') % package)
167 resources_by_type = resources_by_package[package] 171 resources_by_type = resources_by_package[package]
168 # The sub-R.txt files have the wrong values at this point. Read them to 172 # 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 173 # figure out which entries belong to them, but use the values from the
170 # main R.txt file. 174 # main R.txt file.
171 for entry in _ParseTextSymbolsFile(r_txt_file): 175 for entry in _ParseTextSymbolsFile(r_txt_file):
172 entry = all_resources[(entry.resource_type, entry.name)] 176 entry = all_resources.get((entry.resource_type, entry.name))
173 resources_by_type[entry.resource_type].append(entry) 177 # For most cases missing entry here is an error. It means that some
178 # library claims to have or depend on a resource that isn't included into
179 # the APK. There is one notable exception: Google Play Services (GMS).
180 # GMS is shipped as a bunch of AARs. One of them - basement - contains
181 # R.txt with ids of all resources, but most of the resources are in the
182 # other AARs. However, all other AARs reference their resources via
183 # basement's R.java so the latter must contain all ids that are in its
184 # R.txt. Most targets depend on only a subset of GMS AARs so some
185 # resources are missing, which is okay because the code that references
186 # them is missing too. We can't get an id for a resource that isn't here
187 # so the only solution is to skip the resource entry entirely.
188 #
189 # We can verify that all entries referenced in the code were generated
190 # correctly by running Proguard on the APK: it will report missing
191 # fields.
192 if entry:
193 resources_by_type[entry.resource_type].append(entry)
174 194
175 for package, resources_by_type in resources_by_package.iteritems(): 195 for package, resources_by_type in resources_by_package.iteritems():
176 package_r_java_dir = os.path.join(srcjar_dir, *package.split('.')) 196 package_r_java_dir = os.path.join(srcjar_dir, *package.split('.'))
177 build_utils.MakeDirectory(package_r_java_dir) 197 build_utils.MakeDirectory(package_r_java_dir)
178 package_r_java_path = os.path.join(package_r_java_dir, 'R.java') 198 package_r_java_path = os.path.join(package_r_java_dir, 'R.java')
179 java_file_contents = _CreateRJavaFile( 199 java_file_contents = _CreateRJavaFile(
180 package, resources_by_type, shared_resources) 200 package, resources_by_type, shared_resources)
181 with open(package_r_java_path, 'w') as f: 201 with open(package_r_java_path, 'w') as f:
182 f.write(java_file_contents) 202 f.write(java_file_contents)
183 203
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 445
426 if options.proguard_file: 446 if options.proguard_file:
427 package_command += ['-G', options.proguard_file] 447 package_command += ['-G', options.proguard_file]
428 build_utils.CheckOutput(package_command, print_stderr=False) 448 build_utils.CheckOutput(package_command, print_stderr=False)
429 449
430 # When an empty res/ directory is passed, aapt does not write an R.txt. 450 # When an empty res/ directory is passed, aapt does not write an R.txt.
431 if not os.path.exists(r_txt_path): 451 if not os.path.exists(r_txt_path):
432 build_utils.Touch(r_txt_path) 452 build_utils.Touch(r_txt_path)
433 453
434 if not options.include_all_resources: 454 if not options.include_all_resources:
455 # --include-all-resources can only be specified for generating final R
456 # classes for APK. It makes no sense for APK to have pre-generated R.txt
457 # though, because aapt-generated already lists all available resources.
458 if options.r_text_in:
459 r_txt_path = options.r_text_in
460
435 packages = list(options.extra_res_packages) 461 packages = list(options.extra_res_packages)
436 r_txt_files = list(options.extra_r_text_files) 462 r_txt_files = list(options.extra_r_text_files)
437 463
438 cur_package = options.custom_package 464 cur_package = options.custom_package
439 if not options.custom_package: 465 if not options.custom_package:
440 cur_package = _ExtractPackageFromManifest(options.android_manifest) 466 cur_package = _ExtractPackageFromManifest(options.android_manifest)
441 467
442 # Don't create a .java file for the current resource target when: 468 # Don't create a .java file for the current resource target when:
443 # - no package name was provided (either by manifest or build rules), 469 # - no package name was provided (either by manifest or build rules),
444 # - there was already a dependent android_resources() with the same 470 # - there was already a dependent android_resources() with the same
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 options, 558 options,
533 input_paths=input_paths, 559 input_paths=input_paths,
534 input_strings=input_strings, 560 input_strings=input_strings,
535 output_paths=output_paths, 561 output_paths=output_paths,
536 # TODO(agrieve): Remove R_dir when it's no longer used (used only by GYP). 562 # TODO(agrieve): Remove R_dir when it's no longer used (used only by GYP).
537 force=options.R_dir) 563 force=options.R_dir)
538 564
539 565
540 if __name__ == '__main__': 566 if __name__ == '__main__':
541 main(sys.argv[1:]) 567 main(sys.argv[1:])
OLDNEW
« no previous file with comments | « build/android/gyp/aar.py ('k') | build/config/android/internal_rules.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698