| 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 resources to generate R.java, and prepare for packaging. | 7 """Process Android resources to generate R.java, and prepare for packaging. |
| 8 | 8 |
| 9 This will crunch images and generate v14 compatible resources | 9 This will crunch images and generate v14 compatible resources |
| 10 (see generate_v14_compatible_resources.py). | 10 (see generate_v14_compatible_resources.py). |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 build_utils.MakeDirectory(package_r_java_dir) | 119 build_utils.MakeDirectory(package_r_java_dir) |
| 120 package_r_java_path = os.path.join(package_r_java_dir, 'R.java') | 120 package_r_java_path = os.path.join(package_r_java_dir, 'R.java') |
| 121 open(package_r_java_path, 'w').write( | 121 open(package_r_java_path, 'w').write( |
| 122 re.sub(r'package [.\w]*;', 'package %s;' % package, r_java_contents)) | 122 re.sub(r'package [.\w]*;', 'package %s;' % package, r_java_contents)) |
| 123 # TODO(cjhopman): These extra package's R.java files should be filtered to | 123 # TODO(cjhopman): These extra package's R.java files should be filtered to |
| 124 # only contain the resources listed in their R.txt files. At this point, we | 124 # only contain the resources listed in their R.txt files. At this point, we |
| 125 # have already compiled those other libraries, so doing this would only | 125 # have already compiled those other libraries, so doing this would only |
| 126 # affect how the code in this .apk target could refer to the resources. | 126 # affect how the code in this .apk target could refer to the resources. |
| 127 | 127 |
| 128 | 128 |
| 129 def CrunchDirectory(aapt, input_dir, output_dir): |
| 130 """Crunches the images in input_dir and its subdirectories into output_dir. |
| 131 |
| 132 If an image is already optimized, crunching often increases image size. In |
| 133 this case, the crunched image is overwritten with the original image. |
| 134 """ |
| 135 aapt_cmd = [aapt, |
| 136 'crunch', |
| 137 '-C', output_dir, |
| 138 '-S', input_dir, |
| 139 '--ignore-assets', build_utils.AAPT_IGNORE_PATTERN] |
| 140 build_utils.CheckOutput(aapt_cmd, stderr_filter=FilterCrunchStderr, |
| 141 fail_func=DidCrunchFail) |
| 142 |
| 143 # Check for images whose size increased during crunching and replace them |
| 144 # with their originals (except for 9-patches, which must be crunched). |
| 145 for dir_, _, files in os.walk(output_dir): |
| 146 for crunched in files: |
| 147 if crunched.endswith('.9.png'): |
| 148 continue |
| 149 if not crunched.endswith('.png'): |
| 150 raise Exception('Unexpected file in crunched dir: ' + crunched) |
| 151 crunched = os.path.join(dir_, crunched) |
| 152 original = os.path.join(input_dir, os.path.relpath(crunched, output_dir)) |
| 153 original_size = os.path.getsize(original) |
| 154 crunched_size = os.path.getsize(crunched) |
| 155 if original_size < crunched_size: |
| 156 shutil.copyfile(original, crunched) |
| 157 |
| 158 |
| 129 def FilterCrunchStderr(stderr): | 159 def FilterCrunchStderr(stderr): |
| 130 """Filters out lines from aapt crunch's stderr that can safely be ignored.""" | 160 """Filters out lines from aapt crunch's stderr that can safely be ignored.""" |
| 131 filtered_lines = [] | 161 filtered_lines = [] |
| 132 for line in stderr.splitlines(True): | 162 for line in stderr.splitlines(True): |
| 133 # Ignore this libpng warning, which is a known non-error condition. | 163 # Ignore this libpng warning, which is a known non-error condition. |
| 134 # http://crbug.com/364355 | 164 # http://crbug.com/364355 |
| 135 if ('libpng warning: iCCP: Not recognizing known sRGB profile that has ' | 165 if ('libpng warning: iCCP: Not recognizing known sRGB profile that has ' |
| 136 + 'been edited' in line): | 166 + 'been edited' in line): |
| 137 continue | 167 continue |
| 138 filtered_lines.append(line) | 168 filtered_lines.append(line) |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 # This is the list of directories with resources to put in the final .zip | 283 # This is the list of directories with resources to put in the final .zip |
| 254 # file. The order of these is important so that crunched/v14 resources | 284 # file. The order of these is important so that crunched/v14 resources |
| 255 # override the normal ones. | 285 # override the normal ones. |
| 256 zip_resource_dirs = input_resource_dirs + [v14_dir] | 286 zip_resource_dirs = input_resource_dirs + [v14_dir] |
| 257 | 287 |
| 258 base_crunch_dir = os.path.join(temp_dir, 'crunch') | 288 base_crunch_dir = os.path.join(temp_dir, 'crunch') |
| 259 | 289 |
| 260 # Crunch image resources. This shrinks png files and is necessary for | 290 # Crunch image resources. This shrinks png files and is necessary for |
| 261 # 9-patch images to display correctly. 'aapt crunch' accepts only a single | 291 # 9-patch images to display correctly. 'aapt crunch' accepts only a single |
| 262 # directory at a time and deletes everything in the output directory. | 292 # directory at a time and deletes everything in the output directory. |
| 263 for idx, d in enumerate(input_resource_dirs): | 293 for idx, input_dir in enumerate(input_resource_dirs): |
| 264 crunch_dir = os.path.join(base_crunch_dir, str(idx)) | 294 crunch_dir = os.path.join(base_crunch_dir, str(idx)) |
| 265 build_utils.MakeDirectory(crunch_dir) | 295 build_utils.MakeDirectory(crunch_dir) |
| 266 zip_resource_dirs.append(crunch_dir) | 296 zip_resource_dirs.append(crunch_dir) |
| 267 aapt_cmd = [aapt, | 297 CrunchDirectory(aapt, input_dir, crunch_dir) |
| 268 'crunch', | |
| 269 '-C', crunch_dir, | |
| 270 '-S', d, | |
| 271 '--ignore-assets', build_utils.AAPT_IGNORE_PATTERN] | |
| 272 build_utils.CheckOutput(aapt_cmd, stderr_filter=FilterCrunchStderr, | |
| 273 fail_func=DidCrunchFail) | |
| 274 | 298 |
| 275 ZipResources(zip_resource_dirs, options.resource_zip_out) | 299 ZipResources(zip_resource_dirs, options.resource_zip_out) |
| 276 | 300 |
| 277 if options.all_resources_zip_out: | 301 if options.all_resources_zip_out: |
| 278 CombineZips([options.resource_zip_out] + dep_zips, | 302 CombineZips([options.resource_zip_out] + dep_zips, |
| 279 options.all_resources_zip_out) | 303 options.all_resources_zip_out) |
| 280 | 304 |
| 281 if options.R_dir: | 305 if options.R_dir: |
| 282 build_utils.DeleteDirectory(options.R_dir) | 306 build_utils.DeleteDirectory(options.R_dir) |
| 283 shutil.copytree(gen_dir, options.R_dir) | 307 shutil.copytree(gen_dir, options.R_dir) |
| 284 else: | 308 else: |
| 285 build_utils.ZipDir(options.srcjar_out, gen_dir) | 309 build_utils.ZipDir(options.srcjar_out, gen_dir) |
| 286 | 310 |
| 287 if options.depfile: | 311 if options.depfile: |
| 288 input_files += build_utils.GetPythonDependencies() | 312 input_files += build_utils.GetPythonDependencies() |
| 289 build_utils.WriteDepfile(options.depfile, input_files) | 313 build_utils.WriteDepfile(options.depfile, input_files) |
| 290 | 314 |
| 291 if options.stamp: | 315 if options.stamp: |
| 292 build_utils.Touch(options.stamp) | 316 build_utils.Touch(options.stamp) |
| 293 | 317 |
| 294 | 318 |
| 295 if __name__ == '__main__': | 319 if __name__ == '__main__': |
| 296 main() | 320 main() |
| OLD | NEW |