OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 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 """Generates the obfuscated jar and test jar for an apk. | 7 """Generates the obfuscated jar and test jar for an apk. |
8 | 8 |
9 If proguard is not enabled or 'Release' is not in the configuration name, | 9 If proguard is not enabled or 'Release' is not in the configuration name, |
10 obfuscation will be a no-op. | 10 obfuscation will be a no-op. |
11 """ | 11 """ |
12 | 12 |
| 13 import json |
13 import optparse | 14 import optparse |
14 import os | 15 import os |
15 import sys | 16 import sys |
| 17 import tempfile |
16 | 18 |
17 from util import build_utils | 19 from util import build_utils |
18 from util import proguard_util | 20 from util import proguard_util |
19 | 21 |
20 | 22 |
| 23 _PROGUARD_KEEP_CLASS = '''-keep class %s { |
| 24 *; |
| 25 } |
| 26 ''' |
| 27 |
| 28 |
21 def ParseArgs(argv): | 29 def ParseArgs(argv): |
22 parser = optparse.OptionParser() | 30 parser = optparse.OptionParser() |
23 parser.add_option('--android-sdk', help='path to the Android SDK folder') | 31 parser.add_option('--android-sdk', help='path to the Android SDK folder') |
24 parser.add_option('--android-sdk-tools', | 32 parser.add_option('--android-sdk-tools', |
25 help='path to the Android SDK build tools folder') | 33 help='path to the Android SDK build tools folder') |
26 parser.add_option('--android-sdk-jar', | 34 parser.add_option('--android-sdk-jar', |
27 help='path to Android SDK\'s android.jar') | 35 help='path to Android SDK\'s android.jar') |
28 parser.add_option('--proguard-jar-path', | 36 parser.add_option('--proguard-jar-path', |
29 help='Path to proguard.jar in the sdk') | 37 help='Path to proguard.jar in the sdk') |
30 parser.add_option('--input-jars-paths', | 38 parser.add_option('--input-jars-paths', |
(...skipping 18 matching lines...) Expand all Loading... |
49 parser.add_option('--testapp', action='store_true', | 57 parser.add_option('--testapp', action='store_true', |
50 help='Set this if building an instrumentation test apk') | 58 help='Set this if building an instrumentation test apk') |
51 parser.add_option('--tested-apk-obfuscated-jar-path', | 59 parser.add_option('--tested-apk-obfuscated-jar-path', |
52 help='Path to obfusctated jar of the tested apk') | 60 help='Path to obfusctated jar of the tested apk') |
53 parser.add_option('--test-jar-path', | 61 parser.add_option('--test-jar-path', |
54 help='Output path for jar containing all the test apk\'s ' | 62 help='Output path for jar containing all the test apk\'s ' |
55 'code.') | 63 'code.') |
56 | 64 |
57 parser.add_option('--stamp', help='File to touch on success') | 65 parser.add_option('--stamp', help='File to touch on success') |
58 | 66 |
| 67 parser.add_option('--main-dex-list-path', |
| 68 help='The list of classes to retain in the main dex. ' |
| 69 'These will not be obfuscated.') |
| 70 parser.add_option('--multidex-configuration-path', |
| 71 help='A JSON file containing multidex build configuration.') |
| 72 |
59 (options, args) = parser.parse_args(argv) | 73 (options, args) = parser.parse_args(argv) |
60 | 74 |
61 if args: | 75 if args: |
62 parser.error('No positional arguments should be given. ' + str(args)) | 76 parser.error('No positional arguments should be given. ' + str(args)) |
63 | 77 |
64 # Check that required options have been provided. | 78 # Check that required options have been provided. |
65 required_options = ( | 79 required_options = ( |
66 'android_sdk', | 80 'android_sdk', |
67 'android_sdk_tools', | 81 'android_sdk_tools', |
68 'android_sdk_jar', | 82 'android_sdk_jar', |
(...skipping 30 matching lines...) Expand all Loading... |
99 exclude_paths = tested_jar_info['inputs'] | 113 exclude_paths = tested_jar_info['inputs'] |
100 configs = tested_jar_info['configs'] | 114 configs = tested_jar_info['configs'] |
101 | 115 |
102 proguard.is_test(True) | 116 proguard.is_test(True) |
103 proguard.mapping(options.tested_apk_obfuscated_jar_path + '.mapping') | 117 proguard.mapping(options.tested_apk_obfuscated_jar_path + '.mapping') |
104 library_classpath.append(options.tested_apk_obfuscated_jar_path) | 118 library_classpath.append(options.tested_apk_obfuscated_jar_path) |
105 | 119 |
106 proguard.libraryjars(library_classpath) | 120 proguard.libraryjars(library_classpath) |
107 proguard_injars = [p for p in input_jars if p not in exclude_paths] | 121 proguard_injars = [p for p in input_jars if p not in exclude_paths] |
108 proguard.injars(proguard_injars) | 122 proguard.injars(proguard_injars) |
| 123 |
| 124 multidex_config = _PossibleMultidexConfig(options) |
| 125 if multidex_config: |
| 126 configs.append(multidex_config) |
| 127 |
109 proguard.configs(configs) | 128 proguard.configs(configs) |
110 | |
111 proguard.CheckOutput() | 129 proguard.CheckOutput() |
112 | 130 |
113 this_info = { | 131 this_info = { |
114 'inputs': proguard_injars, | 132 'inputs': proguard_injars, |
115 'configs': configs | 133 'configs': configs |
116 } | 134 } |
117 | 135 |
118 build_utils.WriteJson( | 136 build_utils.WriteJson( |
119 this_info, options.obfuscated_jar_path + '.info') | 137 this_info, options.obfuscated_jar_path + '.info') |
120 | 138 |
121 | 139 |
| 140 def _PossibleMultidexConfig(options): |
| 141 if not options.multidex_configuration_path: |
| 142 return None |
| 143 |
| 144 with open(options.multidex_configuration_path) as multidex_config_file: |
| 145 multidex_config = json.loads(multidex_config_file.read()) |
| 146 |
| 147 if not (multidex_config.get('enabled') and options.main_dex_list_path): |
| 148 return None |
| 149 |
| 150 main_dex_list_config = '' |
| 151 with open(options.main_dex_list_path) as main_dex_list: |
| 152 for clazz in (l.strip() for l in main_dex_list): |
| 153 if clazz.endswith('.class'): |
| 154 clazz = clazz[:-len('.class')] |
| 155 clazz = clazz.replace('/', '.') |
| 156 main_dex_list_config += (_PROGUARD_KEEP_CLASS % clazz) |
| 157 with tempfile.NamedTemporaryFile( |
| 158 delete=False, |
| 159 dir=os.path.dirname(options.main_dex_list_path), |
| 160 prefix='main_dex_list_proguard', |
| 161 suffix='.flags') as main_dex_config_file: |
| 162 main_dex_config_file.write(main_dex_list_config) |
| 163 return main_dex_config_file.name |
| 164 |
| 165 |
122 def main(argv): | 166 def main(argv): |
123 options, _ = ParseArgs(argv) | 167 options, _ = ParseArgs(argv) |
124 | 168 |
125 input_jars = build_utils.ParseGypList(options.input_jars_paths) | 169 input_jars = build_utils.ParseGypList(options.input_jars_paths) |
126 | 170 |
127 if options.testapp: | 171 if options.testapp: |
128 dependency_class_filters = [ | 172 dependency_class_filters = [ |
129 '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] | 173 '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] |
130 build_utils.MergeZips( | 174 build_utils.MergeZips( |
131 options.test_jar_path, input_jars, dependency_class_filters) | 175 options.test_jar_path, input_jars, dependency_class_filters) |
(...skipping 13 matching lines...) Expand all Loading... |
145 for f in output_files: | 189 for f in output_files: |
146 if os.path.exists(f): | 190 if os.path.exists(f): |
147 os.remove(f) | 191 os.remove(f) |
148 build_utils.Touch(f) | 192 build_utils.Touch(f) |
149 | 193 |
150 if options.stamp: | 194 if options.stamp: |
151 build_utils.Touch(options.stamp) | 195 build_utils.Touch(options.stamp) |
152 | 196 |
153 if __name__ == '__main__': | 197 if __name__ == '__main__': |
154 sys.exit(main(sys.argv[1:])) | 198 sys.exit(main(sys.argv[1:])) |
OLD | NEW |