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 from pylib import build_utils | 14 from pylib import build_utils |
14 | 15 |
15 def ParseArgs(): | 16 def ParseArgs(): |
16 """Parses command line options. | 17 """Parses command line options. |
17 | 18 |
18 Returns: | 19 Returns: |
19 An options object as from optparse.OptionsParser.parse_args() | 20 An options object as from optparse.OptionsParser.parse_args() |
20 """ | 21 """ |
21 parser = optparse.OptionParser() | 22 parser = optparse.OptionParser() |
22 parser.add_option('--android-sdk', help='path to the Android SDK folder') | 23 parser.add_option('--android-sdk', help='path to the Android SDK folder') |
23 parser.add_option('--android-sdk-tools', | 24 parser.add_option('--android-sdk-tools', |
24 help='path to the Android SDK platform tools folder') | 25 help='path to the Android SDK platform tools folder') |
25 parser.add_option('--R-dir', help='directory to hold generated R.java') | 26 parser.add_option('--R-dir', help='directory to hold generated R.java') |
26 parser.add_option('--res-dir', help='directory containing resources') | 27 parser.add_option('--res-dirs', |
27 parser.add_option('--out-res-dir', | 28 help='directories containing resources to be packaged') |
| 29 parser.add_option('--crunch-input-dir', |
| 30 help='directory containing images to be crunched') |
| 31 parser.add_option('--crunch-output-dir', |
28 help='directory to hold crunched resources') | 32 help='directory to hold crunched resources') |
29 parser.add_option('--non-constant-id', action='store_true') | 33 parser.add_option('--non-constant-id', action='store_true') |
30 parser.add_option('--custom-package', help='Java package for R.java') | 34 parser.add_option('--custom-package', help='Java package for R.java') |
31 parser.add_option('--android-manifest', help='AndroidManifest.xml path') | 35 parser.add_option('--android-manifest', help='AndroidManifest.xml path') |
32 parser.add_option('--stamp', help='File to touch on success') | 36 parser.add_option('--stamp', help='File to touch on success') |
33 | 37 |
34 # This is part of a temporary fix for crbug.com/177552. | 38 # This is part of a temporary fix for crbug.com/177552. |
35 # TODO(newt): remove this once crbug.com/177552 is fixed in ninja. | 39 # TODO(newt): remove this once crbug.com/177552 is fixed in ninja. |
36 parser.add_option('--ignore', help='this argument is ignored') | 40 parser.add_option('--ignore', help='this argument is ignored') |
37 (options, args) = parser.parse_args() | 41 (options, args) = parser.parse_args() |
38 | 42 |
39 if args: | 43 if args: |
40 parser.error('No positional arguments should be given.') | 44 parser.error('No positional arguments should be given.') |
41 | 45 |
42 # Check that required options have been provided. | 46 # Check that required options have been provided. |
43 required_options = ('android_sdk', 'android_sdk_tools', | 47 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir', 'res_dirs', |
44 'R_dir', 'res_dir', 'out_res_dir') | 48 'crunch_input_dir', 'crunch_output_dir') |
45 for option_name in required_options: | 49 for option_name in required_options: |
46 if getattr(options, option_name) is None: | 50 if not getattr(options, option_name): |
47 parser.error('--%s is required' % option_name.replace('_', '-')) | 51 parser.error('--%s is required' % option_name.replace('_', '-')) |
48 | 52 |
49 return options | 53 return options |
50 | 54 |
51 | 55 |
52 def main(): | 56 def main(): |
53 options = ParseArgs() | 57 options = ParseArgs() |
54 android_jar = os.path.join(options.android_sdk, 'android.jar') | 58 android_jar = os.path.join(options.android_sdk, 'android.jar') |
55 aapt = os.path.join(options.android_sdk_tools, 'aapt') | 59 aapt = os.path.join(options.android_sdk_tools, 'aapt') |
56 | 60 |
57 build_utils.MakeDirectory(options.R_dir) | 61 build_utils.MakeDirectory(options.R_dir) |
58 | 62 |
59 # 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 |
60 # 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 |
61 # 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 |
62 # 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 |
63 # project. | 67 # project. |
64 package_command = [aapt, | 68 package_command = [aapt, |
65 'package', | 69 'package', |
66 '-m', | 70 '-m', |
67 '-M', options.android_manifest, | 71 '-M', options.android_manifest, |
68 '-S', options.res_dir, | |
69 '--auto-add-overlay', | 72 '--auto-add-overlay', |
70 '-I', android_jar, | 73 '-I', android_jar, |
71 '--output-text-symbols', options.R_dir, | 74 '--output-text-symbols', options.R_dir, |
72 '-J', options.R_dir] | 75 '-J', options.R_dir] |
73 # If strings.xml was generated from a grd file, it will be in out_res_dir. | 76 res_dirs = shlex.split(options.res_dirs) |
74 if os.path.isdir(options.out_res_dir): | 77 for res_dir in res_dirs: |
75 package_command += ['-S', options.out_res_dir] | 78 package_command += ['-S', res_dir] |
76 if options.non_constant_id: | 79 if options.non_constant_id: |
77 package_command.append('--non-constant-id') | 80 package_command.append('--non-constant-id') |
78 if options.custom_package: | 81 if options.custom_package: |
79 package_command += ['--custom-package', options.custom_package] | 82 package_command += ['--custom-package', options.custom_package] |
80 subprocess.check_call(package_command) | 83 subprocess.check_call(package_command) |
81 | 84 |
82 # Crunch image resources. This shrinks png files and is necessary for 9-patch | 85 # Crunch image resources. This shrinks png files and is necessary for 9-patch |
83 # images to display correctly. | 86 # images to display correctly. |
84 subprocess.check_call([aapt, | 87 subprocess.check_call([aapt, |
85 'crunch', | 88 'crunch', |
86 '-S', options.res_dir, | 89 '-S', options.crunch_input_dir, |
87 '-C', options.out_res_dir]) | 90 '-C', options.crunch_output_dir]) |
88 | 91 |
89 build_utils.Touch(options.stamp) | 92 build_utils.Touch(options.stamp) |
90 | 93 |
91 | 94 |
92 if __name__ == '__main__': | 95 if __name__ == '__main__': |
93 main() | 96 main() |
OLD | NEW |