Commit 69edfd6c authored by Sam Maier's avatar Sam Maier Committed by Commit Bot

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

This reverts commit 0dce4507.

Reason for revert: Fixed cronet issue

Original change's description:
> Revert "Android build: moved proguarding into dex() for most cases"
> 
> This reverts commit 93aa458f.
> 
> Reason for revert: Suspect for build failure https://ci.chromium.org/p/chromium/builders/luci.chromium.ci/android-rel/2802
> 
> Original change's description:
> > 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/1227142
> > Reviewed-by: Yaron Friedman <yfriedman@chromium.org>
> > Reviewed-by: agrieve <agrieve@chromium.org>
> > Commit-Queue: Sam Maier <smaier@chromium.org>
> > Cr-Commit-Position: refs/heads/master@{#597257}
> 
> TBR=yfriedman@chromium.org,agrieve@chromium.org,smaier@chromium.org
> 
> Change-Id: Idf8f855094fb1bf4c4e1b47326b76166a158c1e8
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: 872904
> Reviewed-on: https://chromium-review.googlesource.com/c/1265559
> Reviewed-by: Kevin McNee <mcnee@chromium.org>
> Commit-Queue: Kevin McNee <mcnee@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#597274}

TBR=yfriedman@chromium.org,agrieve@chromium.org,mcnee@chromium.org,smaier@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: 872904
Change-Id: I1bebed520132e81b199c639690d36171ccb46ab6
Reviewed-on: https://chromium-review.googlesource.com/c/1271792
Commit-Queue: Sam Maier <smaier@chromium.org>
Reviewed-by: default avataragrieve <agrieve@chromium.org>
Reviewed-by: default avatarSam Maier <smaier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#598766}
parent 62135e9f
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
import optparse import optparse
import os import os
import shutil
import sys import sys
import tempfile import tempfile
...@@ -56,15 +57,23 @@ def _ParseOptions(args): ...@@ -56,15 +57,23 @@ def _ParseOptions(args):
'included by --proguard-configs, but that should ' 'included by --proguard-configs, but that should '
'not actually be included.') 'not actually be included.')
parser.add_option('--mapping', help='Path to proguard mapping to apply.') 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', parser.add_option('--classpath', action='append',
help='Classpath for proguard.') help='Classpath for proguard.')
parser.add_option('--enable-dangerous-optimizations', action='store_true', parser.add_option('--enable-dangerous-optimizations', action='store_true',
help='Enable optimizations which are known to have issues.') 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', parser.add_option('--verbose', '-v', action='store_true',
help='Print all proguard output') help='Print all proguard output')
options, _ = parser.parse_args(args) 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 = [] classpath = []
for arg in options.classpath: for arg in options.classpath:
classpath += build_utils.ParseGnList(arg) classpath += build_utils.ParseGnList(arg)
...@@ -79,16 +88,38 @@ def _ParseOptions(args): ...@@ -79,16 +88,38 @@ def _ParseOptions(args):
options.input_paths = build_utils.ParseGnList(options.input_paths) options.input_paths = build_utils.ParseGnList(options.input_paths)
if not options.mapping_output:
options.mapping_output = options.output_path + ".mapping"
return options 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. # TODO: R8 needs -applymapping equivalent.
cmd = [ cmd = [
'java', '-jar', options.r8_path, 'java', '-jar', options.r8_path,
'--no-desugaring', '--no-desugaring',
'--classfile', '--no-data-resources',
'--output', options.output_path, '--output', output_dir,
'--pg-map-output', map_output_path, '--pg-map-output', map_output_path,
] ]
...@@ -101,6 +132,10 @@ def _CreateR8Command(options, map_output_path): ...@@ -101,6 +132,10 @@ def _CreateR8Command(options, map_output_path):
for config_file in options.proguard_configs: for config_file in options.proguard_configs:
cmd += ['--pg-conf', config_file] 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 cmd += options.input_paths
return cmd return cmd
...@@ -114,6 +149,7 @@ def main(args): ...@@ -114,6 +149,7 @@ def main(args):
proguard.configs(options.proguard_configs) proguard.configs(options.proguard_configs)
proguard.config_exclusions(options.proguard_config_exclusions) proguard.config_exclusions(options.proguard_config_exclusions)
proguard.outjar(options.output_path) 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. # If a jar is part of input no need to include it as library jar.
classpath = [ classpath = [
...@@ -127,12 +163,17 @@ def main(args): ...@@ -127,12 +163,17 @@ def main(args):
# TODO(agrieve): Remove proguard usages. # TODO(agrieve): Remove proguard usages.
if options.r8_path: if options.r8_path:
with tempfile.NamedTemporaryFile() as mapping_temp: with tempfile.NamedTemporaryFile() as mapping_temp:
# R8 will output mapping file to this temp file if options.output_path.endswith('.dex'):
cmd = _CreateR8Command(options, mapping_temp.name) with build_utils.TempDir() as tmp_dex_dir:
build_utils.CheckOutput(cmd) 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. # 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: with build_utils.AtomicOutput(map_path) as mapping:
# Mapping files generated by R8 include comments that may break # Mapping files generated by R8 include comments that may break
# some of our tooling so remove those. # some of our tooling so remove those.
......
...@@ -51,6 +51,7 @@ class ProguardCmdBuilder(object): ...@@ -51,6 +51,7 @@ class ProguardCmdBuilder(object):
self._configs = None self._configs = None
self._config_exclusions = None self._config_exclusions = None
self._outjar = None self._outjar = None
self._mapping_output = None
self._verbose = False self._verbose = False
self._disabled_optimizations = [] self._disabled_optimizations = []
...@@ -58,6 +59,10 @@ class ProguardCmdBuilder(object): ...@@ -58,6 +59,10 @@ class ProguardCmdBuilder(object):
assert self._outjar is None assert self._outjar is None
self._outjar = path self._outjar = path
def mapping_output(self, path):
assert self._mapping_output is None
self._mapping_output = path
def mapping(self, path): def mapping(self, path):
assert self._mapping is None assert self._mapping is None
assert os.path.exists(path), path assert os.path.exists(path), path
...@@ -124,7 +129,7 @@ class ProguardCmdBuilder(object): ...@@ -124,7 +129,7 @@ class ProguardCmdBuilder(object):
'-outjars', self._outjar, '-outjars', self._outjar,
'-printseeds', self._outjar + '.seeds', '-printseeds', self._outjar + '.seeds',
'-printusage', self._outjar + '.usage', '-printusage', self._outjar + '.usage',
'-printmapping', self._outjar + '.mapping', '-printmapping', self._mapping_output,
] ]
if self._verbose: if self._verbose:
...@@ -156,7 +161,7 @@ class ProguardCmdBuilder(object): ...@@ -156,7 +161,7 @@ class ProguardCmdBuilder(object):
return [ return [
self._outjar, self._outjar,
self._outjar + '.flags', self._outjar + '.flags',
self._outjar + '.mapping', self._mapping_output,
self._outjar + '.seeds', self._outjar + '.seeds',
self._outjar + '.usage', self._outjar + '.usage',
] ]
......
...@@ -860,7 +860,7 @@ def main(argv): ...@@ -860,7 +860,7 @@ def main(argv):
help='Whether proguard is enabled for this apk or bundle module.') help='Whether proguard is enabled for this apk or bundle module.')
parser.add_option('--proguard-configs', parser.add_option('--proguard-configs',
help='GN-list of proguard flag files to use in final apk.') help='GN-list of proguard flag files to use in final apk.')
parser.add_option('--proguard-output-jar-path', parser.add_option('--proguard-mapping-path',
help='Path to jar created by ProGuard step') help='Path to jar created by ProGuard step')
parser.add_option('--fail', parser.add_option('--fail',
help='GN-list of error message lines to fail with.') help='GN-list of error message lines to fail with.')
...@@ -1257,8 +1257,8 @@ def main(argv): ...@@ -1257,8 +1257,8 @@ def main(argv):
deps_proguard_disabled)) deps_proguard_disabled))
else: else:
deps_info['proguard_enabled'] = bool(options.proguard_enabled) deps_info['proguard_enabled'] = bool(options.proguard_enabled)
if options.proguard_output_jar_path: if options.proguard_mapping_path:
deps_info['proguard_output_jar_path'] = options.proguard_output_jar_path deps_info['proguard_mapping_path'] = options.proguard_mapping_path
# The java code for an instrumentation test apk is assembled differently for # The java code for an instrumentation test apk is assembled differently for
# ProGuard vs. non-ProGuard. # ProGuard vs. non-ProGuard.
...@@ -1287,7 +1287,7 @@ def main(argv): ...@@ -1287,7 +1287,7 @@ def main(argv):
if p not in extra_jars) if p not in extra_jars)
tested_apk_config = GetDepConfig(options.tested_apk_config) tested_apk_config = GetDepConfig(options.tested_apk_config)
deps_info['proguard_under_test_mapping'] = ( deps_info['proguard_under_test_mapping'] = (
tested_apk_config['proguard_output_jar_path'] + '.mapping') tested_apk_config['proguard_mapping_path'])
elif options.proguard_enabled: elif options.proguard_enabled:
# Not sure why you'd want to proguard the test apk when the under-test apk # Not sure why you'd want to proguard the test apk when the under-test apk
# is not proguarded, but it's easy enough to support. # is not proguarded, but it's easy enough to support.
......
This diff is collapsed.
This diff is collapsed.
...@@ -1501,7 +1501,9 @@ template("monochrome_public_apk_or_module_tmpl") { ...@@ -1501,7 +1501,9 @@ template("monochrome_public_apk_or_module_tmpl") {
} }
version_name = chrome_version_name version_name = chrome_version_name
enable_multidex = true if (experimental_r8_path == "") {
enable_multidex = true
}
} }
} }
...@@ -1849,7 +1851,9 @@ android_app_bundle("monochrome_public_bundle") { ...@@ -1849,7 +1851,9 @@ android_app_bundle("monochrome_public_bundle") {
base_module_target = ":monochrome_public_base_module" base_module_target = ":monochrome_public_base_module"
if (!is_java_debug) { if (!is_java_debug) {
proguard_enabled = true proguard_enabled = true
enable_multidex = true if (experimental_r8_path == "") {
enable_multidex = true
}
proguard_android_sdk_dep = webview_framework_dep proguard_android_sdk_dep = webview_framework_dep
} }
if (modularize_ar) { if (modularize_ar) {
......
...@@ -49,19 +49,16 @@ android_library("runtime_library_for_tests_java") { ...@@ -49,19 +49,16 @@ android_library("runtime_library_for_tests_java") {
] ]
} }
dist_jar("webapk_runtime_library") { proguarded_dist_dex("webapk_runtime_library") {
requires_android = true
deps = [ deps = [
":runtime_library_for_assets_java", ":runtime_library_for_assets_java",
] ]
proguard_enabled = true
proguard_configs = [ proguard_configs = [
"runtime_library.proguard.flags", "runtime_library.proguard.flags",
"//base/android/proguard/chromium_code.flags", "//base/android/proguard/chromium_code.flags",
"//base/android/proguard/chromium_apk.flags", "//base/android/proguard/chromium_apk.flags",
] ]
output = "$target_gen_dir/$target_name.jar" output = "$target_gen_dir/$runtime_library_dex_asset_name"
dex_path = "$target_gen_dir/$runtime_library_dex_asset_name"
} }
android_assets("runtime_library_assets") { 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