| 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 optparse |   13 import optparse | 
|   14 import os |   14 import os | 
|   15 import sys |   15 import sys | 
|   16  |   16  | 
|   17 from util import build_utils |   17 from util import build_utils | 
 |   18 from util import proguard_util | 
 |   19  | 
|   18  |   20  | 
|   19 def ParseArgs(argv): |   21 def ParseArgs(argv): | 
|   20   parser = optparse.OptionParser() |   22   parser = optparse.OptionParser() | 
|   21   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') | 
|   22   parser.add_option('--android-sdk-tools', |   24   parser.add_option('--android-sdk-tools', | 
|   23                     help='path to the Android SDK build tools folder') |   25                     help='path to the Android SDK build tools folder') | 
|   24   parser.add_option('--android-sdk-jar', |   26   parser.add_option('--android-sdk-jar', | 
|   25                     help='path to Android SDK\'s android.jar') |   27                     help='path to Android SDK\'s android.jar') | 
|   26   parser.add_option('--proguard-jar-path', |   28   parser.add_option('--proguard-jar-path', | 
|   27                     help='Path to proguard.jar in the sdk') |   29                     help='Path to proguard.jar in the sdk') | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   64       'configuration_name', |   66       'configuration_name', | 
|   65       'obfuscated_jar_path', |   67       'obfuscated_jar_path', | 
|   66       ) |   68       ) | 
|   67  |   69  | 
|   68   if options.testapp: |   70   if options.testapp: | 
|   69     required_options += ( |   71     required_options += ( | 
|   70         'test_jar_path', |   72         'test_jar_path', | 
|   71         ) |   73         ) | 
|   72  |   74  | 
|   73   build_utils.CheckOptions(options, parser, required=required_options) |   75   build_utils.CheckOptions(options, parser, required=required_options) | 
 |   76   return options, args | 
|   74  |   77  | 
|   75   return options, args |   78  | 
 |   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') | 
|   76  |  115  | 
|   77  |  116  | 
|   78 def main(argv): |  117 def main(argv): | 
|   79   options, _ = ParseArgs(argv) |  118   options, _ = ParseArgs(argv) | 
|   80  |  119  | 
|   81   library_classpath = [options.android_sdk_jar] |  | 
|   82   input_jars = build_utils.ParseGypList(options.input_jars_paths) |  120   input_jars = build_utils.ParseGypList(options.input_jars_paths) | 
|   83  |  121  | 
|   84   dependency_class_filters = [ |  | 
|   85       '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] |  | 
|   86  |  | 
|   87   if options.testapp: |  122   if options.testapp: | 
 |  123     dependency_class_filters = [ | 
 |  124         '*R.class', '*R$*.class', '*Manifest.class', '*BuildConfig.class'] | 
|   88     build_utils.MergeZips( |  125     build_utils.MergeZips( | 
|   89         options.test_jar_path, input_jars, dependency_class_filters) |  126         options.test_jar_path, input_jars, dependency_class_filters) | 
|   90  |  127  | 
|   91   if options.configuration_name == 'Release' and options.proguard_enabled: |  128   if options.configuration_name == 'Release' and options.proguard_enabled: | 
|   92     proguard_cmd = [ |  129     DoProguard(options) | 
|   93         'java', '-jar', options.proguard_jar_path, |  | 
|   94         '-forceprocessing', |  | 
|   95         '-libraryjars', ':'.join(library_classpath), |  | 
|   96         '-dump', options.obfuscated_jar_path + '.dump', |  | 
|   97         '-printseeds', options.obfuscated_jar_path + '.seeds', |  | 
|   98         '-printusage', options.obfuscated_jar_path + '.usage', |  | 
|   99         '-printmapping', options.obfuscated_jar_path + '.mapping', |  | 
|  100         ] |  | 
|  101  |  | 
|  102     exclude_paths = [] |  | 
|  103     configs = build_utils.ParseGypList(options.proguard_configs) |  | 
|  104     if (options.tested_apk_obfuscated_jar_path and |  | 
|  105         options.tested_apk_obfuscated_jar_path != '/'): |  | 
|  106       # configs should only contain the process_resources.py generated config. |  | 
|  107       assert len(configs) == 1, ( |  | 
|  108           'test apks should not have custom proguard configs: ' + str(configs)) |  | 
|  109       tested_jar_info = build_utils.ReadJson( |  | 
|  110           options.tested_apk_obfuscated_jar_path + '.info') |  | 
|  111       exclude_paths = tested_jar_info['inputs'] |  | 
|  112       configs = tested_jar_info['configs'] |  | 
|  113       proguard_cmd += [ |  | 
|  114           '-dontobfuscate', |  | 
|  115           '-dontoptimize', |  | 
|  116           '-dontshrink', |  | 
|  117           '-dontskipnonpubliclibraryclassmembers', |  | 
|  118           '-libraryjars', options.tested_apk_obfuscated_jar_path, |  | 
|  119           '-applymapping', options.tested_apk_obfuscated_jar_path + '.mapping', |  | 
|  120           ] |  | 
|  121  |  | 
|  122     proguard_injars = [p for p in input_jars if p not in exclude_paths] |  | 
|  123     proguard_cmd += ['-injars', ':'.join(proguard_injars)] |  | 
|  124  |  | 
|  125     for config_file in configs: |  | 
|  126       proguard_cmd += ['-include', config_file] |  | 
|  127  |  | 
|  128     # The output jar must be specified after inputs. |  | 
|  129     proguard_cmd += ['-outjars', options.obfuscated_jar_path] |  | 
|  130  |  | 
|  131     build_utils.CheckOutput(proguard_cmd) |  | 
|  132  |  | 
|  133     this_info = { |  | 
|  134       'inputs': proguard_injars, |  | 
|  135       'configs': configs |  | 
|  136     } |  | 
|  137  |  | 
|  138     build_utils.WriteJson( |  | 
|  139         this_info, options.obfuscated_jar_path + '.info') |  | 
|  140   else: |  130   else: | 
|  141     output_files = [ |  131     output_files = [ | 
|  142         options.obfuscated_jar_path, |  132         options.obfuscated_jar_path, | 
|  143         options.obfuscated_jar_path + '.info', |  133         options.obfuscated_jar_path + '.info', | 
|  144         options.obfuscated_jar_path + '.dump', |  134         options.obfuscated_jar_path + '.dump', | 
|  145         options.obfuscated_jar_path + '.seeds', |  135         options.obfuscated_jar_path + '.seeds', | 
|  146         options.obfuscated_jar_path + '.usage', |  136         options.obfuscated_jar_path + '.usage', | 
|  147         options.obfuscated_jar_path + '.mapping'] |  137         options.obfuscated_jar_path + '.mapping'] | 
|  148     for f in output_files: |  138     for f in output_files: | 
|  149       if os.path.exists(f): |  139       if os.path.exists(f): | 
|  150         os.remove(f) |  140         os.remove(f) | 
|  151       build_utils.Touch(f) |  141       build_utils.Touch(f) | 
|  152  |  142  | 
|  153   if options.stamp: |  143   if options.stamp: | 
|  154     build_utils.Touch(options.stamp) |  144     build_utils.Touch(options.stamp) | 
|  155  |  145  | 
|  156 if __name__ == '__main__': |  146 if __name__ == '__main__': | 
|  157   sys.exit(main(sys.argv[1:])) |  147   sys.exit(main(sys.argv[1:])) | 
| OLD | NEW |