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