Chromium Code Reviews| Index: build/android/gyp/apk_obfuscate.py |
| diff --git a/build/android/gyp/apk_obfuscate.py b/build/android/gyp/apk_obfuscate.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..2baa8b45653f50daaaecfdab0877e04049f018a1 |
| --- /dev/null |
| +++ b/build/android/gyp/apk_obfuscate.py |
| @@ -0,0 +1,134 @@ |
| +#!/usr/bin/env python |
| +# |
| +# Copyright 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Generates the obfuscated jar and test jar for an apk. |
| + |
| +If proguard is not enabled or 'Release' is not in the configuration name, |
| +obfuscation will be a no-op. |
| +""" |
| + |
| +import fnmatch |
| +import optparse |
| +import os |
| +import sys |
| +import zipfile |
| + |
| +from util import build_utils |
| + |
| +def ParseArgs(argv): |
| + parser = optparse.OptionParser() |
| + parser.add_option('--android-sdk', help='path to the Android SDK folder') |
| + parser.add_option('--android-sdk-tools', |
| + help='path to the Android SDK build tools folder') |
| + parser.add_option('--android-sdk-jar', |
| + help='path to Android SDK\'s android.jar') |
| + parser.add_option('--proguard-jar-path', |
| + help='Path to proguard.jar in the sdk') |
| + |
| + parser.add_option('--input-jars-paths', |
| + help='Path to jars to include in obfuscated jar') |
| + |
| + parser.add_option('--proguard-config-files', |
| + help='Paths to proguard config files') |
| + |
| + parser.add_option('--configuration-name', |
| + help='Gyp configuration name (i.e. Debug, Release)') |
| + parser.add_option('--proguard-enabled', action='store_true', |
| + help='Set if proguard is enabled for this target.') |
| + |
| + parser.add_option('--obfuscated-jar-path', |
| + help='Output path for obfuscated jar.') |
| + |
| + parser.add_option('--testapp', action='store_true', |
| + help='Set this if building an instrumentation test apk') |
| + parser.add_option('--test-jar-path', |
| + help='Output path for jar containing all the test apk\'s ' |
| + 'code.') |
| + |
| + parser.add_option('--stamp', help='File to touch on success') |
| + |
| + (options, args) = parser.parse_args(argv) |
| + |
| + if args: |
| + parser.error('No positional arguments should be given. ' + str(args)) |
| + |
| + # Check that required options have been provided. |
| + required_options = ( |
| + 'android_sdk', |
| + 'android_sdk_tools', |
| + 'android_sdk_jar', |
| + 'proguard_jar_path', |
| + 'input_jars_paths', |
| + 'configuration_name', |
| + 'obfuscated_jar_path', |
| + ) |
| + build_utils.CheckOptions(options, parser, required=required_options) |
| + |
| + return options, args |
| + |
| + |
| +def main(argv): |
| + options, _ = ParseArgs(argv) |
| + |
| + library_classpath = [options.android_sdk_jar] |
| + javac_custom_classpath = build_utils.ParseGypList(options.input_jars_paths) |
| + |
| + dependency_class_filters = [ |
| + '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] |
| + |
| + def DependencyClassFilter(name): |
| + for name_filter in dependency_class_filters: |
| + if fnmatch.fnmatch(name, name_filter): |
| + return False |
| + return True |
| + |
| + if options.testapp: |
| + with zipfile.ZipFile(options.test_jar_path, 'w') as test_jar: |
| + for jar in build_utils.ParseGypList(options.input_jars_paths): |
| + with zipfile.ZipFile(jar, 'r') as jar_zip: |
| + for name in filter(DependencyClassFilter, jar_zip.namelist()): |
| + with jar_zip.open(name) as zip_entry: |
| + test_jar.writestr(name, zip_entry.read()) |
| + |
| + 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.
|
| + proguard_project_classpath = javac_custom_classpath |
| + |
| + proguard_cmd = [ |
| + 'java', '-jar', options.proguard_jar_path, |
| + '-forceprocessing', |
| + '-injars', ':'.join(proguard_project_classpath), |
| + '-libraryjars', ':'.join(library_classpath), |
| + '-outjars', options.obfuscated_jar_path |
| + ] |
| + |
| + for proguard_file in build_utils.ParseGypList( |
| + options.proguard_config_files): |
| + proguard_cmd += ['-include', proguard_file] |
| + |
| + proguard_cmd += ['-dump', options.obfuscated_jar_path + '.dump'] |
| + 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.
|
| + proguard_cmd += ['-printusage', options.obfuscated_jar_path + '.usage'] |
| + proguard_cmd += ['-printmapping', |
| + options.obfuscated_jar_path + '.mapping'] |
| + |
| + build_utils.CheckOutput(proguard_cmd) |
| + else: |
| + output_files = [ |
| + options.obfuscated_jar_path, |
| + options.obfuscated_jar_path + '.dump', |
| + options.obfuscated_jar_path + '.seeds', |
| + options.obfuscated_jar_path + '.usage', |
| + options.obfuscated_jar_path + '.mapping'] |
| + for f in output_files: |
| + if os.path.exists(f): |
| + 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
|
| + build_utils.Touch(f) |
| + |
| + if options.stamp: |
| + build_utils.Touch(options.stamp) |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv[1:])) |