Chromium Code Reviews| 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 12 matching lines...) Expand all Loading... | |
| 81 return options, args | 95 return options, args |
| 82 | 96 |
| 83 | 97 |
| 84 def DoProguard(options): | 98 def DoProguard(options): |
| 85 proguard = proguard_util.ProguardCmdBuilder(options.proguard_jar_path) | 99 proguard = proguard_util.ProguardCmdBuilder(options.proguard_jar_path) |
| 86 proguard.outjar(options.obfuscated_jar_path) | 100 proguard.outjar(options.obfuscated_jar_path) |
| 87 | 101 |
| 88 library_classpath = [options.android_sdk_jar] | 102 library_classpath = [options.android_sdk_jar] |
| 89 input_jars = build_utils.ParseGypList(options.input_jars_paths) | 103 input_jars = build_utils.ParseGypList(options.input_jars_paths) |
| 90 | 104 |
| 105 if options.multidex_configuration_path: | |
| 106 with open(options.multidex_configuration_path) as multidex_config_file: | |
| 107 multidex_config = json.loads(multidex_config_file.read()) | |
| 108 input_jars.extend(multidex_config.get('libs', [])) | |
| 109 | |
| 91 exclude_paths = [] | 110 exclude_paths = [] |
| 92 configs = build_utils.ParseGypList(options.proguard_configs) | 111 configs = build_utils.ParseGypList(options.proguard_configs) |
| 93 if options.tested_apk_obfuscated_jar_path: | 112 if options.tested_apk_obfuscated_jar_path: |
| 94 # configs should only contain the process_resources.py generated config. | 113 # configs should only contain the process_resources.py generated config. |
| 95 assert len(configs) == 1, ( | 114 assert len(configs) == 1, ( |
| 96 'test apks should not have custom proguard configs: ' + str(configs)) | 115 'test apks should not have custom proguard configs: ' + str(configs)) |
| 97 tested_jar_info = build_utils.ReadJson( | 116 tested_jar_info = build_utils.ReadJson( |
| 98 options.tested_apk_obfuscated_jar_path + '.info') | 117 options.tested_apk_obfuscated_jar_path + '.info') |
| 99 exclude_paths = tested_jar_info['inputs'] | 118 exclude_paths = tested_jar_info['inputs'] |
| 100 configs = tested_jar_info['configs'] | 119 configs = tested_jar_info['configs'] |
| 101 | 120 |
| 102 proguard.is_test(True) | 121 proguard.is_test(True) |
| 103 proguard.mapping(options.tested_apk_obfuscated_jar_path + '.mapping') | 122 proguard.mapping(options.tested_apk_obfuscated_jar_path + '.mapping') |
| 104 library_classpath.append(options.tested_apk_obfuscated_jar_path) | 123 library_classpath.append(options.tested_apk_obfuscated_jar_path) |
| 105 | 124 |
| 106 proguard.libraryjars(library_classpath) | 125 proguard.libraryjars(library_classpath) |
| 107 proguard_injars = [p for p in input_jars if p not in exclude_paths] | 126 proguard_injars = [p for p in input_jars if p not in exclude_paths] |
| 108 proguard.injars(proguard_injars) | 127 proguard.injars(proguard_injars) |
| 128 | |
| 129 if options.main_dex_list_path: | |
|
Yaron
2015/11/11 02:06:38
nit: extract fn
jbudorick
2015/11/12 20:33:40
Done.
| |
| 130 main_dex_list_config = '' | |
| 131 with open(options.main_dex_list_path) as main_dex_list: | |
| 132 for clazz in (l.strip() for l in main_dex_list): | |
| 133 if clazz.endswith('.class'): | |
| 134 clazz = clazz[:-len('.class')] | |
| 135 clazz = clazz.replace('/', '.') | |
| 136 main_dex_list_config += (_PROGUARD_KEEP_CLASS % clazz) | |
| 137 with tempfile.NamedTemporaryFile( | |
| 138 delete=False, | |
| 139 dir=os.path.dirname(options.main_dex_list_path), | |
| 140 prefix='main_dex_list_proguard', | |
| 141 suffix='.flags') as main_dex_config_file: | |
| 142 main_dex_config_file.write(main_dex_list_config) | |
| 143 configs.append(main_dex_config_file.name) | |
| 144 | |
| 109 proguard.configs(configs) | 145 proguard.configs(configs) |
| 110 | |
| 111 proguard.CheckOutput() | 146 proguard.CheckOutput() |
| 112 | 147 |
| 113 this_info = { | 148 this_info = { |
| 114 'inputs': proguard_injars, | 149 'inputs': proguard_injars, |
| 115 'configs': configs | 150 'configs': configs |
| 116 } | 151 } |
| 117 | 152 |
| 118 build_utils.WriteJson( | 153 build_utils.WriteJson( |
| 119 this_info, options.obfuscated_jar_path + '.info') | 154 this_info, options.obfuscated_jar_path + '.info') |
| 120 | 155 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 145 for f in output_files: | 180 for f in output_files: |
| 146 if os.path.exists(f): | 181 if os.path.exists(f): |
| 147 os.remove(f) | 182 os.remove(f) |
| 148 build_utils.Touch(f) | 183 build_utils.Touch(f) |
| 149 | 184 |
| 150 if options.stamp: | 185 if options.stamp: |
| 151 build_utils.Touch(options.stamp) | 186 build_utils.Touch(options.stamp) |
| 152 | 187 |
| 153 if __name__ == '__main__': | 188 if __name__ == '__main__': |
| 154 sys.exit(main(sys.argv[1:])) | 189 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |