Chromium Code Reviews| OLD | NEW |
|---|---|
| 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). |
| 11 """ | 11 """ |
| 12 | 12 |
| 13 import codecs | |
| 14 import optparse | 13 import optparse |
| 15 import os | 14 import os |
| 16 import re | 15 import re |
| 17 import shutil | 16 import shutil |
| 17 from string import Template | |
| 18 import sys | 18 import sys |
| 19 import zipfile | 19 import zipfile |
| 20 | 20 |
| 21 import generate_v14_compatible_resources | 21 import generate_v14_compatible_resources |
| 22 | 22 |
| 23 from util import build_utils | 23 from util import build_utils |
| 24 | 24 |
| 25 | 25 |
| 26 def ParseArgs(args): | 26 def ParseArgs(args): |
| 27 """Parses command line options. | 27 """Parses command line options. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 50 parser.add_option('--dependencies-res-zips', | 50 parser.add_option('--dependencies-res-zips', |
| 51 help='Resources from dependents.') | 51 help='Resources from dependents.') |
| 52 | 52 |
| 53 parser.add_option('--resource-zip-out', | 53 parser.add_option('--resource-zip-out', |
| 54 help='Path for output zipped resources.') | 54 help='Path for output zipped resources.') |
| 55 | 55 |
| 56 parser.add_option('--R-dir', | 56 parser.add_option('--R-dir', |
| 57 help='directory to hold generated R.java.') | 57 help='directory to hold generated R.java.') |
| 58 parser.add_option('--srcjar-out', | 58 parser.add_option('--srcjar-out', |
| 59 help='Path to srcjar to contain generated R.java.') | 59 help='Path to srcjar to contain generated R.java.') |
| 60 parser.add_option('--r-text-out', | |
| 61 help='Path to store the R.txt file generated by appt.') | |
| 60 | 62 |
| 61 parser.add_option('--proguard-file', | 63 parser.add_option('--proguard-file', |
| 62 help='Path to proguard.txt generated file') | 64 help='Path to proguard.txt generated file') |
| 63 | 65 |
| 64 parser.add_option( | 66 parser.add_option( |
| 65 '--v14-verify-only', | 67 '--v14-verify-only', |
| 66 action='store_true', | 68 action='store_true', |
| 67 help='Do not generate v14 resources. Instead, just verify that the ' | 69 help='Do not generate v14 resources. Instead, just verify that the ' |
| 68 'resources are already compatible with v14, i.e. they don\'t use ' | 70 'resources are already compatible with v14, i.e. they don\'t use ' |
| 69 'attributes that cause crashes on certain devices.') | 71 'attributes that cause crashes on certain devices.') |
| 70 | 72 |
| 71 parser.add_option( | 73 parser.add_option( |
| 72 '--extra-res-packages', | 74 '--extra-res-packages', |
| 73 help='Additional package names to generate R.java files for') | 75 help='Additional package names to generate R.java files for') |
| 74 # TODO(cjhopman): Actually use --extra-r-text-files. We currently include all | |
| 75 # the resources in all R.java files for a particular apk. | |
| 76 parser.add_option( | 76 parser.add_option( |
| 77 '--extra-r-text-files', | 77 '--extra-r-text-files', |
| 78 help='For each additional package, the R.txt file should contain a ' | 78 help='For each additional package, the R.txt file should contain a ' |
| 79 'list of resources to be included in the R.java file in the format ' | 79 'list of resources to be included in the R.java file in the format ' |
| 80 'generated by aapt') | 80 'generated by aapt') |
| 81 | 81 |
| 82 parser.add_option( | 82 parser.add_option( |
| 83 '--all-resources-zip-out', | 83 '--all-resources-zip-out', |
| 84 help='Path for output of all resources. This includes resources in ' | 84 help='Path for output of all resources. This includes resources in ' |
| 85 'dependencies.') | 85 'dependencies.') |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 101 'resource_zip_out', | 101 'resource_zip_out', |
| 102 ) | 102 ) |
| 103 build_utils.CheckOptions(options, parser, required=required_options) | 103 build_utils.CheckOptions(options, parser, required=required_options) |
| 104 | 104 |
| 105 if (options.R_dir is None) == (options.srcjar_out is None): | 105 if (options.R_dir is None) == (options.srcjar_out is None): |
| 106 raise Exception('Exactly one of --R-dir or --srcjar-out must be specified.') | 106 raise Exception('Exactly one of --R-dir or --srcjar-out must be specified.') |
| 107 | 107 |
| 108 return options | 108 return options |
| 109 | 109 |
| 110 | 110 |
| 111 def CreateExtraRJavaFiles(r_dir, extra_packages): | 111 def CreateExtraRJavaFiles(r_dir, extra_packages, extra_r_text_files): |
| 112 java_files = build_utils.FindInDirectory(r_dir, "R.java") | 112 r_txt_files = build_utils.FindInDirectory(r_dir, 'R.txt') |
|
cjhopman
2015/04/27 18:34:47
Below you find the R.txt by just doing os.path.joi
Ian Cullinan
2015/04/27 18:58:46
Acknowledged.
| |
| 113 if len(java_files) != 1: | 113 if len(r_txt_files) != 1: |
| 114 return | 114 return |
| 115 r_java_file = java_files[0] | 115 if len(extra_packages) != len(extra_r_text_files): |
| 116 r_java_contents = codecs.open(r_java_file, encoding='utf-8').read() | 116 raise Exception('Need one R.txt file per extra package') |
| 117 | 117 |
| 118 for package in extra_packages: | 118 all_resources = {} |
| 119 with open(r_txt_files[0]) as f: | |
| 120 for line in f: | |
| 121 m = re.match(r'(int(?:\[\])?) (\w+) (\w+) (.+)$', line) | |
| 122 if not m: | |
| 123 raise Exception('Unexpected line in R.txt: %s' % line) | |
| 124 java_type, resource_type, name, value = m.groups() | |
| 125 all_resources[(resource_type, name)] = (java_type, value) | |
| 126 | |
| 127 for package, r_text_file in zip(extra_packages, extra_r_text_files): | |
| 119 package_r_java_dir = os.path.join(r_dir, *package.split('.')) | 128 package_r_java_dir = os.path.join(r_dir, *package.split('.')) |
| 120 build_utils.MakeDirectory(package_r_java_dir) | 129 build_utils.MakeDirectory(package_r_java_dir) |
| 121 package_r_java_path = os.path.join(package_r_java_dir, 'R.java') | 130 package_r_java_path = os.path.join(package_r_java_dir, 'R.java') |
| 122 new_r_java = re.sub(r'package [.\w]*;', u'package %s;' % package, | 131 CreateExtraRJavaFile( |
| 123 r_java_contents) | 132 package, package_r_java_path, r_text_file, all_resources) |
| 124 codecs.open(package_r_java_path, 'w', encoding='utf-8').write(new_r_java) | 133 |
| 125 # TODO(cjhopman): These extra package's R.java files should be filtered to | 134 |
| 126 # only contain the resources listed in their R.txt files. At this point, we | 135 def CreateExtraRJavaFile(package, r_java_path, r_text_file, all_resources): |
| 127 # have already compiled those other libraries, so doing this would only | 136 resources = {} |
| 128 # affect how the code in this .apk target could refer to the resources. | 137 with open(r_text_file) as f: |
| 138 for line in f: | |
| 139 m = re.match(r'int(?:\[\])? (\w+) (\w+) ', line) | |
| 140 if not m: | |
| 141 raise Exception('Unexpected line in R.txt: %s' % line) | |
| 142 resource_type, name = m.groups() | |
| 143 if resource_type not in resources: | |
| 144 resources[resource_type] = [] | |
| 145 resources[resource_type].append(name) | |
| 146 | |
| 147 template = Template("""/* AUTO-GENERATED FILE. DO NOT MODIFY. */ | |
| 148 | |
| 149 package ${PACKAGE}; | |
| 150 | |
| 151 public final class R { | |
| 152 ${RESOURCE_CLASSES} | |
| 153 } | |
| 154 """) | |
| 155 | |
| 156 class_template = Template(""" public static final class ${RESOURCE_TYPE} { | |
| 157 ${RESOURCES} | |
| 158 } | |
| 159 """) | |
| 160 | |
| 161 resource_template = Template( | |
| 162 ' public static final ${TYPE} ${NAME} = ${VALUE};') | |
| 163 resource_classes = [] | |
| 164 for resource_type in sorted(resources.keys()): | |
| 165 resource_strings = [] | |
| 166 for name in resources[resource_type]: | |
| 167 java_type, value = all_resources[(resource_type, name)] | |
| 168 values = { | |
| 169 'TYPE': java_type, | |
| 170 'NAME': name, | |
| 171 'VALUE': value | |
| 172 } | |
| 173 resource_strings.append(resource_template.substitute(values)) | |
| 174 values = { | |
| 175 'RESOURCE_TYPE': resource_type, | |
| 176 'RESOURCES': '\n'.join(resource_strings) | |
| 177 } | |
| 178 resource_classes.append(class_template.substitute(values)) | |
| 179 values = { | |
| 180 'PACKAGE': package, | |
| 181 'RESOURCE_CLASSES': '\n'.join(resource_classes) | |
| 182 } | |
| 183 with open(r_java_path, 'w') as f: | |
| 184 f.write(template.substitute(values)) | |
| 129 | 185 |
| 130 | 186 |
| 131 def CrunchDirectory(aapt, input_dir, output_dir): | 187 def CrunchDirectory(aapt, input_dir, output_dir): |
| 132 """Crunches the images in input_dir and its subdirectories into output_dir. | 188 """Crunches the images in input_dir and its subdirectories into output_dir. |
| 133 | 189 |
| 134 If an image is already optimized, crunching often increases image size. In | 190 If an image is already optimized, crunching often increases image size. In |
| 135 this case, the crunched image is overwritten with the original image. | 191 this case, the crunched image is overwritten with the original image. |
| 136 """ | 192 """ |
| 137 aapt_cmd = [aapt, | 193 aapt_cmd = [aapt, |
| 138 'crunch', | 194 'crunch', |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 package_command += ['--custom-package', options.custom_package] | 329 package_command += ['--custom-package', options.custom_package] |
| 274 if options.proguard_file: | 330 if options.proguard_file: |
| 275 package_command += ['-G', options.proguard_file] | 331 package_command += ['-G', options.proguard_file] |
| 276 if options.shared_resources: | 332 if options.shared_resources: |
| 277 package_command.append('--shared-lib') | 333 package_command.append('--shared-lib') |
| 278 build_utils.CheckOutput(package_command, print_stderr=False) | 334 build_utils.CheckOutput(package_command, print_stderr=False) |
| 279 | 335 |
| 280 if options.extra_res_packages: | 336 if options.extra_res_packages: |
| 281 CreateExtraRJavaFiles( | 337 CreateExtraRJavaFiles( |
| 282 gen_dir, | 338 gen_dir, |
| 283 build_utils.ParseGypList(options.extra_res_packages)) | 339 build_utils.ParseGypList(options.extra_res_packages), |
| 340 build_utils.ParseGypList(options.extra_r_text_files)) | |
| 284 | 341 |
| 285 # This is the list of directories with resources to put in the final .zip | 342 # This is the list of directories with resources to put in the final .zip |
| 286 # file. The order of these is important so that crunched/v14 resources | 343 # file. The order of these is important so that crunched/v14 resources |
| 287 # override the normal ones. | 344 # override the normal ones. |
| 288 zip_resource_dirs = input_resource_dirs + [v14_dir] | 345 zip_resource_dirs = input_resource_dirs + [v14_dir] |
| 289 | 346 |
| 290 base_crunch_dir = os.path.join(temp_dir, 'crunch') | 347 base_crunch_dir = os.path.join(temp_dir, 'crunch') |
| 291 | 348 |
| 292 # Crunch image resources. This shrinks png files and is necessary for | 349 # Crunch image resources. This shrinks png files and is necessary for |
| 293 # 9-patch images to display correctly. 'aapt crunch' accepts only a single | 350 # 9-patch images to display correctly. 'aapt crunch' accepts only a single |
| 294 # directory at a time and deletes everything in the output directory. | 351 # directory at a time and deletes everything in the output directory. |
| 295 for idx, input_dir in enumerate(input_resource_dirs): | 352 for idx, input_dir in enumerate(input_resource_dirs): |
| 296 crunch_dir = os.path.join(base_crunch_dir, str(idx)) | 353 crunch_dir = os.path.join(base_crunch_dir, str(idx)) |
| 297 build_utils.MakeDirectory(crunch_dir) | 354 build_utils.MakeDirectory(crunch_dir) |
| 298 zip_resource_dirs.append(crunch_dir) | 355 zip_resource_dirs.append(crunch_dir) |
| 299 CrunchDirectory(aapt, input_dir, crunch_dir) | 356 CrunchDirectory(aapt, input_dir, crunch_dir) |
| 300 | 357 |
| 301 ZipResources(zip_resource_dirs, options.resource_zip_out) | 358 ZipResources(zip_resource_dirs, options.resource_zip_out) |
| 302 | 359 |
| 303 if options.all_resources_zip_out: | 360 if options.all_resources_zip_out: |
| 304 CombineZips([options.resource_zip_out] + dep_zips, | 361 CombineZips([options.resource_zip_out] + dep_zips, |
| 305 options.all_resources_zip_out) | 362 options.all_resources_zip_out) |
| 306 | 363 |
| 307 if options.R_dir: | 364 if options.R_dir: |
| 308 build_utils.DeleteDirectory(options.R_dir) | 365 build_utils.DeleteDirectory(options.R_dir) |
| 309 shutil.copytree(gen_dir, options.R_dir) | 366 shutil.copytree(gen_dir, options.R_dir) |
| 310 else: | 367 else: |
| 311 build_utils.ZipDir(options.srcjar_out, gen_dir) | 368 build_utils.ZipDir(options.srcjar_out, gen_dir) |
| 312 | 369 |
| 370 if options.r_text_out: | |
| 371 r_text_path = os.path.join(gen_dir, 'R.txt') | |
| 372 shutil.copyfile(r_text_path, options.r_text_out) | |
| 373 | |
| 313 if options.depfile: | 374 if options.depfile: |
| 314 input_files += build_utils.GetPythonDependencies() | 375 input_files += build_utils.GetPythonDependencies() |
| 315 build_utils.WriteDepfile(options.depfile, input_files) | 376 build_utils.WriteDepfile(options.depfile, input_files) |
| 316 | 377 |
| 317 if options.stamp: | 378 if options.stamp: |
| 318 build_utils.Touch(options.stamp) | 379 build_utils.Touch(options.stamp) |
| 319 | 380 |
| 320 | 381 |
| 321 if __name__ == '__main__': | 382 if __name__ == '__main__': |
| 322 main() | 383 main() |
| OLD | NEW |