James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 optparse |
| 14 | import os |
| 15 | import sys |
| 16 | |
| 17 | from util import build_utils |
James Robinson | c8f302a | 2015-05-14 16:38:33 -0700 | [diff] [blame] | 18 | from util import proguard_util |
| 19 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | parser.add_option('--input-jars-paths', |
| 31 | help='Path to jars to include in obfuscated jar') |
| 32 | |
| 33 | parser.add_option('--proguard-configs', |
| 34 | help='Paths to proguard config files') |
| 35 | |
| 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.') |
| 40 | |
| 41 | parser.add_option('--obfuscated-jar-path', |
| 42 | help='Output path for obfuscated jar.') |
| 43 | |
| 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.') |
| 51 | |
| 52 | parser.add_option('--stamp', help='File to touch on success') |
| 53 | |
| 54 | (options, args) = parser.parse_args(argv) |
| 55 | |
| 56 | if args: |
| 57 | parser.error('No positional arguments should be given. ' + str(args)) |
| 58 | |
| 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', |
| 68 | ) |
| 69 | |
| 70 | if options.testapp: |
| 71 | required_options += ( |
| 72 | 'test_jar_path', |
| 73 | ) |
| 74 | |
| 75 | build_utils.CheckOptions(options, parser, required=required_options) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 76 | return options, args |
| 77 | |
| 78 | |
James Robinson | c8f302a | 2015-05-14 16:38:33 -0700 | [diff] [blame] | 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: |
| 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'] |
| 96 | |
| 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) |
| 100 | |
| 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) |
| 105 | |
| 106 | proguard.CheckOutput() |
| 107 | |
| 108 | this_info = { |
| 109 | 'inputs': proguard_injars, |
| 110 | 'configs': configs |
| 111 | } |
| 112 | |
| 113 | build_utils.WriteJson( |
| 114 | this_info, options.obfuscated_jar_path + '.info') |
| 115 | |
| 116 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 117 | def main(argv): |
| 118 | options, _ = ParseArgs(argv) |
| 119 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 120 | input_jars = build_utils.ParseGypList(options.input_jars_paths) |
| 121 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 122 | if options.testapp: |
James Robinson | c8f302a | 2015-05-14 16:38:33 -0700 | [diff] [blame] | 123 | dependency_class_filters = [ |
| 124 | '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 125 | build_utils.MergeZips( |
| 126 | options.test_jar_path, input_jars, dependency_class_filters) |
| 127 | |
| 128 | if options.configuration_name == 'Release' and options.proguard_enabled: |
James Robinson | c8f302a | 2015-05-14 16:38:33 -0700 | [diff] [blame] | 129 | DoProguard(options) |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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) |
| 142 | |
| 143 | if options.stamp: |
| 144 | build_utils.Touch(options.stamp) |
| 145 | |
| 146 | if __name__ == '__main__': |
| 147 | sys.exit(main(sys.argv[1:])) |