Skip to content

Commit

Permalink
Add missing Proguard rule for library R class.
Browse files Browse the repository at this point in the history
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: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702861}
  • Loading branch information
tornewuff authored and Commit Bot committed Oct 4, 2019
1 parent 157d110 commit bfe48f4
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions build/android/gyp/compile_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import subprocess
import sys
import tempfile
import textwrap
import zipfile
from xml.etree import ElementTree

Expand Down Expand Up @@ -657,6 +658,8 @@ def _PackageApk(options, build):
Args:
options: The command-line options.
build: BuildContext object.
Returns:
The manifest package name for the APK.
"""
dep_subdirs = resource_utils.ExtractDeps(options.dependencies_res_zips,
build.deps_dir)
Expand Down Expand Up @@ -763,6 +766,19 @@ def _PackageApk(options, build):

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:
build_utils.CheckOutput([
options.aapt2_path, 'convert', '-o', build.arsc_path, build.proto_path
Expand All @@ -775,6 +791,8 @@ def _PackageApk(options, build):
_OptimizeApk(build.optimized_arsc_path, options, build.temp_dir,
build.arsc_path, build.r_txt_path)

return desired_manifest_package_name


def _OptimizeApk(output, options, temp_dir, unoptimized_path, r_txt_path):
"""Optimize intermediate .ap_ file with aapt2.
Expand Down Expand Up @@ -897,7 +915,7 @@ def main(args):
build_utils.MakeDirectory(debug_temp_resources_dir)

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
# the corresponding R.txt file will be non-final, and an onResourcesLoaded()
Expand Down Expand Up @@ -929,11 +947,16 @@ def main(args):
custom_root_package_name = options.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(
build.srcjar_dir, None, build.r_txt_path, options.extra_res_packages,
options.extra_r_text_files, rjava_build_options, options.srcjar_out,
custom_root_package_name, grandparent_custom_package_name,
options.extra_main_r_text_files)
build.srcjar_dir, package_for_library, build.r_txt_path,
options.extra_res_packages, options.extra_r_text_files,
rjava_build_options, options.srcjar_out, custom_root_package_name,
grandparent_custom_package_name, options.extra_main_r_text_files)

build_utils.ZipDir(build.srcjar_path, build.srcjar_dir)

Expand Down

0 comments on commit bfe48f4

Please sign in to comment.