diff options
author | cjhopman@chromium.org <cjhopman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-14 19:51:58 +0000 |
---|---|---|
committer | cjhopman@chromium.org <cjhopman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-14 19:51:58 +0000 |
commit | 9d20d7d94e5e3ee442be84c3ed7290e57bcba75e (patch) | |
tree | 39859f66f4d8f10a929e2cd100ae69cd60839bcf | |
parent | 80d1a1a649c151aa924ea7efb4738cb193791a71 (diff) | |
download | chromium_src-9d20d7d94e5e3ee442be84c3ed7290e57bcba75e.zip chromium_src-9d20d7d94e5e3ee442be84c3ed7290e57bcba75e.tar.gz chromium_src-9d20d7d94e5e3ee442be84c3ed7290e57bcba75e.tar.bz2 |
Make test apks only dex files not in tested apk (proguard version)
At runtime, the classloader will look for classes in both apk's dex
files. In the standard Android build system, an instrumentation test
apk's dex file does not include the classes included in the tested apk's
dex file.
To do this, when doing obfuscation for an apk, write the list of
libraries included in the obfuscated jar and the list of proguard config
files. Then, when proguarding the test apk's code, exclude those
libraries included in the tested apk, use the configs from the tested
apk, and apply the proguard mapping (the renames from obfuscation). Also
add some extra test-specific proguard options.
Now that the test apk does not bundle its own copy of all the tested
apk's classes, some things may need to be kept in the main apk just for
tests. However, we already keep everything in org.chromium.** and
com.google.android.apps.** because of the fact that the test apk was
using its own copy of all the classes and so we couldn't depend on the
tests to actually catch us from over-optimizing with proguard.
BUG=272790
Review URL: https://codereview.chromium.org/321883002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@277257 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | build/android/dex_action.gypi | 2 | ||||
-rwxr-xr-x | build/android/gyp/apk_obfuscate.py | 58 | ||||
-rw-r--r-- | build/android/gyp/util/build_utils.py | 2 | ||||
-rw-r--r-- | build/java_aidl.gypi | 2 | ||||
-rw-r--r-- | build/java_apk.gypi | 39 |
5 files changed, 81 insertions, 22 deletions
diff --git a/build/android/dex_action.gypi b/build/android/dex_action.gypi index 0575e52..9ea3e71 100644 --- a/build/android/dex_action.gypi +++ b/build/android/dex_action.gypi @@ -49,7 +49,7 @@ '--dex-path=<(output_path)', '--android-sdk-tools=<(android_sdk_tools)', '--configuration-name=<(CONFIGURATION_NAME)', - '--proguard-enabled=<(proguard_enabled)', + '--proguard-enabled=>(proguard_enabled)', '--proguard-enabled-input-path=<(proguard_enabled_input_path)', '--no-locals=>(dex_no_locals)', '>@(dex_additional_options)', diff --git a/build/android/gyp/apk_obfuscate.py b/build/android/gyp/apk_obfuscate.py index 1e2eccf..fb29d0c 100755 --- a/build/android/gyp/apk_obfuscate.py +++ b/build/android/gyp/apk_obfuscate.py @@ -27,11 +27,10 @@ def ParseArgs(argv): 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', + parser.add_option('--proguard-configs', help='Paths to proguard config files') parser.add_option('--configuration-name', @@ -44,6 +43,8 @@ def ParseArgs(argv): parser.add_option('--testapp', action='store_true', help='Set this if building an instrumentation test apk') + parser.add_option('--tested-apk-obfuscated-jar-path', + help='Path to obfusctated jar of the tested apk') parser.add_option('--test-jar-path', help='Output path for jar containing all the test apk\'s ' 'code.') @@ -65,6 +66,12 @@ def ParseArgs(argv): 'configuration_name', 'obfuscated_jar_path', ) + + if options.testapp: + required_options += ( + 'test_jar_path', + ) + build_utils.CheckOptions(options, parser, required=required_options) return options, args @@ -74,7 +81,7 @@ def main(argv): options, _ = ParseArgs(argv) library_classpath = [options.android_sdk_jar] - javac_custom_classpath = build_utils.ParseGypList(options.input_jars_paths) + input_jars = build_utils.ParseGypList(options.input_jars_paths) dependency_class_filters = [ '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] @@ -87,35 +94,64 @@ def main(argv): if options.testapp: with zipfile.ZipFile(options.test_jar_path, 'w') as test_jar: - for jar in build_utils.ParseGypList(options.input_jars_paths): + for jar in input_jars: 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 options.configuration_name == 'Release' and options.proguard_enabled: - 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, '-dump', options.obfuscated_jar_path + '.dump', '-printseeds', options.obfuscated_jar_path + '.seeds', '-printusage', options.obfuscated_jar_path + '.usage', '-printmapping', options.obfuscated_jar_path + '.mapping', ] - for proguard_file in build_utils.ParseGypList( - options.proguard_config_files): - proguard_cmd += ['-include', proguard_file] + exclude_paths = [] + configs = build_utils.ParseGypList(options.proguard_configs) + if (options.tested_apk_obfuscated_jar_path and + options.tested_apk_obfuscated_jar_path != '/'): + # configs should only contain the process_resources.py generated config. + assert len(configs) == 1, ( + 'test apks should not have custom proguard configs: ' + str(configs)) + tested_jar_info = build_utils.ReadJson( + options.tested_apk_obfuscated_jar_path + '.info') + exclude_paths = tested_jar_info['inputs'] + configs = tested_jar_info['configs'] + proguard_cmd += [ + '-dontobfuscate', + '-dontoptimize', + '-dontskipnonpubliclibraryclassmembers', + '-libraryjars', options.tested_apk_obfuscated_jar_path, + '-applymapping', options.tested_apk_obfuscated_jar_path + '.mapping', + ] + + proguard_injars = [p for p in input_jars if p not in exclude_paths] + proguard_cmd += ['-injars', ':'.join(proguard_injars)] + + for config_file in configs: + proguard_cmd += ['-include', config_file] + + # The output jar must be specified after inputs. + proguard_cmd += ['-outjars', options.obfuscated_jar_path] build_utils.CheckOutput(proguard_cmd) + + this_info = { + 'inputs': proguard_injars, + 'configs': configs + } + + build_utils.WriteJson( + this_info, options.obfuscated_jar_path + '.info') else: output_files = [ options.obfuscated_jar_path, + options.obfuscated_jar_path + '.info', options.obfuscated_jar_path + '.dump', options.obfuscated_jar_path + '.seeds', options.obfuscated_jar_path + '.usage', diff --git a/build/android/gyp/util/build_utils.py b/build/android/gyp/util/build_utils.py index fa8592b..fb6f4f1 100644 --- a/build/android/gyp/util/build_utils.py +++ b/build/android/gyp/util/build_utils.py @@ -85,7 +85,7 @@ def WriteJson(obj, path, only_if_changed=False): with open(path, 'r') as oldfile: old_dump = oldfile.read() - new_dump = json.dumps(obj) + new_dump = json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': ')) if not only_if_changed or old_dump != new_dump: with open(path, 'w') as outfile: diff --git a/build/java_aidl.gypi b/build/java_aidl.gypi index 2d4ae2e..3c6f8e4 100644 --- a/build/java_aidl.gypi +++ b/build/java_aidl.gypi @@ -46,7 +46,7 @@ }, }, 'conditions': [ - ['"<(aidl_import_include)"!=""', { + ['aidl_import_include != ""', { 'variables': { 'additional_aidl_arguments': [ '-I<(aidl_import_include)' ], 'additional_aidl_input_paths': [ '<!@(find <(aidl_import_include) -name "*.java")', ] diff --git a/build/java_apk.gypi b/build/java_apk.gypi index 348ce81..92c8cfa 100644 --- a/build/java_apk.gypi +++ b/build/java_apk.gypi @@ -52,6 +52,7 @@ # never_lint - Set to 1 to not run lint on this target. { 'variables': { + 'tested_apk_obfuscated_jar_path%': '/', 'tested_apk_dex_path%': '/', 'additional_input_paths': [], 'input_jars_paths': [], @@ -158,8 +159,16 @@ # direct_dependent_settings, but a variable set by a direct_dependent_settings # cannot be lifted in a dependent to all_dependent_settings. 'all_dependent_settings': { + 'conditions': [ + ['proguard_enabled == "true"', { + 'variables': { + 'proguard_enabled': 'true', + } + }], + ], 'variables': { 'apk_output_jar_path': '<(jar_path)', + 'tested_apk_obfuscated_jar_path': '<(obfuscated_jar_path)', 'tested_apk_dex_path': '<(dex_path)', }, }, @@ -604,23 +613,36 @@ 'message': 'Obfuscating <(_target_name)', 'variables': { 'additional_obfuscate_options': [], + 'additional_obfuscate_input_paths': [], 'proguard_out_dir': '<(intermediate_dir)/proguard', 'proguard_input_jar_paths': [ '>@(input_jars_paths)', '<(jar_path)', ], - 'conditions': [ + 'target_conditions': [ ['is_test_apk == 1', { 'additional_obfuscate_options': [ '--testapp', ], }], + ['is_test_apk == 1 and tested_apk_obfuscated_jar_path != "/"', { + 'additional_obfuscate_options': [ + '--tested-apk-obfuscated-jar-path', '>(tested_apk_obfuscated_jar_path)', + ], + 'additional_obfuscate_input_paths': [ + '>(tested_apk_obfuscated_jar_path).info', + ], + }], ['proguard_enabled == "true"', { 'additional_obfuscate_options': [ '--proguard-enabled', ], }], ], + 'obfuscate_input_jars_paths': [ + '>@(input_jars_paths)', + '<(jar_path)', + ], }, 'conditions': [ ['is_test_apk == 1', { @@ -633,15 +655,16 @@ '<(DEPTH)/build/android/gyp/apk_obfuscate.py', '<(DEPTH)/build/android/gyp/util/build_utils.py', '>@(proguard_flags_paths)', - '>@(proguard_input_jar_paths)', + '>@(obfuscate_input_jars_paths)', + '>@(additional_obfuscate_input_paths)', + '<(instr_stamp)', ], 'outputs': [ - # This lists obfuscate_stamp instead of obfuscated_jar_path because - # ant only writes the latter if the md5 of the inputs changes. '<(obfuscate_stamp)', # In non-Release builds, these paths will all be empty files. '<(obfuscated_jar_path)', + '<(obfuscated_jar_path).info', '<(obfuscated_jar_path).dump', '<(obfuscated_jar_path).seeds', '<(obfuscated_jar_path).mapping', @@ -657,15 +680,17 @@ '--android-sdk-jar', '<(android_sdk_jar)', '--input-jars-paths=>(proguard_input_jar_paths)', + '--proguard-configs=>(proguard_flags_paths)', + + '--test-jar-path', '<(test_jar_path)', '--obfuscated-jar-path', '<(obfuscated_jar_path)', '--proguard-jar-path', '<(android_sdk_root)/tools/proguard/lib/proguard.jar', - '--proguard-config-files=<(proguard_flags_paths)', '--stamp', '<(obfuscate_stamp)', - '<@(additional_obfuscate_options)', + '>@(additional_obfuscate_options)', ], }, { @@ -695,8 +720,6 @@ '>(tested_apk_dex_path).inputs', ], }], - ], - 'conditions': [ ['proguard_enabled == "true"', { 'inputs': [ '<(obfuscate_stamp)' ] }, { |