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