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 options.configuration_name == 'Release' and options.proguard_enabled: |
| 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 '-dump', options.obfuscated_jar_path + '.dump', |
| 106 '-printseeds', options.obfuscated_jar_path + '.seeds', |
| 107 '-printusage', options.obfuscated_jar_path + '.usage', |
| 108 '-printmapping', options.obfuscated_jar_path + '.mapping', |
| 109 ] |
| 110 |
| 111 for proguard_file in build_utils.ParseGypList( |
| 112 options.proguard_config_files): |
| 113 proguard_cmd += ['-include', proguard_file] |
| 114 |
| 115 build_utils.CheckOutput(proguard_cmd) |
| 116 else: |
| 117 output_files = [ |
| 118 options.obfuscated_jar_path, |
| 119 options.obfuscated_jar_path + '.dump', |
| 120 options.obfuscated_jar_path + '.seeds', |
| 121 options.obfuscated_jar_path + '.usage', |
| 122 options.obfuscated_jar_path + '.mapping'] |
| 123 for f in output_files: |
| 124 if os.path.exists(f): |
| 125 os.remove(f) |
| 126 build_utils.Touch(f) |
| 127 |
| 128 if options.stamp: |
| 129 build_utils.Touch(options.stamp) |
| 130 |
| 131 if __name__ == '__main__': |
| 132 sys.exit(main(sys.argv[1:])) |
OLD | NEW |