Commit fb2a98c7 authored by Clark DuVall's avatar Clark DuVall Committed by Commit Bot

Only add WebLayer translations package for bundles

This prevents the special translations package from being added to APKs,
since it's not necessary there. In a follow up, I would like to add a
test that different languages work for both WebLayer and WebView as
bundles. It looks like that may be a bit complicated as there isn't a
way to install language splits in tests yet.

Bug: 1075704
Change-Id: Ifedd86818208320683a42b9d3218409d20d90891
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2181199
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#765403}
parent 93f489fb
......@@ -274,6 +274,11 @@ def _ParseArgs(args):
help='Path to file produced by aapt2 that maps original resource paths '
'to shortened resource paths inside the apk or module.')
input_opts.add_argument(
'--is-bundle-module',
action='store_true',
help='Whether resources are being generated for a bundle module.')
options = parser.parse_args(args)
resource_utils.HandleCommonOptions(options)
......@@ -814,36 +819,41 @@ def _HardcodeSharedLibraryDynamicAttributes(zip_path, options):
with open(os.path.join(tmp_dir, 'resources.pb')) as f:
table.ParseFromString(f.read())
# A separate top level package will be added to the resources, which
# contains only locale specific resources. The package ID of the locale
# resources is hardcoded to _SHARED_LIBRARY_HARDCODED_ID. This causes
# resources in locale splits to all get assigned
# _SHARED_LIBRARY_HARDCODED_ID as their package ID, which prevents a bug in
# shared library bundles where each split APK gets a separate dynamic ID,
# and cannot be accessed by the main APK.
translations_package = Resources_pb2.Package()
translations_package.package_id.id = _SHARED_LIBRARY_HARDCODED_ID
translations_package.package_name = table.package[
0].package_name + '_translations'
# These resources are allowed in the base resources, since they are needed
# by WebView.
allowed_resource_names = set()
if options.shared_resources_allowlist:
allowed_resource_names = set(
resource_utils.GetRTxtStringResourceNames(
options.shared_resources_allowlist))
translations_package = None
if options.is_bundle_module:
# A separate top level package will be added to the resources, which
# contains only locale specific resources. The package ID of the locale
# resources is hardcoded to _SHARED_LIBRARY_HARDCODED_ID. This causes
# resources in locale splits to all get assigned
# _SHARED_LIBRARY_HARDCODED_ID as their package ID, which prevents a bug
# in shared library bundles where each split APK gets a separate dynamic
# ID, and cannot be accessed by the main APK.
translations_package = Resources_pb2.Package()
translations_package.package_id.id = _SHARED_LIBRARY_HARDCODED_ID
translations_package.package_name = table.package[
0].package_name + '_translations'
# These resources are allowed in the base resources, since they are needed
# by WebView.
allowed_resource_names = set()
if options.shared_resources_allowlist:
allowed_resource_names = set(
resource_utils.GetRTxtStringResourceNames(
options.shared_resources_allowlist))
for package in table.package:
for _type in package.type:
for entry in _type.entry:
for config_value in entry.config_value:
_ProcessProtoValue(config_value.value)
locale_type = _SplitLocaleResourceType(_type, allowed_resource_names)
if locale_type:
translations_package.type.add().CopyFrom(locale_type)
if translations_package is not None:
locale_type = _SplitLocaleResourceType(_type, allowed_resource_names)
if locale_type:
translations_package.type.add().CopyFrom(locale_type)
table.package.add().CopyFrom(translations_package)
if translations_package is not None:
table.package.add().CopyFrom(translations_package)
with open(os.path.join(tmp_dir, 'resources.pb'), 'w') as f:
f.write(table.SerializeToString())
......
......@@ -2481,6 +2481,10 @@ if (enable_java_templates) {
_args += [ "--manifest-package=${invoker.manifest_package}" ]
}
if (defined(invoker.is_bundle_module) && invoker.is_bundle_module) {
_args += [ "--is-bundle-module" ]
}
if (defined(invoker.verify_manifest_target_name)) {
_expectations_target =
"${invoker.verify_manifest_target_name}_manifest_expectations"
......
......@@ -2530,6 +2530,7 @@ if (enable_java_templates) {
}
if (_is_bundle_module) {
is_bundle_module = true
proto_output = _proto_resources_path
if (_optimize_resources) {
optimized_proto_output = _optimized_proto_resources_path
......
......@@ -10,6 +10,7 @@ import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
......@@ -374,9 +375,12 @@ public final class WebLayerImpl extends IWebLayer.Stub {
}
Context context = ContextUtils.getApplicationContext();
// String may be missing translations, since they are loaded at a different package ID
// by default in standalone WebView.
assert !context.getResources().getResourceTypeName(id).equals("string");
try {
// String may be missing translations, since they are loaded at a different package ID
// by default in standalone WebView.
assert !context.getResources().getResourceTypeName(id).equals("string");
} catch (Resources.NotFoundException e) {
}
id &= 0x00ffffff;
id |= (0x01000000
* getPackageId(context, WebViewFactory.getLoadedPackageInfo().packageName));
......@@ -385,10 +389,14 @@ public final class WebLayerImpl extends IWebLayer.Stub {
/** Returns whether this ID is from the android system package. */
public static boolean isAndroidResource(int id) {
return ContextUtils.getApplicationContext()
.getResources()
.getResourcePackageName(id)
.equals("android");
try {
return ContextUtils.getApplicationContext()
.getResources()
.getResourcePackageName(id)
.equals("android");
} catch (Resources.NotFoundException e) {
return false;
}
}
/**
......
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