Add Apps.AppListSearchQueryLength UMA histogram.
[chromium-blink-merge.git] / build / android / gyp / apk_obfuscate.py
blobb075758800735973967c04af107aa91c6e62dc7c
1 #!/usr/bin/env python
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.
7 """Generates the obfuscated jar and test jar for an apk.
9 If proguard is not enabled or 'Release' is not in the configuration name,
10 obfuscation will be a no-op.
11 """
13 import optparse
14 import os
15 import sys
17 from util import build_utils
18 from util import proguard_util
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 parser.add_option('--input-jars-paths',
31 help='Path to jars to include in obfuscated jar')
33 parser.add_option('--proguard-configs',
34 help='Paths to proguard config files')
36 parser.add_option('--configuration-name',
37 help='Gyp configuration name (i.e. Debug, Release)')
38 parser.add_option('--proguard-enabled', action='store_true',
39 help='Set if proguard is enabled for this target.')
41 parser.add_option('--obfuscated-jar-path',
42 help='Output path for obfuscated jar.')
44 parser.add_option('--testapp', action='store_true',
45 help='Set this if building an instrumentation test apk')
46 parser.add_option('--tested-apk-obfuscated-jar-path',
47 help='Path to obfusctated jar of the tested apk')
48 parser.add_option('--test-jar-path',
49 help='Output path for jar containing all the test apk\'s '
50 'code.')
52 parser.add_option('--stamp', help='File to touch on success')
54 (options, args) = parser.parse_args(argv)
56 if args:
57 parser.error('No positional arguments should be given. ' + str(args))
59 # Check that required options have been provided.
60 required_options = (
61 'android_sdk',
62 'android_sdk_tools',
63 'android_sdk_jar',
64 'proguard_jar_path',
65 'input_jars_paths',
66 'configuration_name',
67 'obfuscated_jar_path',
70 if options.testapp:
71 required_options += (
72 'test_jar_path',
75 build_utils.CheckOptions(options, parser, required=required_options)
76 return options, args
79 def DoProguard(options):
80 proguard = proguard_util.ProguardCmdBuilder(options.proguard_jar_path)
81 proguard.outjar(options.obfuscated_jar_path)
83 library_classpath = [options.android_sdk_jar]
84 input_jars = build_utils.ParseGypList(options.input_jars_paths)
86 exclude_paths = []
87 configs = build_utils.ParseGypList(options.proguard_configs)
88 if options.tested_apk_obfuscated_jar_path:
89 # configs should only contain the process_resources.py generated config.
90 assert len(configs) == 1, (
91 'test apks should not have custom proguard configs: ' + str(configs))
92 tested_jar_info = build_utils.ReadJson(
93 options.tested_apk_obfuscated_jar_path + '.info')
94 exclude_paths = tested_jar_info['inputs']
95 configs = tested_jar_info['configs']
97 proguard.is_test(True)
98 proguard.mapping(options.tested_apk_obfuscated_jar_path + '.mapping')
99 library_classpath.append(options.tested_apk_obfuscated_jar_path)
101 proguard.libraryjars(library_classpath)
102 proguard_injars = [p for p in input_jars if p not in exclude_paths]
103 proguard.injars(proguard_injars)
104 proguard.configs(configs)
106 proguard.CheckOutput()
108 this_info = {
109 'inputs': proguard_injars,
110 'configs': configs
113 build_utils.WriteJson(
114 this_info, options.obfuscated_jar_path + '.info')
117 def main(argv):
118 options, _ = ParseArgs(argv)
120 input_jars = build_utils.ParseGypList(options.input_jars_paths)
122 if options.testapp:
123 dependency_class_filters = [
124 '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class']
125 build_utils.MergeZips(
126 options.test_jar_path, input_jars, dependency_class_filters)
128 if options.configuration_name == 'Release' and options.proguard_enabled:
129 DoProguard(options)
130 else:
131 output_files = [
132 options.obfuscated_jar_path,
133 options.obfuscated_jar_path + '.info',
134 options.obfuscated_jar_path + '.dump',
135 options.obfuscated_jar_path + '.seeds',
136 options.obfuscated_jar_path + '.usage',
137 options.obfuscated_jar_path + '.mapping']
138 for f in output_files:
139 if os.path.exists(f):
140 os.remove(f)
141 build_utils.Touch(f)
143 if options.stamp:
144 build_utils.Touch(options.stamp)
146 if __name__ == '__main__':
147 sys.exit(main(sys.argv[1:]))