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 re | |
11 import shlex | 12 import shlex |
12 import shutil | 13 import shutil |
13 | 14 |
14 from util import build_utils | 15 from util import build_utils |
15 | 16 |
16 def ParseArgs(): | 17 def ParseArgs(): |
17 """Parses command line options. | 18 """Parses command line options. |
18 | 19 |
19 Returns: | 20 Returns: |
20 An options object as from optparse.OptionsParser.parse_args() | 21 An options object as from optparse.OptionsParser.parse_args() |
21 """ | 22 """ |
22 parser = optparse.OptionParser() | 23 parser = optparse.OptionParser() |
23 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') |
24 parser.add_option('--android-sdk-tools', | 25 parser.add_option('--android-sdk-tools', |
25 help='path to the Android SDK build tools folder') | 26 help='path to the Android SDK build tools folder') |
26 parser.add_option('--R-dir', help='directory to hold generated R.java') | 27 parser.add_option('--R-dir', help='directory to hold generated R.java') |
27 parser.add_option('--res-dirs', | 28 parser.add_option('--dependencies-res-dirs', |
28 help='directories containing resources to be packaged') | 29 help='directories containing resources to be packaged') |
29 parser.add_option('--crunch-input-dir', | 30 parser.add_option('--resource-dir', |
30 help='directory containing images to be crunched') | 31 help='directory containing images to be crunched') |
Yaron
2014/05/19 18:33:00
isn't this all resources and not just crunched ima
cjhopman
2014/05/20 20:40:41
Done.
| |
31 parser.add_option('--crunch-output-dir', | 32 parser.add_option('--crunch-output-dir', |
32 help='directory to hold crunched resources') | 33 help='directory to hold crunched resources') |
33 parser.add_option('--non-constant-id', action='store_true') | 34 parser.add_option('--non-constant-id', action='store_true') |
34 parser.add_option('--custom-package', help='Java package for R.java') | 35 parser.add_option('--custom-package', help='Java package for R.java') |
35 parser.add_option('--android-manifest', help='AndroidManifest.xml path') | 36 parser.add_option('--android-manifest', help='AndroidManifest.xml path') |
36 parser.add_option('--stamp', help='File to touch on success') | 37 parser.add_option('--stamp', help='File to touch on success') |
37 | 38 |
39 parser.add_option( | |
40 '--extra-res-packages', | |
41 help='Additional package names to generate R.java files for') | |
42 parser.add_option( | |
43 '--extra-r-text-files', | |
44 help='For each additional package, the R.txt file should contain a ' | |
45 'list of resources to be included in the R.java file in the format ' | |
46 'generated by aapt') | |
47 | |
38 (options, args) = parser.parse_args() | 48 (options, args) = parser.parse_args() |
39 | 49 |
40 if args: | 50 if args: |
41 parser.error('No positional arguments should be given.') | 51 parser.error('No positional arguments should be given.') |
42 | 52 |
43 # Check that required options have been provided. | 53 # Check that required options have been provided. |
44 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir', | 54 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir', |
45 'res_dirs', 'crunch_input_dir', 'crunch_output_dir') | 55 'res_dirs', 'crunch_input_dir', 'crunch_output_dir') |
46 build_utils.CheckOptions(options, parser, required=required_options) | 56 #build_utils.CheckOptions(options, parser, required=required_options) |
Yaron
2014/05/19 18:33:00
?
cjhopman
2014/05/20 20:40:41
Done.
| |
47 | 57 |
48 return options | 58 return options |
49 | 59 |
50 | 60 |
61 def CreateExtraRJavaFiles( | |
62 r_dir, extra_packages, extra_r_text_files): | |
63 if len(extra_packages) != len(extra_r_text_files): | |
64 raise Exception('--extra-res-packages and --extra-r-text-files' | |
65 'should have the same length') | |
66 | |
67 java_files = build_utils.FindInDirectory(r_dir, "R.java") | |
68 if len(java_files) != 1: | |
69 raise Exception('couldn\'t find R.java file: ' + ','.join(java_files)) | |
70 r_java_file = java_files[0] | |
71 r_java_contents = open(r_java_file).read() | |
72 | |
73 for package in extra_packages: | |
74 package_r_java_dir = os.path.join(r_dir, *package.split('.')) | |
75 build_utils.MakeDirectory(package_r_java_dir) | |
76 package_r_java_path = os.path.join(package_r_java_dir, 'R.java') | |
77 open(package_r_java_path, 'w').write( | |
78 re.sub(r'package [.\w]*;', 'package %s;' % package, r_java_contents)) | |
79 # TODO(cjhopman): These extra package's R.java files should be filtered to | |
80 # only contain the resources listed in their R.txt files. At this point, we | |
81 # have already compiled those other libraries, so doing this would only | |
82 # affect how the code in this .apk target could refer to the resources. | |
83 | |
84 | |
85 | |
51 def MoveImagesToNonMdpiFolders(res_root): | 86 def MoveImagesToNonMdpiFolders(res_root): |
52 """Move images from drawable-*-mdpi-* folders to drawable-* folders. | 87 """Move images from drawable-*-mdpi-* folders to drawable-* folders. |
53 | 88 |
54 Why? http://crbug.com/289843 | 89 Why? http://crbug.com/289843 |
55 """ | 90 """ |
56 for src_dir_name in os.listdir(res_root): | 91 for src_dir_name in os.listdir(res_root): |
57 src_components = src_dir_name.split('-') | 92 src_components = src_dir_name.split('-') |
58 if src_components[0] != 'drawable' or 'mdpi' not in src_components: | 93 if src_components[0] != 'drawable' or 'mdpi' not in src_components: |
59 continue | 94 continue |
60 src_dir = os.path.join(res_root, src_dir_name) | 95 src_dir = os.path.join(res_root, src_dir_name) |
(...skipping 27 matching lines...) Expand all Loading... | |
88 if line and not 'libpng warning' in line: | 123 if line and not 'libpng warning' in line: |
89 return True | 124 return True |
90 return False | 125 return False |
91 | 126 |
92 | 127 |
93 def main(): | 128 def main(): |
94 options = ParseArgs() | 129 options = ParseArgs() |
95 android_jar = os.path.join(options.android_sdk, 'android.jar') | 130 android_jar = os.path.join(options.android_sdk, 'android.jar') |
96 aapt = os.path.join(options.android_sdk_tools, 'aapt') | 131 aapt = os.path.join(options.android_sdk_tools, 'aapt') |
97 | 132 |
133 build_utils.DeleteDirectory(options.R_dir) | |
Yaron
2014/05/19 18:33:00
So any touched resources causes full reprocessing?
cjhopman
2014/05/20 20:40:41
Really, the only thing in this directory should be
| |
98 build_utils.MakeDirectory(options.R_dir) | 134 build_utils.MakeDirectory(options.R_dir) |
99 | 135 |
100 # Generate R.java. This R.java contains non-final constants and is used only | 136 # Generate R.java. This R.java contains non-final constants and is used only |
101 # while compiling the library jar (e.g. chromium_content.jar). When building | 137 # while compiling the library jar (e.g. chromium_content.jar). When building |
102 # an apk, a new R.java file with the correct resource -> ID mappings will be | 138 # an apk, a new R.java file with the correct resource -> ID mappings will be |
103 # generated by merging the resources from all libraries and the main apk | 139 # generated by merging the resources from all libraries and the main apk |
104 # project. | 140 # project. |
105 package_command = [aapt, | 141 package_command = [aapt, |
106 'package', | 142 'package', |
107 '-m', | 143 '-m', |
108 '-M', options.android_manifest, | 144 '-M', options.android_manifest, |
109 '--auto-add-overlay', | 145 '--auto-add-overlay', |
110 '-I', android_jar, | 146 '-I', android_jar, |
111 '--output-text-symbols', options.R_dir, | 147 '--output-text-symbols', options.R_dir, |
112 '-J', options.R_dir] | 148 '-J', options.R_dir] |
113 res_dirs = shlex.split(options.res_dirs) | 149 all_res_dirs = (shlex.split(options.dependencies_res_dirs) |
114 for res_dir in res_dirs: | 150 + [options.resource_dir]) |
151 for res_dir in all_res_dirs: | |
115 package_command += ['-S', res_dir] | 152 package_command += ['-S', res_dir] |
116 if options.non_constant_id: | 153 if options.non_constant_id: |
117 package_command.append('--non-constant-id') | 154 package_command.append('--non-constant-id') |
118 if options.custom_package: | 155 if options.custom_package: |
119 package_command += ['--custom-package', options.custom_package] | 156 package_command += ['--custom-package', options.custom_package] |
120 build_utils.CheckOutput(package_command) | 157 build_utils.CheckOutput(package_command) |
121 | 158 |
159 if options.extra_res_packages: | |
160 CreateExtraRJavaFiles( | |
161 options.R_dir, | |
162 build_utils.ParseGypList(options.extra_res_packages), | |
163 build_utils.ParseGypList(options.extra_r_text_files)) | |
164 | |
122 # Crunch image resources. This shrinks png files and is necessary for 9-patch | 165 # Crunch image resources. This shrinks png files and is necessary for 9-patch |
123 # images to display correctly. | 166 # images to display correctly. |
124 build_utils.MakeDirectory(options.crunch_output_dir) | 167 build_utils.MakeDirectory(options.crunch_output_dir) |
125 aapt_cmd = [aapt, | 168 aapt_cmd = [aapt, |
126 'crunch', | 169 'crunch', |
127 '-S', options.crunch_input_dir, | 170 '-S', options.resource_dir, |
128 '-C', options.crunch_output_dir] | 171 '-C', options.crunch_output_dir] |
129 build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail) | 172 build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail) |
130 | 173 |
131 MoveImagesToNonMdpiFolders(options.crunch_output_dir) | 174 MoveImagesToNonMdpiFolders(options.crunch_output_dir) |
132 | 175 |
133 if options.stamp: | 176 if options.stamp: |
134 build_utils.Touch(options.stamp) | 177 build_utils.Touch(options.stamp) |
135 | 178 |
136 | 179 |
137 if __name__ == '__main__': | 180 if __name__ == '__main__': |
138 main() | 181 main() |
OLD | NEW |