| 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 this target\'s resources.') |
| 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') |
| 37 parser.add_option('--proguard-file', |
| 38 help='Path to proguard.txt generated file') |
| 36 parser.add_option('--stamp', help='File to touch on success') | 39 parser.add_option('--stamp', help='File to touch on success') |
| 37 | 40 |
| 41 parser.add_option( |
| 42 '--extra-res-packages', |
| 43 help='Additional package names to generate R.java files for') |
| 44 parser.add_option( |
| 45 '--extra-r-text-files', |
| 46 help='For each additional package, the R.txt file should contain a ' |
| 47 'list of resources to be included in the R.java file in the format ' |
| 48 'generated by aapt') |
| 49 |
| 38 (options, args) = parser.parse_args() | 50 (options, args) = parser.parse_args() |
| 39 | 51 |
| 40 if args: | 52 if args: |
| 41 parser.error('No positional arguments should be given.') | 53 parser.error('No positional arguments should be given.') |
| 42 | 54 |
| 43 # Check that required options have been provided. | 55 # Check that required options have been provided. |
| 44 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir', | 56 required_options = ( |
| 45 'res_dirs', 'crunch_input_dir', 'crunch_output_dir') | 57 'android_sdk', |
| 58 'android_sdk_tools', |
| 59 'android_manifest', |
| 60 'dependencies_res_dirs', |
| 61 'resource_dir', |
| 62 'crunch_output_dir', |
| 63 'R_dir', |
| 64 ) |
| 46 build_utils.CheckOptions(options, parser, required=required_options) | 65 build_utils.CheckOptions(options, parser, required=required_options) |
| 47 | 66 |
| 48 return options | 67 return options |
| 49 | 68 |
| 50 | 69 |
| 70 def CreateExtraRJavaFiles( |
| 71 r_dir, extra_packages, extra_r_text_files): |
| 72 if len(extra_packages) != len(extra_r_text_files): |
| 73 raise Exception('--extra-res-packages and --extra-r-text-files' |
| 74 'should have the same length') |
| 75 |
| 76 java_files = build_utils.FindInDirectory(r_dir, "R.java") |
| 77 if len(java_files) != 1: |
| 78 return |
| 79 r_java_file = java_files[0] |
| 80 r_java_contents = open(r_java_file).read() |
| 81 |
| 82 for package in extra_packages: |
| 83 package_r_java_dir = os.path.join(r_dir, *package.split('.')) |
| 84 build_utils.MakeDirectory(package_r_java_dir) |
| 85 package_r_java_path = os.path.join(package_r_java_dir, 'R.java') |
| 86 open(package_r_java_path, 'w').write( |
| 87 re.sub(r'package [.\w]*;', 'package %s;' % package, r_java_contents)) |
| 88 # TODO(cjhopman): These extra package's R.java files should be filtered to |
| 89 # only contain the resources listed in their R.txt files. At this point, we |
| 90 # have already compiled those other libraries, so doing this would only |
| 91 # affect how the code in this .apk target could refer to the resources. |
| 92 |
| 93 |
| 94 |
| 51 def MoveImagesToNonMdpiFolders(res_root): | 95 def MoveImagesToNonMdpiFolders(res_root): |
| 52 """Move images from drawable-*-mdpi-* folders to drawable-* folders. | 96 """Move images from drawable-*-mdpi-* folders to drawable-* folders. |
| 53 | 97 |
| 54 Why? http://crbug.com/289843 | 98 Why? http://crbug.com/289843 |
| 55 """ | 99 """ |
| 56 for src_dir_name in os.listdir(res_root): | 100 for src_dir_name in os.listdir(res_root): |
| 57 src_components = src_dir_name.split('-') | 101 src_components = src_dir_name.split('-') |
| 58 if src_components[0] != 'drawable' or 'mdpi' not in src_components: | 102 if src_components[0] != 'drawable' or 'mdpi' not in src_components: |
| 59 continue | 103 continue |
| 60 src_dir = os.path.join(res_root, src_dir_name) | 104 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: | 132 if line and not 'libpng warning' in line: |
| 89 return True | 133 return True |
| 90 return False | 134 return False |
| 91 | 135 |
| 92 | 136 |
| 93 def main(): | 137 def main(): |
| 94 options = ParseArgs() | 138 options = ParseArgs() |
| 95 android_jar = os.path.join(options.android_sdk, 'android.jar') | 139 android_jar = os.path.join(options.android_sdk, 'android.jar') |
| 96 aapt = os.path.join(options.android_sdk_tools, 'aapt') | 140 aapt = os.path.join(options.android_sdk_tools, 'aapt') |
| 97 | 141 |
| 142 build_utils.DeleteDirectory(options.R_dir) |
| 98 build_utils.MakeDirectory(options.R_dir) | 143 build_utils.MakeDirectory(options.R_dir) |
| 99 | 144 |
| 100 # Generate R.java. This R.java contains non-final constants and is used only | 145 # 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 | 146 # 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 | 147 # 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 | 148 # generated by merging the resources from all libraries and the main apk |
| 104 # project. | 149 # project. |
| 105 package_command = [aapt, | 150 package_command = [aapt, |
| 106 'package', | 151 'package', |
| 107 '-m', | 152 '-m', |
| 108 '-M', options.android_manifest, | 153 '-M', options.android_manifest, |
| 109 '--auto-add-overlay', | 154 '--auto-add-overlay', |
| 110 '-I', android_jar, | 155 '-I', android_jar, |
| 111 '--output-text-symbols', options.R_dir, | 156 '--output-text-symbols', options.R_dir, |
| 112 '-J', options.R_dir] | 157 '-J', options.R_dir] |
| 113 res_dirs = shlex.split(options.res_dirs) | 158 all_res_dirs = ([options.resource_dir] |
| 114 for res_dir in res_dirs: | 159 + shlex.split(options.dependencies_res_dirs)) |
| 160 for res_dir in all_res_dirs: |
| 115 package_command += ['-S', res_dir] | 161 package_command += ['-S', res_dir] |
| 116 if options.non_constant_id: | 162 if options.non_constant_id: |
| 117 package_command.append('--non-constant-id') | 163 package_command.append('--non-constant-id') |
| 118 if options.custom_package: | 164 if options.custom_package: |
| 119 package_command += ['--custom-package', options.custom_package] | 165 package_command += ['--custom-package', options.custom_package] |
| 166 if options.proguard_file: |
| 167 package_command += ['-G', options.proguard_file] |
| 168 |
| 120 build_utils.CheckOutput(package_command) | 169 build_utils.CheckOutput(package_command) |
| 121 | 170 |
| 171 if options.extra_res_packages: |
| 172 CreateExtraRJavaFiles( |
| 173 options.R_dir, |
| 174 build_utils.ParseGypList(options.extra_res_packages), |
| 175 build_utils.ParseGypList(options.extra_r_text_files)) |
| 176 |
| 122 # Crunch image resources. This shrinks png files and is necessary for 9-patch | 177 # Crunch image resources. This shrinks png files and is necessary for 9-patch |
| 123 # images to display correctly. | 178 # images to display correctly. |
| 179 build_utils.DeleteDirectory(options.crunch_output_dir) |
| 124 build_utils.MakeDirectory(options.crunch_output_dir) | 180 build_utils.MakeDirectory(options.crunch_output_dir) |
| 125 aapt_cmd = [aapt, | 181 aapt_cmd = [aapt, |
| 126 'crunch', | 182 'crunch', |
| 127 '-S', options.crunch_input_dir, | 183 '-S', options.resource_dir, |
| 128 '-C', options.crunch_output_dir] | 184 '-C', options.crunch_output_dir] |
| 129 build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail) | 185 build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail) |
| 130 | 186 |
| 131 MoveImagesToNonMdpiFolders(options.crunch_output_dir) | 187 MoveImagesToNonMdpiFolders(options.crunch_output_dir) |
| 132 | 188 |
| 133 if options.stamp: | 189 if options.stamp: |
| 134 build_utils.Touch(options.stamp) | 190 build_utils.Touch(options.stamp) |
| 135 | 191 |
| 136 | 192 |
| 137 if __name__ == '__main__': | 193 if __name__ == '__main__': |
| 138 main() | 194 main() |
| OLD | NEW |