OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # | |
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 | |
5 # found in the LICENSE file. | |
6 | |
7 """Generates the obfuscated jar and test jar for an apk. | |
8 | |
9 If proguard is not enabled or 'Release' is not in the configuration name, | |
10 obfuscation will be a no-op. | |
11 """ | |
12 | |
13 import fnmatch | |
14 import optparse | |
15 import os | |
16 import sys | |
17 import zipfile | |
18 | |
19 from util import build_utils | |
20 | |
21 def ParseArgs(argv): | |
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('--android-sdk-jar', | |
27 help='path to Android SDK\'s android.jar') | |
28 parser.add_option('--proguard-jar-path', | |
29 help='Path to proguard.jar in the sdk') | |
30 | |
31 parser.add_option('--input-jars-paths', | |
32 help='Path to jars to include in obfuscated jar') | |
33 | |
34 parser.add_option('--proguard-config-files', | |
35 help='Paths to proguard config files') | |
36 | |
37 parser.add_option('--configuration-name', | |
38 help='Gyp configuration name (i.e. Debug, Release)') | |
39 parser.add_option('--proguard-enabled', action='store_true', | |
40 help='Set if proguard is enabled for this target.') | |
41 | |
42 parser.add_option('--obfuscated-jar-path', | |
43 help='Output path for obfuscated jar.') | |
44 | |
45 parser.add_option('--testapp', action='store_true', | |
46 help='Set this if building an instrumentation test apk') | |
47 parser.add_option('--test-jar-path', | |
48 help='Output path for jar containing all the test apk\'s ' | |
49 'code.') | |
50 | |
51 parser.add_option('--stamp', help='File to touch on success') | |
52 | |
53 (options, args) = parser.parse_args(argv) | |
54 | |
55 if args: | |
56 parser.error('No positional arguments should be given. ' + str(args)) | |
57 | |
58 # Check that required options have been provided. | |
59 required_options = ( | |
60 'android_sdk', | |
61 'android_sdk_tools', | |
62 'android_sdk_jar', | |
63 'proguard_jar_path', | |
64 'input_jars_paths', | |
65 'configuration_name', | |
66 'obfuscated_jar_path', | |
67 ) | |
68 build_utils.CheckOptions(options, parser, required=required_options) | |
69 | |
70 return options, args | |
71 | |
72 | |
73 def main(argv): | |
74 options, _ = ParseArgs(argv) | |
75 | |
76 library_classpath = [options.android_sdk_jar] | |
77 javac_custom_classpath = build_utils.ParseGypList(options.input_jars_paths) | |
78 | |
79 dependency_class_filters = [ | |
80 '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] | |
81 | |
82 def DependencyClassFilter(name): | |
83 for name_filter in dependency_class_filters: | |
84 if fnmatch.fnmatch(name, name_filter): | |
85 return False | |
86 return True | |
87 | |
88 if options.testapp: | |
89 with zipfile.ZipFile(options.test_jar_path, 'w') as test_jar: | |
90 for jar in build_utils.ParseGypList(options.input_jars_paths): | |
91 with zipfile.ZipFile(jar, 'r') as jar_zip: | |
92 for name in filter(DependencyClassFilter, jar_zip.namelist()): | |
93 with jar_zip.open(name) as zip_entry: | |
94 test_jar.writestr(name, zip_entry.read()) | |
95 | |
96 if 'Release' in options.configuration_name and options.proguard_enabled: | |
Yaron
2014/06/07 07:02:38
why "in"? isn't configuration_name just a single s
cjhopman
2014/06/10 17:10:16
Done.
| |
97 proguard_project_classpath = javac_custom_classpath | |
98 | |
99 proguard_cmd = [ | |
100 'java', '-jar', options.proguard_jar_path, | |
101 '-forceprocessing', | |
102 '-injars', ':'.join(proguard_project_classpath), | |
103 '-libraryjars', ':'.join(library_classpath), | |
104 '-outjars', options.obfuscated_jar_path | |
105 ] | |
106 | |
107 for proguard_file in build_utils.ParseGypList( | |
108 options.proguard_config_files): | |
109 proguard_cmd += ['-include', proguard_file] | |
110 | |
111 proguard_cmd += ['-dump', options.obfuscated_jar_path + '.dump'] | |
112 proguard_cmd += ['-printseeds', options.obfuscated_jar_path + '.seeds'] | |
Yaron
2014/06/07 07:02:38
nit: can just do commas the whole way, even across
cjhopman
2014/06/10 17:10:16
Done.
| |
113 proguard_cmd += ['-printusage', options.obfuscated_jar_path + '.usage'] | |
114 proguard_cmd += ['-printmapping', | |
115 options.obfuscated_jar_path + '.mapping'] | |
116 | |
117 build_utils.CheckOutput(proguard_cmd) | |
118 else: | |
119 output_files = [ | |
120 options.obfuscated_jar_path, | |
121 options.obfuscated_jar_path + '.dump', | |
122 options.obfuscated_jar_path + '.seeds', | |
123 options.obfuscated_jar_path + '.usage', | |
124 options.obfuscated_jar_path + '.mapping'] | |
125 for f in output_files: | |
126 if os.path.exists(f): | |
127 os.remove(f) | |
Yaron
2014/06/07 07:02:38
Why do you need to remove it if you're going to to
cjhopman
2014/06/10 17:10:16
In this case the expected output is an empty file
| |
128 build_utils.Touch(f) | |
129 | |
130 if options.stamp: | |
131 build_utils.Touch(options.stamp) | |
132 | |
133 if __name__ == '__main__': | |
134 sys.exit(main(sys.argv[1:])) | |
OLD | NEW |