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
5 # found in the LICENSE file.
7 """Process Android library resources to generate R.java and crunched images."""
14 from util import build_utils
17 """Parses command line options.
20 An options object as from optparse.OptionsParser.parse_args()
22 parser = optparse.OptionParser()
23 parser.add_option('--android-sdk', help='path to the Android SDK folder')
24 parser.add_option('--android-sdk-tools',
25 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('--res-dirs',
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',
32 help='directory to hold crunched resources')
33 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('--android-manifest', help='AndroidManifest.xml path')
36 parser.add_option('--stamp', help='File to touch on success')
38 (options, args) = parser.parse_args()
41 parser.error('No positional arguments should be given.')
43 # Check that required options have been provided.
44 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir',
45 'res_dirs', 'crunch_input_dir', 'crunch_output_dir')
46 build_utils.CheckOptions(options, parser, required=required_options)
51 def MoveImagesToNonMdpiFolders(res_root):
52 """Move images from drawable-*-mdpi-* folders to drawable-* folders.
54 Why? http://crbug.com/289843
56 for src_dir_name in os.listdir(res_root):
57 src_components = src_dir_name.split('-')
58 if src_components[0] != 'drawable' or 'mdpi' not in src_components:
60 src_dir = os.path.join(res_root, src_dir_name)
61 if not os.path.isdir(src_dir):
63 dst_components = [c for c in src_components if c != 'mdpi']
64 assert dst_components != src_components
65 dst_dir_name = '-'.join(dst_components)
66 dst_dir = os.path.join(res_root, dst_dir_name)
67 build_utils.MakeDirectory(dst_dir)
68 for src_file_name in os.listdir(src_dir):
69 if not src_file_name.endswith('.png'):
71 src_file = os.path.join(src_dir, src_file_name)
72 dst_file = os.path.join(dst_dir, src_file_name)
73 assert not os.path.lexists(dst_file)
74 shutil.move(src_file, dst_file)
77 def DidCrunchFail(returncode, stderr):
78 """Determines whether aapt crunch failed from its return code and output.
80 Because aapt's return code cannot be trusted, any output to stderr is
81 an indication that aapt has failed (http://crbug.com/314885), except
82 lines that contain "libpng warning", which is a known non-error condition
83 (http://crbug.com/364355).
87 for line in stderr.splitlines():
88 if line and not 'libpng warning' in line:
95 android_jar = os.path.join(options.android_sdk, 'android.jar')
96 aapt = os.path.join(options.android_sdk_tools, 'aapt')
98 build_utils.MakeDirectory(options.R_dir)
100 # 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
102 # 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
105 package_command = [aapt,
108 '-M', options.android_manifest,
109 '--auto-add-overlay',
111 '--output-text-symbols', options.R_dir,
113 res_dirs = shlex.split(options.res_dirs)
114 for res_dir in res_dirs:
115 package_command += ['-S', res_dir]
116 if options.non_constant_id:
117 package_command.append('--non-constant-id')
118 if options.custom_package:
119 package_command += ['--custom-package', options.custom_package]
120 build_utils.CheckOutput(package_command)
122 # Crunch image resources. This shrinks png files and is necessary for 9-patch
123 # images to display correctly.
124 build_utils.MakeDirectory(options.crunch_output_dir)
127 '-S', options.crunch_input_dir,
128 '-C', options.crunch_output_dir]
129 build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail)
131 MoveImagesToNonMdpiFolders(options.crunch_output_dir)
134 build_utils.Touch(options.stamp)
137 if __name__ == '__main__':