Commit bfe48f4d authored by Torne (Richard Coles)'s avatar Torne (Richard Coles) Committed by Commit Bot

Add missing Proguard rule for library R class.

The framework attempts to call {manifest_package}.R.onResourcesLoaded()
when an APK that is built as a library (typically WebView) is run as a
standalone app, which is intended to fix the R class references to point
to the correct package ID.

We usually already generate the required class/method, but we weren't
keeping it during proguard. Fixing this by adding the missing keep rule
and ensuring that we always generate a class with the right package
means that code running in the context of the WebView APK should now be
able to refer to resources normally via the R class without needing to
use getIdentifier.

Bug: 1009982
Change-Id: Ib33d59ca2696ba949536be34c997690a310c2370
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1834042
Commit-Queue: Richard Coles <torne@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702861}
parent 157d1109
...@@ -23,6 +23,7 @@ import shutil ...@@ -23,6 +23,7 @@ import shutil
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
import textwrap
import zipfile import zipfile
from xml.etree import ElementTree from xml.etree import ElementTree
...@@ -657,6 +658,8 @@ def _PackageApk(options, build): ...@@ -657,6 +658,8 @@ def _PackageApk(options, build):
Args: Args:
options: The command-line options. options: The command-line options.
build: BuildContext object. build: BuildContext object.
Returns:
The manifest package name for the APK.
""" """
dep_subdirs = resource_utils.ExtractDeps(options.dependencies_res_zips, dep_subdirs = resource_utils.ExtractDeps(options.dependencies_res_zips,
build.deps_dir) build.deps_dir)
...@@ -763,6 +766,19 @@ def _PackageApk(options, build): ...@@ -763,6 +766,19 @@ def _PackageApk(options, build):
build_utils.CheckOutput(link_command, print_stdout=False, print_stderr=False) build_utils.CheckOutput(link_command, print_stdout=False, print_stderr=False)
if options.proguard_file and (options.shared_resources
or options.app_as_shared_lib):
# Make sure the R class associated with the manifest package does not have
# its onResourcesLoaded method obfuscated or removed, so that the framework
# can call it in the case where the APK is being loaded as a library.
with open(build.proguard_path, 'a') as proguard_file:
keep_rule = '''
-keep class {package}.R {{
public static void onResourcesLoaded(int);
}}
'''.format(package=desired_manifest_package_name)
proguard_file.write(textwrap.dedent(keep_rule))
if options.proto_path and options.arsc_path: if options.proto_path and options.arsc_path:
build_utils.CheckOutput([ build_utils.CheckOutput([
options.aapt2_path, 'convert', '-o', build.arsc_path, build.proto_path options.aapt2_path, 'convert', '-o', build.arsc_path, build.proto_path
...@@ -775,6 +791,8 @@ def _PackageApk(options, build): ...@@ -775,6 +791,8 @@ def _PackageApk(options, build):
_OptimizeApk(build.optimized_arsc_path, options, build.temp_dir, _OptimizeApk(build.optimized_arsc_path, options, build.temp_dir,
build.arsc_path, build.r_txt_path) build.arsc_path, build.r_txt_path)
return desired_manifest_package_name
def _OptimizeApk(output, options, temp_dir, unoptimized_path, r_txt_path): def _OptimizeApk(output, options, temp_dir, unoptimized_path, r_txt_path):
"""Optimize intermediate .ap_ file with aapt2. """Optimize intermediate .ap_ file with aapt2.
...@@ -897,7 +915,7 @@ def main(args): ...@@ -897,7 +915,7 @@ def main(args):
build_utils.MakeDirectory(debug_temp_resources_dir) build_utils.MakeDirectory(debug_temp_resources_dir)
with resource_utils.BuildContext(debug_temp_resources_dir) as build: with resource_utils.BuildContext(debug_temp_resources_dir) as build:
_PackageApk(options, build) manifest_package_name = _PackageApk(options, build)
# If --shared-resources-whitelist is used, the all resources listed in # If --shared-resources-whitelist is used, the all resources listed in
# the corresponding R.txt file will be non-final, and an onResourcesLoaded() # the corresponding R.txt file will be non-final, and an onResourcesLoaded()
...@@ -929,11 +947,16 @@ def main(args): ...@@ -929,11 +947,16 @@ def main(args):
custom_root_package_name = options.package_name custom_root_package_name = options.package_name
grandparent_custom_package_name = options.r_java_root_package_name grandparent_custom_package_name = options.r_java_root_package_name
if options.shared_resources or options.app_as_shared_lib:
package_for_library = manifest_package_name
else:
package_for_library = None
resource_utils.CreateRJavaFiles( resource_utils.CreateRJavaFiles(
build.srcjar_dir, None, build.r_txt_path, options.extra_res_packages, build.srcjar_dir, package_for_library, build.r_txt_path,
options.extra_r_text_files, rjava_build_options, options.srcjar_out, options.extra_res_packages, options.extra_r_text_files,
custom_root_package_name, grandparent_custom_package_name, rjava_build_options, options.srcjar_out, custom_root_package_name,
options.extra_main_r_text_files) grandparent_custom_package_name, options.extra_main_r_text_files)
build_utils.ZipDir(build.srcjar_path, build.srcjar_dir) build_utils.ZipDir(build.srcjar_path, build.srcjar_dir)
......
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