| 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 fnmatch | 13 import fnmatch |
| 14 import optparse | 14 import optparse |
| 15 import os | 15 import os |
| 16 import sys | 16 import sys |
| 17 import zipfile | 17 import zipfile |
| 18 | 18 |
| 19 from util import build_utils | 19 from util import build_utils |
| 20 | 20 |
| 21 def ParseArgs(argv): | 21 def ParseArgs(argv): |
| 22 parser = optparse.OptionParser() | 22 parser = optparse.OptionParser() |
| 23 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') |
| 24 parser.add_option('--android-sdk-tools', | 24 parser.add_option('--android-sdk-tools', |
| 25 help='path to the Android SDK build tools folder') | 25 help='path to the Android SDK build tools folder') |
| 26 parser.add_option('--android-sdk-jar', | 26 parser.add_option('--android-sdk-jar', |
| 27 help='path to Android SDK\'s android.jar') | 27 help='path to Android SDK\'s android.jar') |
| 28 parser.add_option('--proguard-jar-path', | 28 parser.add_option('--proguard-jar-path', |
| 29 help='Path to proguard.jar in the sdk') | 29 help='Path to proguard.jar in the sdk') |
| 30 | |
| 31 parser.add_option('--input-jars-paths', | 30 parser.add_option('--input-jars-paths', |
| 32 help='Path to jars to include in obfuscated jar') | 31 help='Path to jars to include in obfuscated jar') |
| 33 | 32 |
| 34 parser.add_option('--proguard-config-files', | 33 parser.add_option('--proguard-configs', |
| 35 help='Paths to proguard config files') | 34 help='Paths to proguard config files') |
| 36 | 35 |
| 37 parser.add_option('--configuration-name', | 36 parser.add_option('--configuration-name', |
| 38 help='Gyp configuration name (i.e. Debug, Release)') | 37 help='Gyp configuration name (i.e. Debug, Release)') |
| 39 parser.add_option('--proguard-enabled', action='store_true', | 38 parser.add_option('--proguard-enabled', action='store_true', |
| 40 help='Set if proguard is enabled for this target.') | 39 help='Set if proguard is enabled for this target.') |
| 41 | 40 |
| 42 parser.add_option('--obfuscated-jar-path', | 41 parser.add_option('--obfuscated-jar-path', |
| 43 help='Output path for obfuscated jar.') | 42 help='Output path for obfuscated jar.') |
| 44 | 43 |
| 45 parser.add_option('--testapp', action='store_true', | 44 parser.add_option('--testapp', action='store_true', |
| 46 help='Set this if building an instrumentation test apk') | 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') |
| 47 parser.add_option('--test-jar-path', | 48 parser.add_option('--test-jar-path', |
| 48 help='Output path for jar containing all the test apk\'s ' | 49 help='Output path for jar containing all the test apk\'s ' |
| 49 'code.') | 50 'code.') |
| 50 | 51 |
| 51 parser.add_option('--stamp', help='File to touch on success') | 52 parser.add_option('--stamp', help='File to touch on success') |
| 52 | 53 |
| 53 (options, args) = parser.parse_args(argv) | 54 (options, args) = parser.parse_args(argv) |
| 54 | 55 |
| 55 if args: | 56 if args: |
| 56 parser.error('No positional arguments should be given. ' + str(args)) | 57 parser.error('No positional arguments should be given. ' + str(args)) |
| 57 | 58 |
| 58 # Check that required options have been provided. | 59 # Check that required options have been provided. |
| 59 required_options = ( | 60 required_options = ( |
| 60 'android_sdk', | 61 'android_sdk', |
| 61 'android_sdk_tools', | 62 'android_sdk_tools', |
| 62 'android_sdk_jar', | 63 'android_sdk_jar', |
| 63 'proguard_jar_path', | 64 'proguard_jar_path', |
| 64 'input_jars_paths', | 65 'input_jars_paths', |
| 65 'configuration_name', | 66 'configuration_name', |
| 66 'obfuscated_jar_path', | 67 'obfuscated_jar_path', |
| 67 ) | 68 ) |
| 69 |
| 70 if options.testapp: |
| 71 required_options += ( |
| 72 'test_jar_path', |
| 73 ) |
| 74 |
| 68 build_utils.CheckOptions(options, parser, required=required_options) | 75 build_utils.CheckOptions(options, parser, required=required_options) |
| 69 | 76 |
| 70 return options, args | 77 return options, args |
| 71 | 78 |
| 72 | 79 |
| 73 def main(argv): | 80 def main(argv): |
| 74 options, _ = ParseArgs(argv) | 81 options, _ = ParseArgs(argv) |
| 75 | 82 |
| 76 library_classpath = [options.android_sdk_jar] | 83 library_classpath = [options.android_sdk_jar] |
| 77 javac_custom_classpath = build_utils.ParseGypList(options.input_jars_paths) | 84 input_jars = build_utils.ParseGypList(options.input_jars_paths) |
| 78 | 85 |
| 79 dependency_class_filters = [ | 86 dependency_class_filters = [ |
| 80 '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] | 87 '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] |
| 81 | 88 |
| 82 def DependencyClassFilter(name): | 89 def DependencyClassFilter(name): |
| 83 for name_filter in dependency_class_filters: | 90 for name_filter in dependency_class_filters: |
| 84 if fnmatch.fnmatch(name, name_filter): | 91 if fnmatch.fnmatch(name, name_filter): |
| 85 return False | 92 return False |
| 86 return True | 93 return True |
| 87 | 94 |
| 88 if options.testapp: | 95 if options.testapp: |
| 89 with zipfile.ZipFile(options.test_jar_path, 'w') as test_jar: | 96 with zipfile.ZipFile(options.test_jar_path, 'w') as test_jar: |
| 90 for jar in build_utils.ParseGypList(options.input_jars_paths): | 97 for jar in input_jars: |
| 91 with zipfile.ZipFile(jar, 'r') as jar_zip: | 98 with zipfile.ZipFile(jar, 'r') as jar_zip: |
| 92 for name in filter(DependencyClassFilter, jar_zip.namelist()): | 99 for name in filter(DependencyClassFilter, jar_zip.namelist()): |
| 93 with jar_zip.open(name) as zip_entry: | 100 with jar_zip.open(name) as zip_entry: |
| 94 test_jar.writestr(name, zip_entry.read()) | 101 test_jar.writestr(name, zip_entry.read()) |
| 95 | 102 |
| 96 if options.configuration_name == 'Release' and options.proguard_enabled: | 103 if options.configuration_name == 'Release' and options.proguard_enabled: |
| 97 proguard_project_classpath = javac_custom_classpath | |
| 98 | |
| 99 proguard_cmd = [ | 104 proguard_cmd = [ |
| 100 'java', '-jar', options.proguard_jar_path, | 105 'java', '-jar', options.proguard_jar_path, |
| 101 '-forceprocessing', | 106 '-forceprocessing', |
| 102 '-injars', ':'.join(proguard_project_classpath), | |
| 103 '-libraryjars', ':'.join(library_classpath), | 107 '-libraryjars', ':'.join(library_classpath), |
| 104 '-outjars', options.obfuscated_jar_path, | |
| 105 '-dump', options.obfuscated_jar_path + '.dump', | 108 '-dump', options.obfuscated_jar_path + '.dump', |
| 106 '-printseeds', options.obfuscated_jar_path + '.seeds', | 109 '-printseeds', options.obfuscated_jar_path + '.seeds', |
| 107 '-printusage', options.obfuscated_jar_path + '.usage', | 110 '-printusage', options.obfuscated_jar_path + '.usage', |
| 108 '-printmapping', options.obfuscated_jar_path + '.mapping', | 111 '-printmapping', options.obfuscated_jar_path + '.mapping', |
| 109 ] | 112 ] |
| 110 | 113 |
| 111 for proguard_file in build_utils.ParseGypList( | 114 exclude_paths = [] |
| 112 options.proguard_config_files): | 115 configs = build_utils.ParseGypList(options.proguard_configs) |
| 113 proguard_cmd += ['-include', proguard_file] | 116 if (options.tested_apk_obfuscated_jar_path and |
| 117 options.tested_apk_obfuscated_jar_path != '/'): |
| 118 # configs should only contain the process_resources.py generated config. |
| 119 assert len(configs) == 1, ( |
| 120 'test apks should not have custom proguard configs: ' + str(configs)) |
| 121 tested_jar_info = build_utils.ReadJson( |
| 122 options.tested_apk_obfuscated_jar_path + '.info') |
| 123 exclude_paths = tested_jar_info['inputs'] |
| 124 configs = tested_jar_info['configs'] |
| 125 proguard_cmd += [ |
| 126 '-dontobfuscate', |
| 127 '-dontoptimize', |
| 128 '-dontskipnonpubliclibraryclassmembers', |
| 129 '-libraryjars', options.tested_apk_obfuscated_jar_path, |
| 130 '-applymapping', options.tested_apk_obfuscated_jar_path + '.mapping', |
| 131 ] |
| 132 |
| 133 proguard_injars = [p for p in input_jars if p not in exclude_paths] |
| 134 proguard_cmd += ['-injars', ':'.join(proguard_injars)] |
| 135 |
| 136 for config_file in configs: |
| 137 proguard_cmd += ['-include', config_file] |
| 138 |
| 139 # The output jar must be specified after inputs. |
| 140 proguard_cmd += ['-outjars', options.obfuscated_jar_path] |
| 114 | 141 |
| 115 build_utils.CheckOutput(proguard_cmd) | 142 build_utils.CheckOutput(proguard_cmd) |
| 143 |
| 144 this_info = { |
| 145 'inputs': proguard_injars, |
| 146 'configs': configs |
| 147 } |
| 148 |
| 149 build_utils.WriteJson( |
| 150 this_info, options.obfuscated_jar_path + '.info') |
| 116 else: | 151 else: |
| 117 output_files = [ | 152 output_files = [ |
| 118 options.obfuscated_jar_path, | 153 options.obfuscated_jar_path, |
| 154 options.obfuscated_jar_path + '.info', |
| 119 options.obfuscated_jar_path + '.dump', | 155 options.obfuscated_jar_path + '.dump', |
| 120 options.obfuscated_jar_path + '.seeds', | 156 options.obfuscated_jar_path + '.seeds', |
| 121 options.obfuscated_jar_path + '.usage', | 157 options.obfuscated_jar_path + '.usage', |
| 122 options.obfuscated_jar_path + '.mapping'] | 158 options.obfuscated_jar_path + '.mapping'] |
| 123 for f in output_files: | 159 for f in output_files: |
| 124 if os.path.exists(f): | 160 if os.path.exists(f): |
| 125 os.remove(f) | 161 os.remove(f) |
| 126 build_utils.Touch(f) | 162 build_utils.Touch(f) |
| 127 | 163 |
| 128 if options.stamp: | 164 if options.stamp: |
| 129 build_utils.Touch(options.stamp) | 165 build_utils.Touch(options.stamp) |
| 130 | 166 |
| 131 if __name__ == '__main__': | 167 if __name__ == '__main__': |
| 132 sys.exit(main(sys.argv[1:])) | 168 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |