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 library resources to generate R.java and crunched images.""" | 7 """Process Android library resources to generate R.java and crunched images.""" |
| 8 | 8 |
| 9 import optparse | 9 import optparse |
| 10 import os | 10 import os |
| 11 import shlex | |
| 11 import subprocess | 12 import subprocess |
| 12 | 13 |
| 13 | 14 |
| 14 BUILD_ANDROID_DIR = os.path.dirname(__file__) | 15 BUILD_ANDROID_DIR = os.path.dirname(__file__) |
| 15 | 16 |
| 16 | 17 |
| 17 def ParseArgs(): | 18 def ParseArgs(): |
| 18 """Parses command line options. | 19 """Parses command line options. |
| 19 | 20 |
| 20 Returns: | 21 Returns: |
| 21 An options object as from optparse.OptionsParser.parse_args() | 22 An options object as from optparse.OptionsParser.parse_args() |
| 22 """ | 23 """ |
| 23 parser = optparse.OptionParser() | 24 parser = optparse.OptionParser() |
| 24 parser.add_option('--android-sdk', help='path to the Android SDK folder') | 25 parser.add_option('--android-sdk', help='path to the Android SDK folder') |
| 25 parser.add_option('--android-sdk-tools', | 26 parser.add_option('--android-sdk-tools', |
| 26 help='path to the Android SDK platform tools folder') | 27 help='path to the Android SDK platform tools folder') |
| 27 parser.add_option('--R-package', help='Java package for generated R.java') | 28 parser.add_option('--R-package', help='Java package for generated R.java') |
| 28 parser.add_option('--R-dir', help='directory to hold generated R.java') | 29 parser.add_option('--R-dir', help='directory to hold generated R.java') |
| 29 parser.add_option('--res-dir', help='directory containing resources') | 30 parser.add_option('--res-input-dirs', |
| 30 parser.add_option('--out-res-dir', | 31 help='list of directories containing resources') |
| 32 parser.add_option('--res-crunched-dir', | |
| 31 help='directory to hold crunched resources') | 33 help='directory to hold crunched resources') |
| 32 # This is part of a temporary fix for crbug.com/177552. | 34 # This is part of a temporary fix for crbug.com/177552. |
| 33 # TODO(newt): remove this once crbug.com/177552 is fixed in ninja. | 35 # TODO(newt): remove this once crbug.com/177552 is fixed in ninja. |
| 34 parser.add_option('--ignore', help='this argument is ignored') | 36 parser.add_option('--ignore', help='this argument is ignored') |
| 35 (options, args) = parser.parse_args() | 37 (options, args) = parser.parse_args() |
| 36 | 38 |
| 37 if args: | 39 if args: |
| 38 parser.error('No positional arguments should be given.') | 40 parser.error('No positional arguments should be given.') |
| 39 | 41 |
| 40 # Check that required options have been provided. | 42 # Check that required options have been provided. |
| 41 required_options = ('android_sdk', 'android_sdk_tools', 'R_package', | 43 required_options = ('android_sdk', 'android_sdk_tools', 'R_package', |
| 42 'R_dir', 'res_dir', 'out_res_dir') | 44 'R_dir', 'res_input_dirs', 'res_crunched_dir') |
| 43 for option_name in required_options: | 45 for option_name in required_options: |
| 44 if getattr(options, option_name) is None: | 46 if not getattr(options, option_name): |
| 45 parser.error('--%s is required' % option_name.replace('_', '-')) | 47 parser.error('--%s is required' % option_name.replace('_', '-')) |
| 46 | 48 |
| 47 return options | 49 return options |
| 48 | 50 |
| 49 | 51 |
| 50 def main(): | 52 def main(): |
| 51 options = ParseArgs() | 53 options = ParseArgs() |
| 52 android_jar = os.path.join(options.android_sdk, 'android.jar') | 54 android_jar = os.path.join(options.android_sdk, 'android.jar') |
| 53 aapt = os.path.join(options.android_sdk_tools, 'aapt') | 55 aapt = os.path.join(options.android_sdk_tools, 'aapt') |
| 54 dummy_manifest = os.path.join(BUILD_ANDROID_DIR, 'AndroidManifest.xml') | 56 dummy_manifest = os.path.join(BUILD_ANDROID_DIR, 'AndroidManifest.xml') |
| 55 | 57 |
| 58 res_dirs = shlex.split(options.res_input_dirs) | |
| 59 res_dirs_args = [] | |
| 60 for res_dir in res_dirs: | |
| 61 res_dirs_args += ['-S', res_dir] | |
| 62 | |
| 56 # Generate R.java. This R.java contains non-final constants and is used only | 63 # Generate R.java. This R.java contains non-final constants and is used only |
| 57 # while compiling the library jar (e.g. chromium_content.jar). When building | 64 # while compiling the library jar (e.g. chromium_content.jar). When building |
| 58 # an apk, a new R.java file with the correct resource -> ID mappings will be | 65 # an apk, a new R.java file with the correct resource -> ID mappings will be |
| 59 # generated by merging the resources from all libraries and the main apk | 66 # generated by merging the resources from all libraries and the main apk |
| 60 # project. | 67 # project. |
| 61 package_command = [aapt, | 68 package_command = [aapt, |
| 62 'package', | 69 'package', |
| 63 '-m', | 70 '-m', |
| 64 '--non-constant-id', | 71 '--non-constant-id', |
| 65 '--custom-package', options.R_package, | 72 '--custom-package', options.R_package, |
| 66 '-M', dummy_manifest, | 73 '-M', dummy_manifest, |
| 67 '-S', options.res_dir, | |
| 68 '--auto-add-overlay', | 74 '--auto-add-overlay', |
| 69 '-I', android_jar, | 75 '-I', android_jar, |
| 70 '--output-text-symbols', options.R_dir, | 76 '--output-text-symbols', options.R_dir, |
| 71 '-J', options.R_dir] | 77 '-J', options.R_dir] + res_dirs_args |
|
cjhopman
2013/03/19 20:27:37
Maybe break before the + like below
| |
| 72 # If strings.xml was generated from a grd file, it will be in out_res_dir. | |
| 73 if os.path.isdir(options.out_res_dir): | |
| 74 package_command += ['-S', options.out_res_dir] | |
| 75 subprocess.check_call(package_command) | 78 subprocess.check_call(package_command) |
| 76 | 79 |
| 77 # Crunch image resources. This shrinks png files and is necessary for 9-patch | 80 # Crunch image resources. This shrinks png files and is necessary for 9-patch |
| 78 # images to display correctly. | 81 # images to display correctly. |
| 79 subprocess.check_call([aapt, | 82 subprocess.check_call([aapt, |
| 80 'crunch', | 83 'crunch', |
| 81 '-S', options.res_dir, | 84 '-C', options.res_crunched_dir] |
| 82 '-C', options.out_res_dir]) | 85 + res_dirs_args) |
| 83 | 86 |
| 84 | 87 |
| 85 if __name__ == '__main__': | 88 if __name__ == '__main__': |
| 86 main() | 89 main() |
| OLD | NEW |