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 # This is part of a temporary fix for crbug.com/177552.
39 # TODO(newt): remove this once crbug.com/177552 is fixed in ninja.
40 parser.add_option('--ignore', help='this argument is ignored')
41 (options, args) = parser.parse_args()
44 parser.error('No positional arguments should be given.')
46 # Check that required options have been provided.
47 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir',
48 'res_dirs', 'crunch_input_dir', 'crunch_output_dir')
49 build_utils.CheckOptions(options, parser, required=required_options)
54 def MoveImagesToNonMdpiFolders(res_root):
55 """Move images from drawable-*-mdpi-* folders to drawable-* folders.
57 Why? http://crbug.com/289843
59 for src_dir_name in os.listdir(res_root):
60 src_components = src_dir_name.split('-')
61 if src_components[0] != 'drawable' or 'mdpi' not in src_components:
63 src_dir = os.path.join(res_root, src_dir_name)
64 if not os.path.isdir(src_dir):
66 dst_components = [c for c in src_components if c != 'mdpi']
67 assert dst_components != src_components
68 dst_dir_name = '-'.join(dst_components)
69 dst_dir = os.path.join(res_root, dst_dir_name)
70 build_utils.MakeDirectory(dst_dir)
71 for src_file_name in os.listdir(src_dir):
72 if not src_file_name.endswith('.png'):
74 src_file = os.path.join(src_dir, src_file_name)
75 dst_file = os.path.join(dst_dir, src_file_name)
76 assert not os.path.lexists(dst_file)
77 shutil.move(src_file, dst_file)
82 android_jar = os.path.join(options.android_sdk, 'android.jar')
83 aapt = os.path.join(options.android_sdk_tools, 'aapt')
85 build_utils.MakeDirectory(options.R_dir)
87 # Generate R.java. This R.java contains non-final constants and is used only
88 # while compiling the library jar (e.g. chromium_content.jar). When building
89 # an apk, a new R.java file with the correct resource -> ID mappings will be
90 # generated by merging the resources from all libraries and the main apk
92 package_command = [aapt,
95 '-M', options.android_manifest,
98 '--output-text-symbols', options.R_dir,
100 res_dirs = shlex.split(options.res_dirs)
101 for res_dir in res_dirs:
102 package_command += ['-S', res_dir]
103 if options.non_constant_id:
104 package_command.append('--non-constant-id')
105 if options.custom_package:
106 package_command += ['--custom-package', options.custom_package]
107 build_utils.CheckCallDie(package_command)
109 # Crunch image resources. This shrinks png files and is necessary for 9-patch
110 # images to display correctly.
111 build_utils.MakeDirectory(options.crunch_output_dir)
114 '-S', options.crunch_input_dir,
115 '-C', options.crunch_output_dir]
116 build_utils.CheckCallDie(aapt_cmd, suppress_output=True)
118 MoveImagesToNonMdpiFolders(options.crunch_output_dir)
121 build_utils.Touch(options.stamp)
124 if __name__ == '__main__':