OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 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 import itertools |
7 import optparse | 8 import optparse |
8 import os | 9 import os |
9 import sys | 10 import sys |
| 11 import tempfile |
| 12 import zipfile |
10 | 13 |
11 from util import build_utils | 14 from util import build_utils |
12 from util import md5_check | 15 from util import md5_check |
13 | 16 |
14 | 17 |
| 18 def _GenerateMainDexListFile(main_dex_list_file, paths, options): |
| 19 proguard_binary = os.path.abspath( |
| 20 os.path.join(options.android_sdk_tools, os.pardir, os.pardir, 'tools', |
| 21 'proguard', 'bin', 'proguard.sh')) |
| 22 shrinked_android_jar = os.path.join( |
| 23 options.android_sdk_tools, 'lib', 'shrinkedAndroid.jar') |
| 24 dx_jar = os.path.join( |
| 25 options.android_sdk_tools, 'lib', 'dx.jar') |
| 26 paths_arg = ':'.join(paths) |
| 27 rules_file = os.path.abspath(os.path.join( |
| 28 options.android_sdk_tools, 'mainDexClasses.rules')) |
| 29 |
| 30 with tempfile.NamedTemporaryFile(suffix='.jar') as temp_jar: |
| 31 proguard_cmd = [ |
| 32 proguard_binary, |
| 33 '-forceprocessing', |
| 34 '-dontwarn', '-dontoptimize', '-dontobfuscate', '-dontpreverify', |
| 35 '-injars', paths_arg, |
| 36 '-outjars', temp_jar.name, |
| 37 '-libraryjars', shrinked_android_jar, |
| 38 '-include', rules_file, |
| 39 ] |
| 40 proguard_cmd += list(itertools.chain( |
| 41 ['-include', m] for m in options.main_dex_rules_files)) |
| 42 build_utils.CheckOutput(proguard_cmd) |
| 43 |
| 44 java_cmd = ['java', '-cp', dx_jar, |
| 45 'com.android.multidex.MainDexListBuilder', |
| 46 temp_jar.name, paths_arg] |
| 47 main_dex_list = build_utils.CheckOutput(java_cmd) |
| 48 main_dex_list_file.write(main_dex_list) |
| 49 main_dex_list_file.flush() |
| 50 |
| 51 |
15 def DoDex(options, paths): | 52 def DoDex(options, paths): |
16 dx_binary = os.path.join(options.android_sdk_tools, 'dx') | 53 dx_binary = os.path.join(options.android_sdk_tools, 'dx') |
17 # See http://crbug.com/272064 for context on --force-jumbo. | 54 # See http://crbug.com/272064 for context on --force-jumbo. |
18 dex_cmd = [dx_binary, '--dex', '--force-jumbo', '--output', options.dex_path] | 55 dex_cmd = [dx_binary, '--dex', '--force-jumbo'] |
19 if options.no_locals != '0': | 56 if options.no_locals != '0': |
20 dex_cmd.append('--no-locals') | 57 dex_cmd.append('--no-locals') |
| 58 with tempfile.NamedTemporaryFile() as main_dex_list_file: |
| 59 if options.multi_dex: |
| 60 _GenerateMainDexListFile(main_dex_list_file, paths, options) |
21 | 61 |
22 dex_cmd += paths | 62 dex_cmd.append('--multi-dex') |
| 63 dex_cmd.append('--minimal-main-dex') |
| 64 dex_cmd.append('--main-dex-list=%s' % main_dex_list_file.name) |
23 | 65 |
24 record_path = '%s.md5.stamp' % options.dex_path | 66 dex_cmd += ['--output', options.dex_path] |
25 md5_check.CallAndRecordIfStale( | 67 dex_cmd += paths |
26 lambda: build_utils.CheckOutput(dex_cmd, print_stderr=False), | 68 |
27 record_path=record_path, | 69 record_path = '%s.md5.stamp' % options.dex_path |
28 input_paths=paths, | 70 md5_check.CallAndRecordIfStale( |
29 input_strings=dex_cmd, | 71 lambda: build_utils.CheckOutput(dex_cmd, print_stderr=False), |
30 force=not os.path.exists(options.dex_path)) | 72 record_path=record_path, |
31 build_utils.WriteJson( | 73 input_paths=paths, |
32 [os.path.relpath(p, options.output_directory) for p in paths], | 74 input_strings=dex_cmd, |
33 options.dex_path + '.inputs') | 75 force=not os.path.exists(options.dex_path)) |
| 76 build_utils.WriteJson( |
| 77 [os.path.relpath(p, options.output_directory) for p in paths], |
| 78 options.dex_path + '.inputs') |
| 79 |
| 80 if options.multi_dex and options.dex_path.endswith('.zip'): |
| 81 iz = zipfile.ZipFile(options.dex_path, 'r') |
| 82 tmp_dex_path = '%s.tmp.zip' % options.dex_path |
| 83 oz = zipfile.ZipFile(tmp_dex_path, 'w', zipfile.ZIP_DEFLATED) |
| 84 for i in iz.namelist(): |
| 85 if i.endswith('.dex'): |
| 86 oz.writestr(i, iz.read(i)) |
| 87 os.remove(options.dex_path) |
| 88 os.rename(tmp_dex_path, options.dex_path) |
34 | 89 |
35 | 90 |
36 def main(): | 91 def main(): |
37 args = build_utils.ExpandFileArgs(sys.argv[1:]) | 92 args = build_utils.ExpandFileArgs(sys.argv[1:]) |
38 | 93 |
39 parser = optparse.OptionParser() | 94 parser = optparse.OptionParser() |
40 build_utils.AddDepfileOption(parser) | 95 build_utils.AddDepfileOption(parser) |
41 | 96 |
42 parser.add_option('--android-sdk-tools', | 97 parser.add_option('--android-sdk-tools', |
43 help='Android sdk build tools directory.') | 98 help='Android sdk build tools directory.') |
44 parser.add_option('--output-directory', | 99 parser.add_option('--output-directory', |
45 default=os.getcwd(), | 100 default=os.getcwd(), |
46 help='Path to the output build directory.') | 101 help='Path to the output build directory.') |
47 parser.add_option('--dex-path', help='Dex output path.') | 102 parser.add_option('--dex-path', help='Dex output path.') |
48 parser.add_option('--configuration-name', | 103 parser.add_option('--configuration-name', |
49 help='The build CONFIGURATION_NAME.') | 104 help='The build CONFIGURATION_NAME.') |
50 parser.add_option('--proguard-enabled', | 105 parser.add_option('--proguard-enabled', |
51 help='"true" if proguard is enabled.') | 106 help='"true" if proguard is enabled.') |
52 parser.add_option('--proguard-enabled-input-path', | 107 parser.add_option('--proguard-enabled-input-path', |
53 help=('Path to dex in Release mode when proguard ' | 108 help=('Path to dex in Release mode when proguard ' |
54 'is enabled.')) | 109 'is enabled.')) |
55 parser.add_option('--no-locals', | 110 parser.add_option('--no-locals', |
56 help='Exclude locals list from the dex file.') | 111 help='Exclude locals list from the dex file.') |
| 112 parser.add_option('--multi-dex', default=False, action='store_true', |
| 113 help='Create multiple dex files.') |
57 parser.add_option('--inputs', help='A list of additional input paths.') | 114 parser.add_option('--inputs', help='A list of additional input paths.') |
58 parser.add_option('--excluded-paths', | 115 parser.add_option('--excluded-paths', |
59 help='A list of paths to exclude from the dex file.') | 116 help='A list of paths to exclude from the dex file.') |
| 117 parser.add_option('--main-dex-rules-file', |
| 118 action='append', |
| 119 default=[], |
| 120 dest='main_dex_rules_files', |
| 121 help='A file containing a list of proguard rules to use ' |
| 122 'in determining the classes to include in the ' |
| 123 'main dex.') |
60 | 124 |
61 options, paths = parser.parse_args(args) | 125 options, paths = parser.parse_args(args) |
62 | 126 |
63 required_options = ('android_sdk_tools',) | 127 required_options = ('android_sdk_tools',) |
64 build_utils.CheckOptions(options, parser, required=required_options) | 128 build_utils.CheckOptions(options, parser, required=required_options) |
65 | 129 |
66 if (options.proguard_enabled == 'true' | 130 if (options.proguard_enabled == 'true' |
67 and options.configuration_name == 'Release'): | 131 and options.configuration_name == 'Release'): |
68 paths = [options.proguard_enabled_input_path] | 132 paths = [options.proguard_enabled_input_path] |
69 | 133 |
(...skipping 10 matching lines...) Expand all Loading... |
80 | 144 |
81 if options.depfile: | 145 if options.depfile: |
82 build_utils.WriteDepfile( | 146 build_utils.WriteDepfile( |
83 options.depfile, | 147 options.depfile, |
84 paths + build_utils.GetPythonDependencies()) | 148 paths + build_utils.GetPythonDependencies()) |
85 | 149 |
86 | 150 |
87 | 151 |
88 if __name__ == '__main__': | 152 if __name__ == '__main__': |
89 sys.exit(main()) | 153 sys.exit(main()) |
OLD | NEW |