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 optparse | 13 import optparse |
14 import os | 14 import os |
15 import sys | 15 import sys |
16 | 16 |
17 from util import build_utils | 17 from util import build_utils |
18 from util import proguard_util | |
19 | |
18 | 20 |
19 def ParseArgs(argv): | 21 def ParseArgs(argv): |
20 parser = optparse.OptionParser() | 22 parser = optparse.OptionParser() |
21 parser.add_option('--android-sdk', help='path to the Android SDK folder') | 23 parser.add_option('--android-sdk', help='path to the Android SDK folder') |
22 parser.add_option('--android-sdk-tools', | 24 parser.add_option('--android-sdk-tools', |
23 help='path to the Android SDK build tools folder') | 25 help='path to the Android SDK build tools folder') |
24 parser.add_option('--android-sdk-jar', | 26 parser.add_option('--android-sdk-jar', |
25 help='path to Android SDK\'s android.jar') | 27 help='path to Android SDK\'s android.jar') |
26 parser.add_option('--proguard-jar-path', | 28 parser.add_option('--proguard-jar-path', |
27 help='Path to proguard.jar in the sdk') | 29 help='Path to proguard.jar in the sdk') |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 'configuration_name', | 66 'configuration_name', |
65 'obfuscated_jar_path', | 67 'obfuscated_jar_path', |
66 ) | 68 ) |
67 | 69 |
68 if options.testapp: | 70 if options.testapp: |
69 required_options += ( | 71 required_options += ( |
70 'test_jar_path', | 72 'test_jar_path', |
71 ) | 73 ) |
72 | 74 |
73 build_utils.CheckOptions(options, parser, required=required_options) | 75 build_utils.CheckOptions(options, parser, required=required_options) |
76 return options, args | |
74 | 77 |
75 return options, args | 78 |
79 def DoProguard(options): | |
80 proguard = proguard_util.ProguardCmdBuilder(options.proguard_jar_path) | |
81 proguard.outjar(options.obfuscated_jar_path) | |
82 | |
83 library_classpath = [options.android_sdk_jar] | |
84 input_jars = build_utils.ParseGypList(options.input_jars_paths) | |
85 | |
86 exclude_paths = [] | |
87 configs = build_utils.ParseGypList(options.proguard_configs) | |
88 if (options.tested_apk_obfuscated_jar_path and | |
89 options.tested_apk_obfuscated_jar_path != '/'): | |
newt (away)
2015/04/28 04:22:14
why would the path be "/"?
cjhopman
2015/05/02 00:41:44
Done.
| |
90 # configs should only contain the process_resources.py generated config. | |
91 assert len(configs) == 1, ( | |
92 'test apks should not have custom proguard configs: ' + str(configs)) | |
93 tested_jar_info = build_utils.ReadJson( | |
94 options.tested_apk_obfuscated_jar_path + '.info') | |
95 exclude_paths = tested_jar_info['inputs'] | |
96 configs = tested_jar_info['configs'] | |
97 | |
98 proguard.testoptions(True) | |
newt (away)
2015/04/28 04:22:14
testoptions() sounds like it's going to take a lis
cjhopman
2015/05/02 00:41:44
Done.
| |
99 proguard.mapping(options.tested_apk_obfuscated_jar_path + '.mapping') | |
100 library_classpath.append(options.tested_apk_obfuscated_jar_path) | |
101 | |
102 proguard.libraryjars(library_classpath) | |
103 proguard_injars = [p for p in input_jars if p not in exclude_paths] | |
104 proguard.injars(proguard_injars) | |
105 proguard.configs(configs) | |
106 | |
107 proguard.CheckOutput() | |
108 | |
109 this_info = { | |
110 'inputs': proguard_injars, | |
111 'configs': configs | |
112 } | |
113 | |
114 build_utils.WriteJson( | |
115 this_info, options.obfuscated_jar_path + '.info') | |
76 | 116 |
77 | 117 |
78 def main(argv): | 118 def main(argv): |
79 options, _ = ParseArgs(argv) | 119 options, _ = ParseArgs(argv) |
80 | 120 |
81 library_classpath = [options.android_sdk_jar] | |
82 input_jars = build_utils.ParseGypList(options.input_jars_paths) | 121 input_jars = build_utils.ParseGypList(options.input_jars_paths) |
83 | 122 |
84 dependency_class_filters = [ | |
85 '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] | |
86 | |
87 if options.testapp: | 123 if options.testapp: |
124 dependency_class_filters = [ | |
125 '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] | |
88 build_utils.MergeZips( | 126 build_utils.MergeZips( |
89 options.test_jar_path, input_jars, dependency_class_filters) | 127 options.test_jar_path, input_jars, dependency_class_filters) |
90 | 128 |
91 if options.configuration_name == 'Release' and options.proguard_enabled: | 129 if options.configuration_name == 'Release' and options.proguard_enabled: |
92 proguard_cmd = [ | 130 DoProguard(options) |
93 'java', '-jar', options.proguard_jar_path, | |
94 '-forceprocessing', | |
95 '-libraryjars', ':'.join(library_classpath), | |
96 '-dump', options.obfuscated_jar_path + '.dump', | |
97 '-printseeds', options.obfuscated_jar_path + '.seeds', | |
98 '-printusage', options.obfuscated_jar_path + '.usage', | |
99 '-printmapping', options.obfuscated_jar_path + '.mapping', | |
100 ] | |
101 | |
102 exclude_paths = [] | |
103 configs = build_utils.ParseGypList(options.proguard_configs) | |
104 if (options.tested_apk_obfuscated_jar_path and | |
105 options.tested_apk_obfuscated_jar_path != '/'): | |
106 # configs should only contain the process_resources.py generated config. | |
107 assert len(configs) == 1, ( | |
108 'test apks should not have custom proguard configs: ' + str(configs)) | |
109 tested_jar_info = build_utils.ReadJson( | |
110 options.tested_apk_obfuscated_jar_path + '.info') | |
111 exclude_paths = tested_jar_info['inputs'] | |
112 configs = tested_jar_info['configs'] | |
113 proguard_cmd += [ | |
114 '-dontobfuscate', | |
115 '-dontoptimize', | |
116 '-dontshrink', | |
117 '-dontskipnonpubliclibraryclassmembers', | |
118 '-libraryjars', options.tested_apk_obfuscated_jar_path, | |
119 '-applymapping', options.tested_apk_obfuscated_jar_path + '.mapping', | |
120 ] | |
121 | |
122 proguard_injars = [p for p in input_jars if p not in exclude_paths] | |
123 proguard_cmd += ['-injars', ':'.join(proguard_injars)] | |
124 | |
125 for config_file in configs: | |
126 proguard_cmd += ['-include', config_file] | |
127 | |
128 # The output jar must be specified after inputs. | |
129 proguard_cmd += ['-outjars', options.obfuscated_jar_path] | |
130 | |
131 build_utils.CheckOutput(proguard_cmd) | |
132 | |
133 this_info = { | |
134 'inputs': proguard_injars, | |
135 'configs': configs | |
136 } | |
137 | |
138 build_utils.WriteJson( | |
139 this_info, options.obfuscated_jar_path + '.info') | |
140 else: | 131 else: |
141 output_files = [ | 132 output_files = [ |
142 options.obfuscated_jar_path, | 133 options.obfuscated_jar_path, |
143 options.obfuscated_jar_path + '.info', | 134 options.obfuscated_jar_path + '.info', |
144 options.obfuscated_jar_path + '.dump', | 135 options.obfuscated_jar_path + '.dump', |
145 options.obfuscated_jar_path + '.seeds', | 136 options.obfuscated_jar_path + '.seeds', |
146 options.obfuscated_jar_path + '.usage', | 137 options.obfuscated_jar_path + '.usage', |
147 options.obfuscated_jar_path + '.mapping'] | 138 options.obfuscated_jar_path + '.mapping'] |
148 for f in output_files: | 139 for f in output_files: |
149 if os.path.exists(f): | 140 if os.path.exists(f): |
150 os.remove(f) | 141 os.remove(f) |
151 build_utils.Touch(f) | 142 build_utils.Touch(f) |
152 | 143 |
153 if options.stamp: | 144 if options.stamp: |
154 build_utils.Touch(options.stamp) | 145 build_utils.Touch(options.stamp) |
155 | 146 |
156 if __name__ == '__main__': | 147 if __name__ == '__main__': |
157 sys.exit(main(sys.argv[1:])) | 148 sys.exit(main(sys.argv[1:])) |
OLD | NEW |