Commit 93aa458f authored by Sam Maier's avatar Sam Maier Committed by Commit Bot

Android build: moved proguarding into dex() for most cases

This is to allow R8 to proguard and dex in one call, since it has some
optimizations that only work when doing everything in one shot.

Bug: 872904
Change-Id: Id2c75dac4d9feecae461a0b1e279056253f1b71b
Reviewed-on: https://chromium-review.googlesource.com/c/1227142Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avataragrieve <agrieve@chromium.org>
Commit-Queue: Sam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#597257}
parent b2612e18
......@@ -6,6 +6,7 @@
import optparse
import os
import shutil
import sys
import tempfile
......@@ -56,15 +57,23 @@ def _ParseOptions(args):
'included by --proguard-configs, but that should '
'not actually be included.')
parser.add_option('--mapping', help='Path to proguard mapping to apply.')
parser.add_option('--mapping-output',
help='Path for proguard to output mapping file to.')
parser.add_option('--classpath', action='append',
help='Classpath for proguard.')
parser.add_option('--enable-dangerous-optimizations', action='store_true',
help='Enable optimizations which are known to have issues.')
parser.add_option('--main-dex-rules-path', action='append',
help='Paths to main dex rules for multidex'
'- only works with R8.')
parser.add_option('--verbose', '-v', action='store_true',
help='Print all proguard output')
options, _ = parser.parse_args(args)
assert not options.main_dex_rules_path or options.r8_path, \
"R8 must be enabled to pass main dex rules."
classpath = []
for arg in options.classpath:
classpath += build_utils.ParseGnList(arg)
......@@ -79,16 +88,38 @@ def _ParseOptions(args):
options.input_paths = build_utils.ParseGnList(options.input_paths)
if not options.mapping_output:
options.mapping_output = options.output_path + ".mapping"
return options
def _CreateR8Command(options, map_output_path):
def _MoveTempDexFile(tmp_dex_dir, dex_path):
"""Move the temp dex file out of |tmp_dex_dir|.
Args:
tmp_dex_dir: Path to temporary directory created with tempfile.mkdtemp().
The directory should have just a single file.
dex_path: Target path to move dex file to.
Raises:
Exception if there are multiple files in |tmp_dex_dir|.
"""
tempfiles = os.listdir(tmp_dex_dir)
if len(tempfiles) > 1:
raise Exception('%d files created, expected 1' % len(tempfiles))
tmp_dex_path = os.path.join(tmp_dex_dir, tempfiles[0])
shutil.move(tmp_dex_path, dex_path)
def _CreateR8Command(options, map_output_path, output_dir):
# TODO: R8 needs -applymapping equivalent.
cmd = [
'java', '-jar', options.r8_path,
'--no-desugaring',
'--classfile',
'--output', options.output_path,
'--no-data-resources',
'--output', output_dir,
'--pg-map-output', map_output_path,
]
......@@ -101,6 +132,10 @@ def _CreateR8Command(options, map_output_path):
for config_file in options.proguard_configs:
cmd += ['--pg-conf', config_file]
if options.main_dex_rules_path:
for main_dex_rule in options.main_dex_rules_path:
cmd += ['--main-dex-rules', main_dex_rule]
cmd += options.input_paths
return cmd
......@@ -114,6 +149,7 @@ def main(args):
proguard.configs(options.proguard_configs)
proguard.config_exclusions(options.proguard_config_exclusions)
proguard.outjar(options.output_path)
proguard.mapping_output(options.mapping_output)
# If a jar is part of input no need to include it as library jar.
classpath = [
......@@ -127,12 +163,17 @@ def main(args):
# TODO(agrieve): Remove proguard usages.
if options.r8_path:
with tempfile.NamedTemporaryFile() as mapping_temp:
# R8 will output mapping file to this temp file
cmd = _CreateR8Command(options, mapping_temp.name)
build_utils.CheckOutput(cmd)
if options.output_path.endswith('.dex'):
with build_utils.TempDir() as tmp_dex_dir:
cmd = _CreateR8Command(options, mapping_temp.name, tmp_dex_dir)
build_utils.CheckOutput(cmd)
_MoveTempDexFile(tmp_dex_dir, options.output_path)
else:
cmd = _CreateR8Command(options, mapping_temp.name, options.output_path)
build_utils.CheckOutput(cmd)
# Copy the mapping file back to where it should be.
map_path = options.output_path + ".mapping"
map_path = options.mapping_output
with build_utils.AtomicOutput(map_path) as mapping:
# Mapping files generated by R8 include comments that may break
# some of our tooling so remove those.
......
......@@ -51,6 +51,7 @@ class ProguardCmdBuilder(object):
self._configs = None
self._config_exclusions = None
self._outjar = None
self._mapping_output = None
self._verbose = False
self._disabled_optimizations = []
......@@ -58,6 +59,10 @@ class ProguardCmdBuilder(object):
assert self._outjar is None
self._outjar = path
def mapping_output(self, path):
assert self._mapping_output is None
self._mapping_output = path
def mapping(self, path):
assert self._mapping is None
assert os.path.exists(path), path
......@@ -124,7 +129,7 @@ class ProguardCmdBuilder(object):
'-outjars', self._outjar,
'-printseeds', self._outjar + '.seeds',
'-printusage', self._outjar + '.usage',
'-printmapping', self._outjar + '.mapping',
'-printmapping', self._mapping_output,
]
if self._verbose:
......@@ -156,7 +161,7 @@ class ProguardCmdBuilder(object):
return [
self._outjar,
self._outjar + '.flags',
self._outjar + '.mapping',
self._mapping_output,
self._outjar + '.seeds',
self._outjar + '.usage',
]
......
This diff is collapsed.
This diff is collapsed.
......@@ -1500,7 +1500,9 @@ template("monochrome_public_apk_or_module_tmpl") {
}
version_name = chrome_version_name
enable_multidex = true
if (experimental_r8_path == "") {
enable_multidex = true
}
}
}
......@@ -1848,7 +1850,9 @@ android_app_bundle("monochrome_public_bundle") {
base_module_target = ":monochrome_public_base_module"
if (!is_java_debug) {
proguard_enabled = true
enable_multidex = true
if (experimental_r8_path == "") {
enable_multidex = true
}
proguard_android_sdk_dep = webview_framework_dep
}
if (modularize_ar) {
......
......@@ -49,19 +49,16 @@ android_library("runtime_library_for_tests_java") {
]
}
dist_jar("webapk_runtime_library") {
requires_android = true
proguarded_dist_dex("webapk_runtime_library") {
deps = [
":runtime_library_for_assets_java",
]
proguard_enabled = true
proguard_configs = [
"runtime_library.proguard.flags",
"//base/android/proguard/chromium_code.flags",
"//base/android/proguard/chromium_apk.flags",
]
output = "$target_gen_dir/$target_name.jar"
dex_path = "$target_gen_dir/$runtime_library_dex_asset_name"
output = "$target_gen_dir/$runtime_library_dex_asset_name"
}
android_assets("runtime_library_assets") {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment