Skip to content

Commit

Permalink
Convert apk obfuscation to python
Browse files Browse the repository at this point in the history
For the apk specific java files, we now run proguard on the jar instead of the classes.

BUG=375324,359249

Review URL: https://codereview.chromium.org/322443005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@276377 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
cjhopman@chromium.org committed Jun 11, 2014
1 parent ac59ea8 commit e92d61e
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 210 deletions.
175 changes: 0 additions & 175 deletions build/android/ant/apk-obfuscate.xml

This file was deleted.

132 changes: 132 additions & 0 deletions build/android/gyp/apk_obfuscate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/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 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]

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)
build_utils.Touch(f)

if options.stamp:
build_utils.Touch(options.stamp)

if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
10 changes: 0 additions & 10 deletions build/apk_fake_jar.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,4 @@
'library_dexed_jars_paths': ['>(apk_output_jar_path)'],
},
},
# Add an action with the appropriate output. This allows the generated
# buildfiles to determine which target the output corresponds to.
'actions': [
{
'action_name': 'fake_generate_jar',
'inputs': [],
'outputs': ['>(apk_output_jar_path)'],
'action': [],
},
],
}
Loading

0 comments on commit e92d61e

Please sign in to comment.