Commit e734bc5e authored by Mohamed Heikal's avatar Mohamed Heikal Committed by Commit Bot

Reland x2 "[Android] Remove most resource names in resources.arsc"

This reverts commit 65f0e20a.

Reason for reland: Fixes crbug.com/900909 and crbug.com/900993

TBR=torne@chromium.org (No changes in webview section)

Original change's description:
> [Android] Remove most resource names in resources.arsc
>
> resource names are stored in the resources.arsc file to allow for
> getIdentifier with the string name of the resource to work. This cl
> obfuscates all of those resources to one name (and thus stored only
> once) for all resources that are not accessed via getIdentifier.
>
> This improves binary size by about 200KB.
>
> Bug: 894208
>
> Change-Id: I28c440c22b90cd045f53017073fdb88c7410d530
> Reviewed-on: https://chromium-review.googlesource.com/c/1265897
> Commit-Queue: Mohamed Heikal <mheikal@chromium.org>
> Reviewed-by: Richard Coles <torne@chromium.org>
> Reviewed-by: agrieve <agrieve@chromium.org>
> Reviewed-by: Ted Choc <tedchoc@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#603229}
Bug: 894208, 900909, 900993

Change-Id: Ie1e8790b392b5cbfff6d207c11e12bed6b8cc765
Reviewed-on: https://chromium-review.googlesource.com/c/1316092Reviewed-by: default avatarMohamed Heikal <mheikal@chromium.org>
Reviewed-by: default avataragrieve <agrieve@chromium.org>
Commit-Queue: Mohamed Heikal <mheikal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605451}
parent 4e150c51
string/license_activity_title#no_obfuscate
...@@ -29,6 +29,9 @@ public class LicenseActivity extends Activity { ...@@ -29,6 +29,9 @@ public class LicenseActivity extends Activity {
String.format("content://%s.%s", packageName, LICENSES_URI_SUFFIX); String.format("content://%s.%s", packageName, LICENSES_URI_SUFFIX);
intent.setDataAndType(Uri.parse(licenseUri), LICENSES_CONTENT_TYPE); intent.setDataAndType(Uri.parse(licenseUri), LICENSES_CONTENT_TYPE);
intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addCategory(Intent.CATEGORY_DEFAULT);
// Resources are accessed via getIdentifier because resource ids in the standalone system
// webview apk have incorrect package_ids. Accessing resources via getIdentifier needs to be
// whitelisted in //android_webview/aapt2.config. see https://crbug.com/894208
final int titleId = final int titleId =
getResources().getIdentifier("license_activity_title", "string", packageName); getResources().getIdentifier("license_activity_title", "string", packageName);
if (titleId != 0) { if (titleId != 0) {
......
...@@ -329,8 +329,9 @@ def main(args): ...@@ -329,8 +329,9 @@ def main(args):
resource_infos = resource_apk.infolist() resource_infos = resource_apk.infolist()
# 1. AndroidManifest.xml # 1. AndroidManifest.xml
assert resource_infos[0].filename == 'AndroidManifest.xml' copy_resource(
copy_resource(resource_infos[0], out_dir=apk_manifest_dir) resource_apk.getinfo('AndroidManifest.xml'),
out_dir=apk_manifest_dir)
# 2. Assets # 2. Assets
if options.write_asset_list: if options.write_asset_list:
...@@ -380,7 +381,8 @@ def main(args): ...@@ -380,7 +381,8 @@ def main(args):
build_utils.AddToZipHermetic(out_apk, apk_path, data='') build_utils.AddToZipHermetic(out_apk, apk_path, data='')
# 5. Resources # 5. Resources
for info in resource_infos[1:]: for info in resource_infos:
if info.filename != 'AndroidManifest.xml':
copy_resource(info) copy_resource(info)
# 6. Java resources that should be accessible via # 6. Java resources that should be accessible via
......
...@@ -141,6 +141,17 @@ def _ParseArgs(args): ...@@ -141,6 +141,17 @@ def _ParseArgs(args):
action='store_true', action='store_true',
help='Whether to strip xml namespaces from processed ' help='Whether to strip xml namespaces from processed '
'xml resources') 'xml resources')
input_opts.add_argument(
'--resources-config-path', help='Path to aapt2 resources config file.')
input_opts.add_argument(
'--optimize-resources',
default=False,
action='store_true',
help='Whether to run the `aapt2 optimize` step on the resources.')
input_opts.add_argument(
'--unoptimized-resources-path',
help='Path to output the intermediate apk before running '
'`aapt2 optimize`.')
input_opts.add_argument( input_opts.add_argument(
'--check-resources-pkg-id', type=_PackageIdArgument, '--check-resources-pkg-id', type=_PackageIdArgument,
...@@ -306,7 +317,6 @@ def _CreateLinkApkArgs(options): ...@@ -306,7 +317,6 @@ def _CreateLinkApkArgs(options):
'--version-name', options.version_name, '--version-name', options.version_name,
'--auto-add-overlay', '--auto-add-overlay',
'--no-version-vectors', '--no-version-vectors',
'-o', options.apk_path,
] ]
for j in options.include_resources: for j in options.include_resources:
...@@ -481,7 +491,11 @@ def _CompileDeps(aapt2_path, dep_subdirs, temp_dir): ...@@ -481,7 +491,11 @@ def _CompileDeps(aapt2_path, dep_subdirs, temp_dir):
partial_path = os.path.join(partials_dir, dirname + '.zip') partial_path = os.path.join(partials_dir, dirname + '.zip')
compile_command = (partial_compile_command + compile_command = (partial_compile_command +
['--dir', directory, '-o', partial_path]) ['--dir', directory, '-o', partial_path])
build_utils.CheckOutput(compile_command) build_utils.CheckOutput(
compile_command,
stderr_filter=lambda output:
build_utils.FilterLines(
output, r'ignoring configuration .* for styleable'))
# Sorting the files in the partial ensures deterministic output from the # Sorting the files in the partial ensures deterministic output from the
# aapt2 link step which uses order of files in the partial. # aapt2 link step which uses order of files in the partial.
...@@ -541,7 +555,15 @@ def _PackageApk(options, dep_subdirs, temp_dir, gen_dir, r_txt_path): ...@@ -541,7 +555,15 @@ def _PackageApk(options, dep_subdirs, temp_dir, gen_dir, r_txt_path):
for directory in dep_subdirs: for directory in dep_subdirs:
renamed_paths.update(_MoveImagesToNonMdpiFolders(directory)) renamed_paths.update(_MoveImagesToNonMdpiFolders(directory))
if options.optimize_resources:
if options.unoptimized_resources_path:
unoptimized_apk_path = options.unoptimized_resources_path
else:
unoptimized_apk_path = os.path.join(gen_dir, 'intermediate.ap_')
else:
unoptimized_apk_path = options.apk_path
link_command = _CreateLinkApkArgs(options) link_command = _CreateLinkApkArgs(options)
link_command += ['-o', unoptimized_apk_path]
link_command += ['--output-text-symbols', r_txt_path] link_command += ['--output-text-symbols', r_txt_path]
# TODO(digit): Is this below actually required for R.txt generation? # TODO(digit): Is this below actually required for R.txt generation?
link_command += ['--java', gen_dir] link_command += ['--java', gen_dir]
...@@ -557,10 +579,69 @@ def _PackageApk(options, dep_subdirs, temp_dir, gen_dir, r_txt_path): ...@@ -557,10 +579,69 @@ def _PackageApk(options, dep_subdirs, temp_dir, gen_dir, r_txt_path):
# Also creates R.txt # Also creates R.txt
build_utils.CheckOutput( build_utils.CheckOutput(
link_command, print_stdout=False, print_stderr=False) link_command, print_stdout=False, print_stderr=False)
if options.optimize_resources:
_OptimizeApk(options, temp_dir, unoptimized_apk_path, r_txt_path)
_CreateResourceInfoFile( _CreateResourceInfoFile(
renamed_paths, options.apk_info_path, options.dependencies_res_zips) renamed_paths, options.apk_info_path, options.dependencies_res_zips)
def _OptimizeApk(options, temp_dir, unoptimized_apk_path, r_txt_path):
"""Optimize intermediate .ap_ file with aapt2.
Args:
options: The command-line options tuple. E.g. the generated apk
will be written to |options.apk_path|.
temp_dir: A temporary directory.
unoptimized_apk_path: path of the apk to optimize.
r_txt_path: path to the R.txt file of the unoptimized apk.
"""
# Resources of type ID are references to UI elements/views. They are used by
# UI automation testing frameworks. They are kept in so that they dont break
# tests, even though they may not actually be used during runtime. See
# https://crbug.com/900993
id_resources = _ExtractIdResources(r_txt_path)
gen_config_path = os.path.join(temp_dir, 'aapt2.config')
if options.resources_config_path:
shutil.copyfile(options.resources_config_path, gen_config_path)
with open(gen_config_path, 'a+') as config:
for resource in id_resources:
config.write('{}#no_obfuscate\n'.format(resource))
# Optimize the resources.arsc file by obfuscating resource names and only
# allow usage via R.java constant.
optimize_command = [
options.aapt2_path,
'optimize',
'--enable-resource-obfuscation',
'-o',
options.apk_path,
'--resources-config-path',
gen_config_path,
unoptimized_apk_path,
]
build_utils.CheckOutput(
optimize_command, print_stdout=False, print_stderr=False)
def _ExtractIdResources(rtxt_path):
"""Extract resources of type ID from the R.txt file
Args:
rtxt_path: Path to R.txt file with all the resources
Returns:
List of id resources in the form of id/<resource_name>
"""
id_resources = []
with open(rtxt_path) as rtxt:
for line in rtxt:
if ' id ' in line:
resource_name = line.split()[2]
id_resources.append('id/{}'.format(resource_name))
return id_resources
def _WriteFinalRTxtFile(options, aapt_r_txt_path): def _WriteFinalRTxtFile(options, aapt_r_txt_path):
"""Determine final R.txt and return its location. """Determine final R.txt and return its location.
...@@ -645,6 +726,7 @@ def main(args): ...@@ -645,6 +726,7 @@ def main(args):
options.srcjar_out, options.srcjar_out,
options.proguard_file, options.proguard_file,
options.proguard_file_main_dex, options.proguard_file_main_dex,
options.unoptimized_resources_path,
] ]
output_paths = [x for x in possible_output_paths if x] output_paths = [x for x in possible_output_paths if x]
...@@ -658,6 +740,7 @@ def main(args): ...@@ -658,6 +740,7 @@ def main(args):
str(options.png_to_webp), str(options.png_to_webp),
str(options.support_zh_hk), str(options.support_zh_hk),
str(options.no_xml_namespaces), str(options.no_xml_namespaces),
str(options.optimize_resources),
] ]
input_strings.extend(_CreateLinkApkArgs(options)) input_strings.extend(_CreateLinkApkArgs(options))
...@@ -674,6 +757,7 @@ def main(args): ...@@ -674,6 +757,7 @@ def main(args):
options.android_manifest, options.android_manifest,
debug_temp_resources_dir, debug_temp_resources_dir,
options.shared_resources_whitelist, options.shared_resources_whitelist,
options.resources_config_path,
] ]
possible_input_paths += options.include_resources possible_input_paths += options.include_resources
input_paths = [x for x in possible_input_paths if x] input_paths = [x for x in possible_input_paths if x]
......
...@@ -1981,14 +1981,15 @@ if (enable_java_templates) { ...@@ -1981,14 +1981,15 @@ if (enable_java_templates) {
outputs = [] outputs = []
_android_aapt_path = android_default_aapt_path _android_aapt_path = android_default_aapt_path
if (_proto_format) {
_android_aapt2_path = android_sdk_tools_bundle_aapt2 _android_aapt2_path = android_sdk_tools_bundle_aapt2
if (_proto_format) {
depfile = "$target_gen_dir/${invoker.target_name}_3.d" depfile = "$target_gen_dir/${invoker.target_name}_3.d"
} }
inputs = [ inputs = [
invoker.build_config, invoker.build_config,
_android_aapt_path, _android_aapt_path,
_android_aapt2_path,
] ]
_rebased_build_config = rebase_path(invoker.build_config, root_build_dir) _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)
...@@ -1999,19 +2000,13 @@ if (enable_java_templates) { ...@@ -1999,19 +2000,13 @@ if (enable_java_templates) {
"--include-resources=@FileArg($_rebased_build_config:android:sdk_jars)", "--include-resources=@FileArg($_rebased_build_config:android:sdk_jars)",
"--aapt-path", "--aapt-path",
rebase_path(_android_aapt_path, root_build_dir), rebase_path(_android_aapt_path, root_build_dir),
"--aapt2-path",
rebase_path(_android_aapt2_path, root_build_dir),
"--dependencies-res-zips=@FileArg($_rebased_build_config:resources:dependency_zips)", "--dependencies-res-zips=@FileArg($_rebased_build_config:resources:dependency_zips)",
"--extra-res-packages=@FileArg($_rebased_build_config:resources:extra_package_names)", "--extra-res-packages=@FileArg($_rebased_build_config:resources:extra_package_names)",
"--extra-r-text-files=@FileArg($_rebased_build_config:resources:extra_r_text_files)", "--extra-r-text-files=@FileArg($_rebased_build_config:resources:extra_r_text_files)",
] ]
if (_proto_format) {
inputs += [ _android_aapt2_path ]
args += [
"--aapt2-path",
rebase_path(_android_aapt2_path, root_build_dir),
]
}
inputs += [ invoker.android_manifest ] inputs += [ invoker.android_manifest ]
args += [ args += [
"--android-manifest", "--android-manifest",
...@@ -2048,6 +2043,25 @@ if (enable_java_templates) { ...@@ -2048,6 +2043,25 @@ if (enable_java_templates) {
] ]
} }
if (defined(invoker.optimize_resources) && invoker.optimize_resources) {
args += [ "--optimize-resources" ]
if (defined(invoker.resources_config_path)) {
inputs += [ invoker.resources_config_path ]
args += [
"--resources-config-path",
rebase_path(invoker.resources_config_path, root_build_dir),
]
}
if (defined(invoker.unoptimized_resources_path)) {
args += [
"--unoptimized-resources-path",
rebase_path(invoker.unoptimized_resources_path, root_build_dir),
]
outputs += [ invoker.unoptimized_resources_path ]
}
}
# Useful to have android:debuggable in the manifest even for Release # Useful to have android:debuggable in the manifest even for Release
# builds. Just omit it for officai # builds. Just omit it for officai
if (debuggable_apks) { if (debuggable_apks) {
...@@ -2501,6 +2515,13 @@ if (enable_java_templates) { ...@@ -2501,6 +2515,13 @@ if (enable_java_templates) {
get_label_info(_incremental_compile_resources_target_name, get_label_info(_incremental_compile_resources_target_name,
"target_gen_dir") + "/AndroidManifest.xml" "target_gen_dir") + "/AndroidManifest.xml"
if (defined(invoker.unoptimized_resources_path)) {
_incremental_packaged_resources_path =
invoker.unoptimized_resources_path
} else {
_incremental_packaged_resources_path = invoker.packaged_resources_path
}
_rebased_build_config = _rebased_build_config =
rebase_path(invoker.assets_build_config, root_build_dir) rebase_path(invoker.assets_build_config, root_build_dir)
...@@ -2511,7 +2532,7 @@ if (enable_java_templates) { ...@@ -2511,7 +2532,7 @@ if (enable_java_templates) {
inputs = [ inputs = [
_android_manifest, _android_manifest,
invoker.assets_build_config, invoker.assets_build_config,
invoker.packaged_resources_path, _incremental_packaged_resources_path,
] ]
outputs = [ outputs = [
# Output the non-compiled manifest for easy debugging (as opposed to # Output the non-compiled manifest for easy debugging (as opposed to
...@@ -2526,7 +2547,7 @@ if (enable_java_templates) { ...@@ -2526,7 +2547,7 @@ if (enable_java_templates) {
"--out-manifest", "--out-manifest",
rebase_path(_incremental_android_manifest, root_build_dir), rebase_path(_incremental_android_manifest, root_build_dir),
"--in-apk", "--in-apk",
rebase_path(invoker.packaged_resources_path, root_build_dir), rebase_path(_incremental_packaged_resources_path, root_build_dir),
"--out-apk", "--out-apk",
rebase_path(_incremental_compiled_resources_path, root_build_dir), rebase_path(_incremental_compiled_resources_path, root_build_dir),
"--aapt-path", "--aapt-path",
......
...@@ -1885,6 +1885,10 @@ if (enable_java_templates) { ...@@ -1885,6 +1885,10 @@ if (enable_java_templates) {
# uncompressed in the APK. Must be unset or true if load_library_from_apk # uncompressed in the APK. Must be unset or true if load_library_from_apk
# is set to true. # is set to true.
# uncompress_dex: Store final .dex files uncompressed in the apk. # uncompress_dex: Store final .dex files uncompressed in the apk.
# optimize_resources: True if resource names should be stripped from the
# resources.arsc file in the apk or module.
# resources_config_path: Path to the aapt2 optimize config file that tags
# resources with acceptable/non-acceptable optimizations.
template("android_apk_or_module") { template("android_apk_or_module") {
forward_variables_from(invoker, [ "testonly" ]) forward_variables_from(invoker, [ "testonly" ])
...@@ -2125,6 +2129,13 @@ if (enable_java_templates) { ...@@ -2125,6 +2129,13 @@ if (enable_java_templates) {
_android_sdk_dep = "//third_party/android_tools:android_sdk_java" _android_sdk_dep = "//third_party/android_tools:android_sdk_java"
} }
_optimize_resources =
defined(invoker.optimize_resources) && invoker.optimize_resources
if (_optimize_resources) {
_unoptimized_resources_path =
"$target_out_dir/$_template_name.unoptimized.ap_"
}
_compile_resources_target = "${_template_name}__compile_resources" _compile_resources_target = "${_template_name}__compile_resources"
_compile_resources_rtxt_out = _compile_resources_rtxt_out =
"${target_gen_dir}/${_compile_resources_target}_R.txt" "${target_gen_dir}/${_compile_resources_target}_R.txt"
...@@ -2137,6 +2148,7 @@ if (enable_java_templates) { ...@@ -2137,6 +2148,7 @@ if (enable_java_templates) {
"aapt_locale_whitelist", "aapt_locale_whitelist",
"resource_blacklist_regex", "resource_blacklist_regex",
"resource_blacklist_exceptions", "resource_blacklist_exceptions",
"resources_config_path",
"png_to_webp", "png_to_webp",
"no_xml_namespaces", "no_xml_namespaces",
]) ])
...@@ -2154,6 +2166,10 @@ if (enable_java_templates) { ...@@ -2154,6 +2166,10 @@ if (enable_java_templates) {
proguard_file_main_dex = _generated_proguard_main_dex_config proguard_file_main_dex = _generated_proguard_main_dex_config
} }
output = _packaged_resources_path output = _packaged_resources_path
if (_optimize_resources) {
optimize_resources = true
unoptimized_resources_path = _unoptimized_resources_path
}
build_config = _build_config build_config = _build_config
deps = _deps + [ deps = _deps + [
...@@ -2590,6 +2606,9 @@ if (enable_java_templates) { ...@@ -2590,6 +2606,9 @@ if (enable_java_templates) {
if (_incremental_allowed) { if (_incremental_allowed) {
android_manifest = _android_manifest android_manifest = _android_manifest
base_path = _base_path base_path = _base_path
if (_optimize_resources) {
unoptimized_resources_path = _unoptimized_resources_path
}
} }
# Incremental apk does not use native libs nor final dex. # Incremental apk does not use native libs nor final dex.
...@@ -2863,6 +2882,7 @@ if (enable_java_templates) { ...@@ -2863,6 +2882,7 @@ if (enable_java_templates) {
"never_incremental", "never_incremental",
"no_build_hooks", "no_build_hooks",
"no_xml_namespaces", "no_xml_namespaces",
"optimize_resources",
"png_to_webp", "png_to_webp",
"post_process_package_resources_script", "post_process_package_resources_script",
"product_version_resources_dep", "product_version_resources_dep",
...@@ -2871,6 +2891,7 @@ if (enable_java_templates) { ...@@ -2871,6 +2891,7 @@ if (enable_java_templates) {
"proguard_jar_path", "proguard_jar_path",
"resource_blacklist_regex", "resource_blacklist_regex",
"resource_blacklist_exceptions", "resource_blacklist_exceptions",
"resources_config_path",
"secondary_abi_loadable_modules", "secondary_abi_loadable_modules",
"secondary_abi_shared_libraries", "secondary_abi_shared_libraries",
"secondary_native_lib_placeholders", "secondary_native_lib_placeholders",
...@@ -2961,6 +2982,7 @@ if (enable_java_templates) { ...@@ -2961,6 +2982,7 @@ if (enable_java_templates) {
"native_lib_version_rule", "native_lib_version_rule",
"negative_main_dex_globs", "negative_main_dex_globs",
"no_xml_namespaces", "no_xml_namespaces",
"optimize_resources",
"png_to_webp", "png_to_webp",
"product_version_resources_dep", "product_version_resources_dep",
"proguard_configs", "proguard_configs",
...@@ -2968,6 +2990,7 @@ if (enable_java_templates) { ...@@ -2968,6 +2990,7 @@ if (enable_java_templates) {
"proguard_jar_path", "proguard_jar_path",
"resource_blacklist_regex", "resource_blacklist_regex",
"resource_blacklist_exceptions", "resource_blacklist_exceptions",
"resources_config_path",
"secondary_abi_loadable_modules", "secondary_abi_loadable_modules",
"secondary_abi_shared_libraries", "secondary_abi_shared_libraries",
"secondary_native_lib_placeholders", "secondary_native_lib_placeholders",
......
...@@ -111,6 +111,8 @@ template("chrome_public_common_apk_or_module_tmpl") { ...@@ -111,6 +111,8 @@ template("chrome_public_common_apk_or_module_tmpl") {
# Use zh-TW strings for zh-HK (https://crbug.com/780847). # Use zh-TW strings for zh-HK (https://crbug.com/780847).
support_zh_hk = true support_zh_hk = true
optimize_resources = true
if (defined(shared_libraries) && shared_libraries != []) { if (defined(shared_libraries) && shared_libraries != []) {
_native_lib_file = _native_lib_file =
rebase_path("$root_gen_dir/CHROME_VERSION.json", root_out_dir) rebase_path("$root_gen_dir/CHROME_VERSION.json", root_out_dir)
...@@ -231,6 +233,9 @@ template("monochrome_public_common_apk_or_module_tmpl") { ...@@ -231,6 +233,9 @@ template("monochrome_public_common_apk_or_module_tmpl") {
# Webview supports all locales (has no omitted ones). # Webview supports all locales (has no omitted ones).
aapt_locale_whitelist = locales aapt_locale_whitelist = locales
# Resources config for blocklisting resource names from obfuscation
resources_config_path = "//android_webview/aapt2.config"
if (!defined(invoker.target_type) || invoker.target_type == "android_apk") { if (!defined(invoker.target_type) || invoker.target_type == "android_apk") {
# Incremental install doesn't work for monochrome. See crbug.com/663492. # Incremental install doesn't work for monochrome. See crbug.com/663492.
never_incremental = true never_incremental = true
......
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