Commit 697c9ab8 authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

Android: Compile .proto and .arsc resources at the same time

Saves a couple of seconds of build time for base bundle modules,
and simplifies the GN logic for resources.

Also fixes chrome_apk using unoptimized arsc file when lemon is
enabled (broken by 2e296d82).

Simplifies temp file usage in compile_resources.py by having all
outputs go to the staging directory.

Removes cruft:
 * Unused --no-compress flag
 * Unnecessary "Touch()" of R.txt

Bug: 950233
Change-Id: Ida77380e8038002435ec3d320105d9feaa3c2136
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1568568
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarTibor Goldschwendt <tiborg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#653275}
parent 83a8ff8f
...@@ -15,6 +15,7 @@ This will crunch images with aapt2. ...@@ -15,6 +15,7 @@ This will crunch images with aapt2.
import argparse import argparse
import collections import collections
import contextlib import contextlib
import filecmp
import multiprocessing.pool import multiprocessing.pool
import os import os
import re import re
...@@ -64,20 +65,20 @@ def _ParseArgs(args): ...@@ -64,20 +65,20 @@ def _ParseArgs(args):
input_opts.add_argument( input_opts.add_argument(
'--aapt2-path', required=True, help='Path to the Android aapt2 tool.') '--aapt2-path', required=True, help='Path to the Android aapt2 tool.')
input_opts.add_argument('--android-manifest', required=True,
help='AndroidManifest.xml path')
input_opts.add_argument( input_opts.add_argument(
'--android-manifest', required=True, help='AndroidManifest.xml path.')
group = input_opts.add_mutually_exclusive_group()
group.add_argument(
'--shared-resources', '--shared-resources',
action='store_true', action='store_true',
help='Make all resources in R.java non-final and allow the resource IDs ' help='Make all resources in R.java non-final and allow the resource IDs '
'to be reset to a different package index when the apk is loaded by ' 'to be reset to a different package index when the apk is loaded by '
'another application at runtime.') 'another application at runtime.')
group.add_argument(
input_opts.add_argument(
'--app-as-shared-lib', '--app-as-shared-lib',
action='store_true', action='store_true',
help='Same as --shared-resources, but also ensures all resource IDs are ' help='Same as --shared-resources, but also ensures all resource IDs are '
'directly usable from the APK loaded as an application.') 'directly usable from the APK loaded as an application.')
input_opts.add_argument( input_opts.add_argument(
'--package-id', '--package-id',
...@@ -94,7 +95,8 @@ def _ParseArgs(args): ...@@ -94,7 +95,8 @@ def _ParseArgs(args):
help='Package name that will be used to determine package ID.') help='Package name that will be used to determine package ID.')
input_opts.add_argument( input_opts.add_argument(
'--arsc-package-name', help='Package name to use for resources.arsc file') '--arsc-package-name',
help='Package name to use for resources.arsc file.')
input_opts.add_argument( input_opts.add_argument(
'--shared-resources-whitelist', '--shared-resources-whitelist',
...@@ -112,26 +114,21 @@ def _ParseArgs(args): ...@@ -112,26 +114,21 @@ def _ParseArgs(args):
input_opts.add_argument( input_opts.add_argument(
'--use-resource-ids-path', '--use-resource-ids-path',
help='Use resource IDs generated by aapt --emit-ids') help='Use resource IDs generated by aapt --emit-ids.')
input_opts.add_argument('--proto-format', action='store_true',
help='Compile resources to protocol buffer format.')
input_opts.add_argument('--support-zh-hk', action='store_true', input_opts.add_argument(
help='Use zh-rTW resources for zh-rHK.') '--support-zh-hk',
action='store_true',
help='Use zh-rTW resources for zh-rHK.')
input_opts.add_argument('--debuggable', input_opts.add_argument(
action='store_true', '--debuggable',
help='Whether to add android:debuggable="true"') action='store_true',
help='Whether to add android:debuggable="true".')
input_opts.add_argument('--version-code', help='Version code for apk.') input_opts.add_argument('--version-code', help='Version code for apk.')
input_opts.add_argument('--version-name', help='Version name for apk.') input_opts.add_argument('--version-name', help='Version name for apk.')
input_opts.add_argument(
'--no-compress',
help='disables compression for the given comma-separated list of '
'extensions')
input_opts.add_argument( input_opts.add_argument(
'--locale-whitelist', '--locale-whitelist',
default='[]', default='[]',
...@@ -154,21 +151,25 @@ def _ParseArgs(args): ...@@ -154,21 +151,25 @@ def _ParseArgs(args):
input_opts.add_argument('--webp-binary', default='', input_opts.add_argument('--webp-binary', default='',
help='Path to the cwebp binary.') help='Path to the cwebp binary.')
input_opts.add_argument('--no-xml-namespaces',
action='store_true',
help='Whether to strip xml namespaces from processed '
'xml resources')
input_opts.add_argument( input_opts.add_argument(
'--resources-config-path', help='Path to aapt2 resources config file.') '--no-xml-namespaces',
action='store_true',
help='Whether to strip xml namespaces from processed xml resources.')
output_opts.add_argument('--arsc-path', help='Apk output for arsc format.')
output_opts.add_argument('--proto-path', help='Apk output for proto format.')
group = input_opts.add_mutually_exclusive_group()
group.add_argument(
'--optimized-arsc-path',
help='Output for `aapt2 optimize` for arsc format (enables the step).')
group.add_argument(
'--optimized-proto-path',
help='Output for `aapt2 optimize` for proto format (enables the step).')
input_opts.add_argument( input_opts.add_argument(
'--optimized-resources-path', '--resources-config-path', help='Path to aapt2 resources config file.')
help='Output for `aapt2 optimize` (also enables the step).')
output_opts.add_argument('--apk-path', required=True,
help='Path to output (partial) apk.')
output_opts.add_argument('--apk-info-path', required=True, output_opts.add_argument(
help='Path to output info file for the partial apk.') '--info-path', help='Path to output info file for the partial apk.')
output_opts.add_argument('--srcjar-out', output_opts.add_argument('--srcjar-out',
help='Path to srcjar to contain generated R.java.') help='Path to srcjar to contain generated R.java.')
...@@ -176,17 +177,15 @@ def _ParseArgs(args): ...@@ -176,17 +177,15 @@ def _ParseArgs(args):
output_opts.add_argument('--r-text-out', output_opts.add_argument('--r-text-out',
help='Path to store the generated R.txt file.') help='Path to store the generated R.txt file.')
output_opts.add_argument('--proguard-file', output_opts.add_argument(
help='Path to proguard.txt generated file') '--proguard-file', help='Path to proguard.txt generated file.')
output_opts.add_argument( output_opts.add_argument(
'--proguard-file-main-dex', '--proguard-file-main-dex',
help='Path to proguard.txt generated file for main dex') help='Path to proguard.txt generated file for main dex.')
output_opts.add_argument( output_opts.add_argument(
'--emit-ids-out', '--emit-ids-out', help='Path to file produced by aapt2 --emit-ids.')
help=
'Path to file produced by aapt2 --emit-ids (for use with --stable-ids)')
options = parser.parse_args(args) options = parser.parse_args(args)
...@@ -198,9 +197,12 @@ def _ParseArgs(args): ...@@ -198,9 +197,12 @@ def _ParseArgs(args):
options.resource_blacklist_exceptions = build_utils.ParseGnList( options.resource_blacklist_exceptions = build_utils.ParseGnList(
options.resource_blacklist_exceptions) options.resource_blacklist_exceptions)
if options.shared_resources and options.app_as_shared_lib: if options.optimized_proto_path and not options.proto_path:
raise Exception('Only one of --app-as-shared-lib or --shared-resources ' # We could write to a temp file, but it's simpler to require it.
'can be used.') parser.error('--optimized-proto-path requires --proto-path')
if not options.arsc_path and not options.proto_path:
parser.error('One of --arsc-path or --proto-path is required.')
if options.package_name_to_id_mapping: if options.package_name_to_id_mapping:
package_names_list = build_utils.ParseGnList( package_names_list = build_utils.ParseGnList(
...@@ -372,54 +374,6 @@ def _PackageIdFromOptions(options): ...@@ -372,54 +374,6 @@ def _PackageIdFromOptions(options):
return package_id return package_id
def _CreateLinkApkArgs(options):
"""Create command-line arguments list to invoke 'aapt2 link'.
Args:
options: The command-line options tuple.
Returns:
A list of strings corresponding to the command-line invokation for
the command, matching the arguments from |options|.
"""
link_command = [
options.aapt2_path,
'link',
'--version-code', options.version_code,
'--version-name', options.version_name,
'--auto-add-overlay',
'--no-version-vectors',
]
for j in options.include_resources:
link_command += ['-I', j]
if options.proguard_file:
link_command += ['--proguard', options.proguard_file]
if options.proguard_file_main_dex:
link_command += ['--proguard-main-dex', options.proguard_file_main_dex]
if options.emit_ids_out:
link_command += ['--emit-ids', options.emit_ids_out]
if options.no_compress:
for ext in options.no_compress.split(','):
link_command += ['-0', ext]
# Note: only one of --proto-format, --shared-lib or --app-as-shared-lib
# can be used with recent versions of aapt2.
if options.proto_format:
link_command.append('--proto-format')
elif options.shared_resources:
link_command.append('--shared-lib')
if options.no_xml_namespaces:
link_command.append('--no-xml-namespaces')
package_id = _PackageIdFromOptions(options)
if package_id is not None:
link_command += ['--package-id', package_id, '--allow-reserved-package-id']
return link_command
def _FixManifest(options, temp_dir): def _FixManifest(options, temp_dir):
"""Fix the APK's AndroidManifest.xml. """Fix the APK's AndroidManifest.xml.
...@@ -433,7 +387,7 @@ def _FixManifest(options, temp_dir): ...@@ -433,7 +387,7 @@ def _FixManifest(options, temp_dir):
Returns: Returns:
Tuple of: Tuple of:
* Manifest path within |temp_dir|. * Manifest path within |temp_dir|.
* Original package_name (if different from arsc_package_name). * Original package_name.
""" """
def maybe_extract_version(j): def maybe_extract_version(j):
try: try:
...@@ -582,8 +536,7 @@ def _CompileDeps(aapt2_path, dep_subdirs, temp_dir): ...@@ -582,8 +536,7 @@ def _CompileDeps(aapt2_path, dep_subdirs, temp_dir):
return partials return partials
def _CreateResourceInfoFile( def _CreateResourceInfoFile(renamed_paths, info_path, dependencies_res_zips):
renamed_paths, apk_info_path, dependencies_res_zips):
lines = set() lines = set()
for zip_file in dependencies_res_zips: for zip_file in dependencies_res_zips:
zip_info_file_path = zip_file + '.info' zip_info_file_path = zip_file + '.info'
...@@ -592,7 +545,7 @@ def _CreateResourceInfoFile( ...@@ -592,7 +545,7 @@ def _CreateResourceInfoFile(
lines.update(zip_info_file.readlines()) lines.update(zip_info_file.readlines())
for dest, source in renamed_paths.iteritems(): for dest, source in renamed_paths.iteritems():
lines.add('Rename:{},{}\n'.format(dest, source)) lines.add('Rename:{},{}\n'.format(dest, source))
with build_utils.AtomicOutput(apk_info_path) as info_file: with open(info_path, 'w') as info_file:
info_file.writelines(sorted(lines)) info_file.writelines(sorted(lines))
...@@ -661,19 +614,15 @@ def _RemoveUnwantedLocalizedStrings(dep_subdirs, options): ...@@ -661,19 +614,15 @@ def _RemoveUnwantedLocalizedStrings(dep_subdirs, options):
path, lambda x: x not in shared_names_whitelist) path, lambda x: x not in shared_names_whitelist)
def _PackageApk(options, dep_subdirs, temp_dir, gen_dir, r_txt_path): def _PackageApk(options, build):
"""Compile resources with aapt2 and generate intermediate .ap_ file. """Compile and link resources with aapt2.
Args: Args:
options: The command-line options tuple. E.g. the generated apk options: The command-line options.
will be written to |options.apk_path|. build: BuildContext object.
dep_subdirs: The list of directories where dependency resource zips
were extracted (its content will be altered by this function).
temp_dir: A temporary directory.
gen_dir: Another temp directory where some intermediate files are
generated.
r_txt_path: The path where the R.txt file will written to.
""" """
dep_subdirs = resource_utils.ExtractDeps(options.dependencies_res_zips,
build.deps_dir)
renamed_paths = dict() renamed_paths = dict()
renamed_paths.update(_DuplicateZhResources(dep_subdirs)) renamed_paths.update(_DuplicateZhResources(dep_subdirs))
renamed_paths.update(_RenameLocaleResourceDirs(dep_subdirs)) renamed_paths.update(_RenameLocaleResourceDirs(dep_subdirs))
...@@ -698,47 +647,89 @@ def _PackageApk(options, dep_subdirs, temp_dir, gen_dir, r_txt_path): ...@@ -698,47 +647,89 @@ 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))
link_command = _CreateLinkApkArgs(options) _CreateResourceInfoFile(renamed_paths, build.info_path,
# TODO(digit): Is this below actually required for R.txt generation? options.dependencies_res_zips)
link_command += ['--java', gen_dir]
fixed_manifest, orig_package = _FixManifest(options, temp_dir) link_command = [
options.aapt2_path,
'link',
'--version-code',
options.version_code,
'--version-name',
options.version_name,
'--auto-add-overlay',
'--no-version-vectors',
]
for j in options.include_resources:
link_command += ['-I', j]
if options.proguard_file:
link_command += ['--proguard', build.proguard_path]
if options.proguard_file_main_dex:
link_command += ['--proguard-main-dex', build.proguard_main_dex_path]
if options.emit_ids_out:
link_command += ['--emit-ids', build.emit_ids_path]
if options.r_text_in:
shutil.copyfile(options.r_text_in, build.r_txt_path)
else:
link_command += ['--output-text-symbols', build.r_txt_path]
# Note: only one of --proto-format, --shared-lib or --app-as-shared-lib
# can be used with recent versions of aapt2.
if options.shared_resources and not options.proto_path:
link_command.append('--shared-lib')
if options.no_xml_namespaces:
link_command.append('--no-xml-namespaces')
package_id = _PackageIdFromOptions(options)
if package_id is not None:
link_command += ['--package-id', package_id, '--allow-reserved-package-id']
fixed_manifest, orig_package = _FixManifest(options, build.temp_dir)
link_command += [ link_command += [
'--manifest', fixed_manifest, '--rename-manifest-package', orig_package '--manifest', fixed_manifest, '--rename-manifest-package', orig_package
] ]
partials = _CompileDeps(options.aapt2_path, dep_subdirs, temp_dir) # Creates a .zip with AndroidManifest.xml, resources.arsc, res/*
# Also creates R.txt
if options.use_resource_ids_path:
_CreateStableIdsFile(options.use_resource_ids_path, build.stable_ids_path,
orig_package)
link_command += ['--stable-ids', build.stable_ids_path]
partials = _CompileDeps(options.aapt2_path, dep_subdirs, build.temp_dir)
for partial in partials: for partial in partials:
link_command += ['-R', partial] link_command += ['-R', partial]
# Creates a .zip with AndroidManifest.xml, resources.arsc, res/* if options.proto_path:
# Also creates R.txt link_command += ['--proto-format', '-o', build.proto_path]
with build_utils.AtomicOutput(options.apk_path) as unoptimized, \ else:
build_utils.AtomicOutput(r_txt_path) as r_txt, \ link_command += ['-o', build.arsc_path]
_MaybeCreateStableIdsFile(options) as stable_ids:
if stable_ids: build_utils.CheckOutput(link_command, print_stdout=False, print_stderr=False)
link_command += ['--stable-ids', stable_ids.name]
link_command += ['-o', unoptimized.name]
link_command += ['--output-text-symbols', r_txt.name]
build_utils.CheckOutput(
link_command, print_stdout=False, print_stderr=False)
if options.optimized_resources_path: if options.proto_path and options.arsc_path:
with build_utils.AtomicOutput(options.optimized_resources_path) as opt: build_utils.CheckOutput([
_OptimizeApk(opt.name, options, temp_dir, unoptimized.name, r_txt.name) options.aapt2_path, 'convert', '-o', build.arsc_path, build.proto_path
])
_CreateResourceInfoFile( if options.optimized_proto_path:
renamed_paths, options.apk_info_path, options.dependencies_res_zips) _OptimizeApk(build.optimized_proto_path, options, build.temp_dir,
build.proto_path, build.r_txt_path)
elif options.optimized_arsc_path:
_OptimizeApk(build.optimized_arsc_path, options, build.temp_dir,
build.arsc_path, build.r_txt_path)
def _OptimizeApk(output, options, temp_dir, unoptimized_apk_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.
Args: Args:
output: Path to write to. output: Path to write to.
options: The command-line options. options: The command-line options.
temp_dir: A temporary directory. temp_dir: A temporary directory.
unoptimized_apk_path: path of the apk to optimize. unoptimized_path: path of the apk to optimize.
r_txt_path: path to the R.txt file of the unoptimized apk. 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 # Resources of type ID are references to UI elements/views. They are used by
...@@ -763,7 +754,7 @@ def _OptimizeApk(output, options, temp_dir, unoptimized_apk_path, r_txt_path): ...@@ -763,7 +754,7 @@ def _OptimizeApk(output, options, temp_dir, unoptimized_apk_path, r_txt_path):
output, output,
'--resources-config-path', '--resources-config-path',
gen_config_path, gen_config_path,
unoptimized_apk_path, unoptimized_path,
] ]
build_utils.CheckOutput( build_utils.CheckOutput(
optimize_command, print_stdout=False, print_stderr=False) optimize_command, print_stdout=False, print_stderr=False)
...@@ -787,7 +778,7 @@ def _ExtractIdResources(rtxt_path): ...@@ -787,7 +778,7 @@ def _ExtractIdResources(rtxt_path):
@contextlib.contextmanager @contextlib.contextmanager
def _MaybeCreateStableIdsFile(options): def _CreateStableIdsFile(in_path, out_path, package_name):
"""Transforms a file generated by --emit-ids from another package. """Transforms a file generated by --emit-ids from another package.
--stable-ids is generally meant to be used by different versions of the same --stable-ids is generally meant to be used by different versions of the same
...@@ -797,55 +788,35 @@ def _MaybeCreateStableIdsFile(options): ...@@ -797,55 +788,35 @@ def _MaybeCreateStableIdsFile(options):
Note: This will fail if the package ID of the resources in Note: This will fail if the package ID of the resources in
|options.use_resource_ids_path| does not match the package ID of the |options.use_resource_ids_path| does not match the package ID of the
resources being linked. resources being linked.
Args:
options: The command-line options
Yields:
Path to the transformed resource IDs file (lines formatted like
package:type/name = 0xPPTTEEEE) or None
""" """
if options.use_resource_ids_path: with open(in_path) as stable_ids_file:
package_name = options.package_name with open(out_path, 'w') as output_ids_file:
if not package_name: output_stable_ids = re.sub(
package_name = resource_utils.ExtractPackageFromManifest( r'^.*?:',
options.android_manifest) package_name + ':',
with open(options.use_resource_ids_path) as stable_ids_file: stable_ids_file.read(),
with tempfile.NamedTemporaryFile() as output_ids_file: flags=re.MULTILINE)
output_stable_ids = re.sub( output_ids_file.write(output_stable_ids)
r'^.*?:',
package_name + ':',
stable_ids_file.read(), def _WriteOutputs(options, build):
flags=re.MULTILINE) possible_outputs = [
output_ids_file.write(output_stable_ids) (options.srcjar_out, build.srcjar_path),
output_ids_file.flush() (options.r_text_out, build.r_txt_path),
yield output_ids_file (options.arsc_path, build.arsc_path),
else: (options.proto_path, build.proto_path),
yield None (options.optimized_arsc_path, build.optimized_arsc_path),
(options.optimized_proto_path, build.optimized_proto_path),
(options.proguard_file, build.proguard_path),
def _WriteFinalRTxtFile(options, aapt_r_txt_path): (options.proguard_file_main_dex, build.proguard_main_dex_path),
"""Determine final R.txt and return its location. (options.emit_ids_out, build.emit_ids_path),
(options.info_path, build.info_path),
This handles --r-text-in and --r-text-out options at the same time. ]
Args:
options: The command-line options tuple.
aapt_r_txt_path: The path to the R.txt generated by aapt.
Returns:
Path to the final R.txt file.
"""
if options.r_text_in:
r_txt_file = options.r_text_in
else:
# When an empty res/ directory is passed, aapt does not write an R.txt.
r_txt_file = aapt_r_txt_path
if not os.path.exists(r_txt_file):
build_utils.Touch(r_txt_file)
if options.r_text_out:
shutil.copyfile(r_txt_file, options.r_text_out)
return r_txt_file for final, temp in possible_outputs:
# Write file only if it's changed.
if final and not (os.path.exists(final) and filecmp.cmp(final, temp)):
shutil.move(temp, final)
def main(args): def main(args):
...@@ -855,18 +826,12 @@ def main(args): ...@@ -855,18 +826,12 @@ def main(args):
debug_temp_resources_dir = os.environ.get(_ENV_DEBUG_VARIABLE) debug_temp_resources_dir = os.environ.get(_ENV_DEBUG_VARIABLE)
if debug_temp_resources_dir: if debug_temp_resources_dir:
debug_temp_resources_dir = os.path.join(debug_temp_resources_dir, debug_temp_resources_dir = os.path.join(debug_temp_resources_dir,
os.path.basename(options.apk_path)) os.path.basename(options.arsc_path))
build_utils.DeleteDirectory(debug_temp_resources_dir) build_utils.DeleteDirectory(debug_temp_resources_dir)
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:
dep_subdirs = resource_utils.ExtractDeps(options.dependencies_res_zips, _PackageApk(options, build)
build.deps_dir)
_PackageApk(options, dep_subdirs, build.temp_dir, build.gen_dir,
build.r_txt_path)
r_txt_path = _WriteFinalRTxtFile(options, build.r_txt_path)
# 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()
...@@ -887,27 +852,30 @@ def main(args): ...@@ -887,27 +852,30 @@ def main(args):
rjava_build_options.GenerateOnResourcesLoaded() rjava_build_options.GenerateOnResourcesLoaded()
resource_utils.CreateRJavaFiles( resource_utils.CreateRJavaFiles(
build.srcjar_dir, None, r_txt_path, options.extra_res_packages, build.srcjar_dir, None, build.r_txt_path, options.extra_res_packages,
options.extra_r_text_files, rjava_build_options) options.extra_r_text_files, rjava_build_options)
if options.srcjar_out: if options.srcjar_out:
build_utils.ZipDir(options.srcjar_out, build.srcjar_dir) build_utils.ZipDir(build.srcjar_path, build.srcjar_dir)
# Sanity check that the created resources have the expected package ID. # Sanity check that the created resources have the expected package ID.
expected_id = _PackageIdFromOptions(options) expected_id = _PackageIdFromOptions(options)
if expected_id is None: if expected_id is None:
expected_id = '0x00' if options.shared_resources else '0x7f' expected_id = '0x00' if options.shared_resources else '0x7f'
expected_id = int(expected_id, 16) expected_id = int(expected_id, 16)
_, package_id = resource_utils.ExtractArscPackage(options.aapt2_path, _, package_id = resource_utils.ExtractArscPackage(
options.apk_path) options.aapt2_path,
build.arsc_path if options.arsc_path else build.proto_path)
if package_id != expected_id: if package_id != expected_id:
raise Exception( raise Exception(
'Invalid package ID 0x%x (expected 0x%x)' % (package_id, expected_id)) 'Invalid package ID 0x%x (expected 0x%x)' % (package_id, expected_id))
_WriteOutputs(options, build)
if options.depfile: if options.depfile:
build_utils.WriteDepfile( build_utils.WriteDepfile(
options.depfile, options.depfile,
options.apk_path, options.arsc_path or options.proto_path,
inputs=options.dependencies_res_zips + options.extra_r_text_files, inputs=options.dependencies_res_zips + options.extra_r_text_files,
add_pydeps=False) add_pydeps=False)
......
...@@ -127,11 +127,10 @@ def main(args): ...@@ -127,11 +127,10 @@ def main(args):
action='append', action='append',
help='Same as --assets, except disables compression.') help='Same as --assets, except disables compression.')
parser.add_argument( parser.add_argument(
'--resource-apk', '--in-res-info-path',
dest='resource_apks',
required=True, required=True,
action='append', action='append',
help='An .ap_ file built using aapt') help='Paths to .ap_.info files')
options = parser.parse_args(args) options = parser.parse_args(args)
...@@ -143,7 +142,7 @@ def main(args): ...@@ -143,7 +142,7 @@ def main(args):
jar_inputs = _FindJarInputs(set(options.jar_files)) jar_inputs = _FindJarInputs(set(options.jar_files))
pak_inputs = _PakInfoPathsForAssets(options.assets + pak_inputs = _PakInfoPathsForAssets(options.assets +
options.uncompressed_assets) options.uncompressed_assets)
res_inputs = [p + '.info' for p in options.resource_apks] res_inputs = options.in_res_info_path
# Don't bother re-running if no .info files have changed (saves ~250ms). # Don't bother re-running if no .info files have changed (saves ~250ms).
md5_check.CallAndRecordIfStale( md5_check.CallAndRecordIfStale(
......
...@@ -599,11 +599,21 @@ class _ResourceBuildContext(object): ...@@ -599,11 +599,21 @@ class _ResourceBuildContext(object):
# A location to place aapt-generated files. # A location to place aapt-generated files.
self.gen_dir = os.path.join(self.temp_dir, 'gen') self.gen_dir = os.path.join(self.temp_dir, 'gen')
os.mkdir(self.gen_dir) os.mkdir(self.gen_dir)
# Location of the generated R.txt file.
self.r_txt_path = os.path.join(self.gen_dir, 'R.txt')
# A location to place generated R.java files. # A location to place generated R.java files.
self.srcjar_dir = os.path.join(self.temp_dir, 'java') self.srcjar_dir = os.path.join(self.temp_dir, 'java')
os.mkdir(self.srcjar_dir) os.mkdir(self.srcjar_dir)
# Temporary file locacations.
self.r_txt_path = os.path.join(self.gen_dir, 'R.txt')
self.srcjar_path = os.path.join(self.temp_dir, 'R.srcjar')
self.info_path = os.path.join(self.temp_dir, 'size.info')
self.stable_ids_path = os.path.join(self.temp_dir, 'in_ids.txt')
self.emit_ids_path = os.path.join(self.temp_dir, 'out_ids.txt')
self.proguard_path = os.path.join(self.temp_dir, 'keeps.flags')
self.proguard_main_dex_path = os.path.join(self.temp_dir, 'maindex.flags')
self.arsc_path = os.path.join(self.temp_dir, 'out.ap_')
self.proto_path = os.path.join(self.temp_dir, 'out.proto.ap_')
self.optimized_arsc_path = os.path.join(self.temp_dir, 'out.opt.ap_')
self.optimized_proto_path = os.path.join(self.temp_dir, 'out.opt.proto.ap_')
def Close(self): def Close(self):
"""Close the context and destroy all temporary files.""" """Close the context and destroy all temporary files."""
......
...@@ -924,6 +924,7 @@ def main(argv): ...@@ -924,6 +924,7 @@ def main(argv):
parser.add_option('--final-dex-path', parser.add_option('--final-dex-path',
help='Path to final input classes.dex (or classes.zip) to ' help='Path to final input classes.dex (or classes.zip) to '
'use in final apk.') 'use in final apk.')
parser.add_option('--res-size-info', help='Path to .ap_.info')
parser.add_option('--apk-proto-resources', parser.add_option('--apk-proto-resources',
help='Path to resources compiled in protocol buffer format ' help='Path to resources compiled in protocol buffer format '
' for this apk.') ' for this apk.')
...@@ -962,7 +963,7 @@ def main(argv): ...@@ -962,7 +963,7 @@ def main(argv):
'android_apk': ['build_config', 'dex_path', 'final_dex_path'] + \ 'android_apk': ['build_config', 'dex_path', 'final_dex_path'] + \
jar_path_options, jar_path_options,
'android_app_bundle_module': ['build_config', 'dex_path', 'android_app_bundle_module': ['build_config', 'dex_path',
'final_dex_path'] + jar_path_options, 'final_dex_path', 'res_size_info'] + jar_path_options,
'android_assets': ['build_config'], 'android_assets': ['build_config'],
'android_resources': ['build_config', 'resources_zip'], 'android_resources': ['build_config', 'resources_zip'],
'dist_aar': ['build_config'], 'dist_aar': ['build_config'],
...@@ -1277,6 +1278,8 @@ def main(argv): ...@@ -1277,6 +1278,8 @@ def main(argv):
if options.type == 'android_apk' and options.tested_apk_config: if options.type == 'android_apk' and options.tested_apk_config:
config['resources']['arsc_package_name'] = ( config['resources']['arsc_package_name'] = (
tested_apk_config['package_name']) tested_apk_config['package_name'])
if options.res_size_info:
config['resources']['size_info'] = options.res_size_info
if is_apk_or_module_target: if is_apk_or_module_target:
deps_dex_files = [c['dex_path'] for c in all_library_deps] deps_dex_files = [c['dex_path'] for c in all_library_deps]
......
...@@ -313,18 +313,21 @@ template("write_build_config") { ...@@ -313,18 +313,21 @@ template("write_build_config") {
rebase_path(invoker.r_text, root_build_dir), rebase_path(invoker.r_text, root_build_dir),
] ]
} }
if (defined(invoker.res_size_info_path)) {
args += [
"--res-size-info",
rebase_path(invoker.res_size_info_path, root_build_dir),
]
}
if (defined(invoker.resource_dirs)) { if (defined(invoker.resource_dirs)) {
resource_dirs = rebase_path(invoker.resource_dirs, root_build_dir) resource_dirs = rebase_path(invoker.resource_dirs, root_build_dir)
args += [ "--resource-dirs=$resource_dirs" ] args += [ "--resource-dirs=$resource_dirs" ]
} }
if (defined(invoker.proto_resources_path)) { if (defined(invoker.proto_resources_path)) {
_rebased_proto_resources = _rebased_proto_resources =
rebase_path(invoker.proto_resources_path, root_build_dir) rebase_path(invoker.proto_resources_path, root_build_dir)
args += [ "--apk-proto-resources=$_rebased_proto_resources" ] args += [ "--apk-proto-resources=$_rebased_proto_resources" ]
} }
if (defined(invoker.module_rtxt_path)) { if (defined(invoker.module_rtxt_path)) {
_rebased_rtxt_path = rebase_path(invoker.module_rtxt_path, root_build_dir) _rebased_rtxt_path = rebase_path(invoker.module_rtxt_path, root_build_dir)
args += [ "--module-rtxt-path=$_rebased_rtxt_path" ] args += [ "--module-rtxt-path=$_rebased_rtxt_path" ]
...@@ -2076,9 +2079,6 @@ if (enable_java_templates) { ...@@ -2076,9 +2079,6 @@ if (enable_java_templates) {
# #
# post_process_script: (optional) # post_process_script: (optional)
# #
# proto_format: (optional). If true, compiles resources into protocol
# buffer format.
#
# package_name: (optional) # package_name: (optional)
# Name of the package for the purpose of assigning package ID. # Name of the package for the purpose of assigning package ID.
# #
...@@ -2097,7 +2097,13 @@ if (enable_java_templates) { ...@@ -2097,7 +2097,13 @@ if (enable_java_templates) {
# used in AndroidManifest.xml. # used in AndroidManifest.xml.
# #
# Output variables: # Output variables:
# output: Path to a zip file containing the compiled resources. # arsc_output: Path to output .ap_ file (optional).
#
# proto_output: Path to output .proto.ap_ file (optional).
#
# optimized_arsc_output: Path to optimized .ap_ file (optional).
#
# optimized_proto_output: Path to optimized .proto.ap_ file (optional).
# #
# r_text_out_path: (optional): # r_text_out_path: (optional):
# Path for the corresponding generated R.txt file. # Path for the corresponding generated R.txt file.
...@@ -2111,24 +2117,30 @@ if (enable_java_templates) { ...@@ -2111,24 +2117,30 @@ if (enable_java_templates) {
# #
# proguard_file_main_dex: (optional) # proguard_file_main_dex: (optional)
# #
#
template("compile_resources") { template("compile_resources") {
_compile_resources_target_name = target_name _compile_resources_target_name = target_name
_compiled_resources_path = invoker.output if (defined(invoker.arsc_output)) {
_arsc_output = invoker.arsc_output
}
if (defined(invoker.optimized_arsc_output)) {
_optimized_arsc_output = invoker.optimized_arsc_output
}
if (defined(invoker.srcjar_path)) { if (defined(invoker.srcjar_path)) {
_srcjar_path = invoker.srcjar_path _srcjar_path = invoker.srcjar_path
} }
if (defined(invoker.post_process_script)) { if (defined(invoker.post_process_script)) {
_compile_resources_target_name = "${target_name}__intermediate" _compile_resources_target_name = "${target_name}__intermediate"
_compiled_resources_path =
get_path_info(_compiled_resources_path, "dir") + "/" +
get_path_info(_compiled_resources_path, "name") + ".intermediate.ap_"
_srcjar_path = "${_srcjar_path}.intermediate.srcjar" _srcjar_path = "${_srcjar_path}.intermediate.srcjar"
_intermediate_path =
get_path_info(_arsc_output, "dir") + "/" +
get_path_info(_arsc_output, "name") + ".intermediate.ap_"
if (defined(_optimized_arsc_output)) {
_optimized_arsc_output = _intermediate_path
} else {
_arsc_output = _intermediate_path
}
} }
_proto_format = defined(invoker.proto_format) && invoker.proto_format
# NOTE: Regarding the names of the depfiles used by this template: # NOTE: Regarding the names of the depfiles used by this template:
# They all have the same prefix, related to invoker.target_name, # They all have the same prefix, related to invoker.target_name,
# instead of $target_name, so it is important they have different # instead of $target_name, so it is important they have different
...@@ -2138,7 +2150,6 @@ if (enable_java_templates) { ...@@ -2138,7 +2150,6 @@ if (enable_java_templates) {
# #
# _1.d for the unprocessed compiled resources. # _1.d for the unprocessed compiled resources.
# _2.d for the optional processed compiled resources. # _2.d for the optional processed compiled resources.
# _3.d for the proto-compiled resources.
action_with_pydeps(_compile_resources_target_name) { action_with_pydeps(_compile_resources_target_name) {
set_sources_assignment_filter([]) set_sources_assignment_filter([])
...@@ -2153,16 +2164,9 @@ if (enable_java_templates) { ...@@ -2153,16 +2164,9 @@ if (enable_java_templates) {
depfile = "$target_gen_dir/${invoker.target_name}_1.d" depfile = "$target_gen_dir/${invoker.target_name}_1.d"
outputs = [] outputs = []
_android_aapt_path = android_default_aapt_path
_android_aapt2_path = android_sdk_tools_bundle_aapt2
if (_proto_format) {
depfile = "$target_gen_dir/${invoker.target_name}_3.d"
}
inputs = [ inputs = [
invoker.build_config, invoker.build_config,
_android_aapt_path, android_sdk_tools_bundle_aapt2,
_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)
...@@ -2172,7 +2176,7 @@ if (enable_java_templates) { ...@@ -2172,7 +2176,7 @@ if (enable_java_templates) {
rebase_path(depfile, root_build_dir), rebase_path(depfile, root_build_dir),
"--include-resources=@FileArg($_rebased_build_config:android:sdk_jars)", "--include-resources=@FileArg($_rebased_build_config:android:sdk_jars)",
"--aapt2-path", "--aapt2-path",
rebase_path(_android_aapt2_path, root_build_dir), rebase_path(android_sdk_tools_bundle_aapt2, 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)",
...@@ -2183,11 +2187,9 @@ if (enable_java_templates) { ...@@ -2183,11 +2187,9 @@ if (enable_java_templates) {
"--android-manifest", "--android-manifest",
rebase_path(invoker.android_manifest, root_build_dir), rebase_path(invoker.android_manifest, root_build_dir),
] ]
if (defined(invoker.no_xml_namespaces) && invoker.no_xml_namespaces) { if (defined(invoker.no_xml_namespaces) && invoker.no_xml_namespaces) {
args += [ "--no-xml-namespaces" ] args += [ "--no-xml-namespaces" ]
} }
if (defined(invoker.version_code)) { if (defined(invoker.version_code)) {
args += [ args += [
"--version-code", "--version-code",
...@@ -2200,34 +2202,47 @@ if (enable_java_templates) { ...@@ -2200,34 +2202,47 @@ if (enable_java_templates) {
invoker.version_name, invoker.version_name,
] ]
} }
if (defined(_compiled_resources_path)) { if (defined(_arsc_output)) {
_info_path = invoker.output + ".info" outputs += [ _arsc_output ]
outputs += [ args += [
_compiled_resources_path, "--arsc-path",
_info_path, rebase_path(_arsc_output, root_build_dir),
] ]
}
if (defined(invoker.proto_output)) {
outputs += [ invoker.proto_output ]
args += [ args += [
"--apk-path", "--proto-path",
rebase_path(_compiled_resources_path, root_build_dir), rebase_path(invoker.proto_output, root_build_dir),
"--apk-info-path",
rebase_path(_info_path, root_build_dir),
] ]
} }
if (defined(invoker.size_info_path)) {
if (defined(invoker.optimized_resources_path)) { outputs += [ invoker.size_info_path ]
args += [ args += [
"--optimized-resources-path", "--info-path",
rebase_path(invoker.optimized_resources_path, root_build_dir), rebase_path(invoker.size_info_path, root_build_dir),
]
}
if (defined(_optimized_arsc_output)) {
outputs += [ _optimized_arsc_output ]
args += [
"--optimized-arsc-path",
rebase_path(_optimized_arsc_output, root_build_dir),
]
}
if (defined(invoker.optimized_proto_output)) {
outputs += [ invoker.optimized_proto_output ]
args += [
"--optimized-proto-path",
rebase_path(invoker.optimized_proto_output, root_build_dir),
]
}
if (defined(invoker.resources_config_path)) {
inputs += [ invoker.resources_config_path ]
args += [
"--resources-config-path",
rebase_path(invoker.resources_config_path, root_build_dir),
] ]
outputs += [ invoker.optimized_resources_path ]
if (defined(invoker.resources_config_path)) {
inputs += [ invoker.resources_config_path ]
args += [
"--resources-config-path",
rebase_path(invoker.resources_config_path, root_build_dir),
]
}
} }
# Useful to have android:debuggable in the manifest even for Release # Useful to have android:debuggable in the manifest even for Release
...@@ -2259,10 +2274,6 @@ if (enable_java_templates) { ...@@ -2259,10 +2274,6 @@ if (enable_java_templates) {
] ]
} }
if (_proto_format) {
args += [ "--proto-format" ]
}
# Define the flags related to shared resources. # Define the flags related to shared resources.
# #
# Note the small sanity check to ensure that the package ID of the # Note the small sanity check to ensure that the package ID of the
...@@ -2382,15 +2393,24 @@ if (enable_java_templates) { ...@@ -2382,15 +2393,24 @@ if (enable_java_templates) {
args = [ args = [
"--depfile", "--depfile",
rebase_path(depfile, root_build_dir), rebase_path(depfile, root_build_dir),
"--apk-path",
rebase_path(_compiled_resources_path, root_build_dir),
"--output",
rebase_path(invoker.output, root_build_dir),
"--srcjar-in", "--srcjar-in",
rebase_path(_srcjar_path, root_build_dir), rebase_path(_srcjar_path, root_build_dir),
"--srcjar-out", "--srcjar-out",
rebase_path(invoker.srcjar_path, root_build_dir), rebase_path(invoker.srcjar_path, root_build_dir),
] ]
if (defined(_optimized_arsc_output)) {
_input_apk = _optimized_arsc_output
_output_apk = invoker.optimized_arsc_output
} else {
_input_apk = _arsc_output
_output_apk = invoker.arsc_output
}
args += [
"--apk-path",
rebase_path(_input_apk, root_build_dir),
"--output",
rebase_path(_output_apk, root_build_dir),
]
if (defined(invoker.shared_resources_whitelist)) { if (defined(invoker.shared_resources_whitelist)) {
args += [ args += [
"--r-text-whitelist", "--r-text-whitelist",
...@@ -2400,14 +2420,14 @@ if (enable_java_templates) { ...@@ -2400,14 +2420,14 @@ if (enable_java_templates) {
] ]
} }
inputs = [ inputs = [
_input_apk,
_srcjar_path, _srcjar_path,
_compiled_resources_path,
] ]
if (defined(invoker.post_process_script_inputs)) { if (defined(invoker.post_process_script_inputs)) {
inputs += invoker.post_process_script_inputs inputs += invoker.post_process_script_inputs
} }
outputs = [ outputs = [
invoker.output, _output_apk,
invoker.srcjar_path, invoker.srcjar_path,
] ]
public_deps = [ public_deps = [
...@@ -2423,7 +2443,7 @@ if (enable_java_templates) { ...@@ -2423,7 +2443,7 @@ if (enable_java_templates) {
# build_config: Path to APK's build config file. Used to extract the # build_config: Path to APK's build config file. Used to extract the
# list of input .jar files from its dependencies. # list of input .jar files from its dependencies.
# name: Name of the apk or app bundle (e.g. "Foo.apk"). # name: Name of the apk or app bundle (e.g. "Foo.apk").
# packaged_resources_path: Path to .ap_ file. # res_size_info_path: Path to input .ap_.info file (for apks).
# #
template("create_size_info_files") { template("create_size_info_files") {
action_with_pydeps(target_name) { action_with_pydeps(target_name) {
...@@ -2460,7 +2480,7 @@ if (enable_java_templates) { ...@@ -2460,7 +2480,7 @@ if (enable_java_templates) {
args += [ args += [
"--jar-files=@FileArg($_rebased_build_config:deps_info:unprocessed_jar_path)", "--jar-files=@FileArg($_rebased_build_config:deps_info:unprocessed_jar_path)",
"--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)", "--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
"--resource-apk=@FileArg($_rebased_build_config:deps_info:proto_resources_path)", "--in-res-info-path=@FileArg($_rebased_build_config:resources:size_info)",
"--assets=@FileArg($_rebased_build_config:assets)", "--assets=@FileArg($_rebased_build_config:assets)",
"--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)", "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
] ]
...@@ -2468,15 +2488,15 @@ if (enable_java_templates) { ...@@ -2468,15 +2488,15 @@ if (enable_java_templates) {
} else { } else {
inputs = [ inputs = [
invoker.build_config, invoker.build_config,
invoker.packaged_resources_path, invoker.res_size_info_path,
] ]
_rebased_build_config = _rebased_build_config =
rebase_path(invoker.build_config, root_build_dir) rebase_path(invoker.build_config, root_build_dir)
args += [ args += [
"--jar-files=@FileArg($_rebased_build_config:deps_info:jar_path)", "--jar-files=@FileArg($_rebased_build_config:deps_info:jar_path)",
"--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)", "--jar-files=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)",
"--resource-apk", "--in-res-info-path",
rebase_path(invoker.packaged_resources_path, root_build_dir), rebase_path(invoker.res_size_info_path, root_build_dir),
"--assets=@FileArg($_rebased_build_config:assets)", "--assets=@FileArg($_rebased_build_config:assets)",
"--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)", "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
] ]
...@@ -3341,6 +3361,7 @@ if (enable_java_templates) { ...@@ -3341,6 +3361,7 @@ if (enable_java_templates) {
"extra_shared_libraries", "extra_shared_libraries",
"final_dex_path", "final_dex_path",
"native_lib_placeholders", "native_lib_placeholders",
"res_size_info_path",
"secondary_abi_shared_libraries_runtime_deps_file", "secondary_abi_shared_libraries_runtime_deps_file",
"secondary_native_lib_placeholders", "secondary_native_lib_placeholders",
"shared_libraries_runtime_deps_file", "shared_libraries_runtime_deps_file",
......
...@@ -2070,7 +2070,10 @@ if (enable_java_templates) { ...@@ -2070,7 +2070,10 @@ if (enable_java_templates) {
_is_bundle_module = _is_bundle_module =
defined(invoker.is_bundle_module) && invoker.is_bundle_module defined(invoker.is_bundle_module) && invoker.is_bundle_module
_is_base_module = defined(invoker.is_base_module) && invoker.is_base_module if (_is_bundle_module) {
_is_base_module =
defined(invoker.is_base_module) && invoker.is_base_module
}
_enable_multidex = _enable_multidex =
!defined(invoker.enable_multidex) || invoker.enable_multidex !defined(invoker.enable_multidex) || invoker.enable_multidex
...@@ -2084,34 +2087,36 @@ if (enable_java_templates) { ...@@ -2084,34 +2087,36 @@ if (enable_java_templates) {
if (!_is_bundle_module) { if (!_is_bundle_module) {
_final_rtxt_path = "${_final_apk_path}.R.txt" _final_rtxt_path = "${_final_apk_path}.R.txt"
} }
_res_size_info_path = "$target_out_dir/$target_name.ap_.info"
_final_apk_path_no_ext_list = _final_apk_path_no_ext_list =
process_file_template([ _final_apk_path ], process_file_template([ _final_apk_path ],
"{{source_dir}}/{{source_name_part}}") "{{source_dir}}/{{source_name_part}}")
_final_apk_path_no_ext = _final_apk_path_no_ext_list[0] _final_apk_path_no_ext = _final_apk_path_no_ext_list[0]
assert(_final_apk_path_no_ext != "") # Mark as used. assert(_final_apk_path_no_ext != "") # Mark as used.
_optimize_resources =
defined(invoker.optimize_resources) && invoker.optimize_resources
# Non-base bundle modules create only proto resources. # Non-base bundle modules create only proto resources.
if (!_is_bundle_module || _is_base_module) { if (!_is_bundle_module || _is_base_module) {
_packaged_resources_path = "$target_out_dir/$target_name.ap_" _arsc_resources_path = "$target_out_dir/$target_name.ap_"
} }
if (_is_bundle_module) { if (_is_bundle_module) {
# Path to the intermediate proto-format resources zip file. # Path to the intermediate proto-format resources zip file.
_proto_resources_path = "$target_gen_dir/$target_name.proto.ap_" _proto_resources_path = "$target_out_dir/$target_name.proto.ap_"
if (_optimize_resources) {
_optimized_proto_resources_path =
"$target_out_dir/$target_name.optimized.proto.ap_"
}
} else { } else {
# resource_sizes.py needs to be able to find the unpacked resources.arsc # resource_sizes.py needs to be able to find the unpacked resources.arsc
# file based on apk name to compute normatlized size. # file based on apk name to compute normatlized size.
_resource_sizes_arsc_path = _resource_sizes_arsc_path =
"$root_out_dir/arsc/" + "$root_out_dir/arsc/" +
rebase_path(_final_apk_path_no_ext, root_build_dir) + ".ap_" rebase_path(_final_apk_path_no_ext, root_build_dir) + ".ap_"
} if (_optimize_resources) {
_optimize_resources = _optimized_arsc_resources_path =
defined(invoker.optimize_resources) && invoker.optimize_resources "$target_out_dir/$target_name.optimized.ap_"
if (_optimize_resources) {
_optimized_resources_path = "$target_out_dir/$_template_name.optimized."
if (_is_bundle_module) {
_optimized_resources_path += ".proto.ap_"
} else {
_optimized_resources_path += ".ap_"
} }
} }
...@@ -2358,6 +2363,7 @@ if (enable_java_templates) { ...@@ -2358,6 +2363,7 @@ if (enable_java_templates) {
srcjar_path = "${target_gen_dir}/${target_name}.srcjar" srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
r_text_out_path = _compile_resources_rtxt_out r_text_out_path = _compile_resources_rtxt_out
emit_ids_out_path = _compile_resources_emit_ids_out emit_ids_out_path = _compile_resources_emit_ids_out
size_info_path = _res_size_info_path
proguard_file = _generated_proguard_config proguard_file = _generated_proguard_config
if (_enable_main_dex_list) { if (_enable_main_dex_list) {
proguard_file_main_dex = _generated_proguard_main_dex_config proguard_file_main_dex = _generated_proguard_main_dex_config
...@@ -2382,28 +2388,37 @@ if (enable_java_templates) { ...@@ -2382,28 +2388,37 @@ if (enable_java_templates) {
assert(!defined(resource_ids_provider_dep)) assert(!defined(resource_ids_provider_dep))
resource_ids_provider_dep = invoker.apk_under_test resource_ids_provider_dep = invoker.apk_under_test
deps += [ "${invoker.apk_under_test}__compile_resources" ]
include_resource = include_resource =
get_label_info(invoker.apk_under_test, "target_out_dir") + "/" + get_label_info(invoker.apk_under_test, "target_out_dir") + "/" +
get_label_info(invoker.apk_under_test, "name") + ".ap_" get_label_info(invoker.apk_under_test, "name") + ".ap_"
_link_against = invoker.apk_under_test
} }
if (_is_bundle_module) { if (_is_bundle_module) {
proto_format = true proto_output = _proto_resources_path
output = _proto_resources_path if (_optimize_resources) {
optimized_proto_output = _optimized_proto_resources_path
}
if (defined(invoker.base_module_target)) { if (defined(invoker.base_module_target)) {
deps += [ "${invoker.base_module_target}__compile_arsc_resources" ]
include_resource = include_resource =
get_label_info(invoker.base_module_target, "target_out_dir") + get_label_info(invoker.base_module_target, "target_out_dir") +
"/" + get_label_info(invoker.base_module_target, "name") + ".ap_" "/" + get_label_info(invoker.base_module_target, "name") + ".ap_"
_link_against = invoker.base_module_target
} }
} else { } else if (_optimize_resources) {
output = _packaged_resources_path optimized_arsc_output = _optimized_arsc_resources_path
} }
if (_optimize_resources) { if (defined(_link_against)) {
optimized_resources_path = _optimized_resources_path deps += [ "${_link_against}__compile_resources" ]
include_resource = get_label_info(_link_against, "target_out_dir") +
"/" + get_label_info(_link_against, "name") + ".ap_"
}
# Bundle modules have to reference resources from the base module.
if (!_is_bundle_module || _is_base_module) {
arsc_output = _arsc_resources_path
} }
if (defined(invoker.shared_resources_whitelist_target)) { if (defined(invoker.shared_resources_whitelist_target)) {
...@@ -2422,7 +2437,7 @@ if (enable_java_templates) { ...@@ -2422,7 +2437,7 @@ if (enable_java_templates) {
# resource_sizes.py doesn't care if it gets the optimized .arsc. # resource_sizes.py doesn't care if it gets the optimized .arsc.
sources = [ sources = [
_packaged_resources_path, _arsc_resources_path,
] ]
outputs = [ outputs = [
_resource_sizes_arsc_path, _resource_sizes_arsc_path,
...@@ -2453,38 +2468,6 @@ if (enable_java_templates) { ...@@ -2453,38 +2468,6 @@ if (enable_java_templates) {
_final_deps += [ ":$_copy_rtxt_target" ] _final_deps += [ ":$_copy_rtxt_target" ]
} }
if (_is_base_module && _is_bundle_module) {
# Bundle modules have to reference resources from the base module.
# However, to compile the bundle module's resources we have to give it an
# arsc resource to link against (aapt2 fails with proto resources). Thus,
# add an arsc resource compilation step to make the bundle module's link
# step work.
compile_resources("${_template_name}__compile_arsc_resources") {
forward_variables_from(invoker,
[
"support_zh_hk",
"aapt_locale_whitelist",
"resource_blacklist_regex",
"resource_blacklist_exceptions",
"png_to_webp",
"no_xml_namespaces",
])
android_manifest = _android_manifest
version_code = _version_code
version_name = _version_name
proto_format = false
output = _packaged_resources_path
build_config = _build_config
deps = _deps + [
":$_merge_manifest_target",
":$_build_config_target",
_android_sdk_dep,
]
}
}
_srcjar_deps += [ ":$_compile_resources_target" ] _srcjar_deps += [ ":$_compile_resources_target" ]
if (_native_libs_deps != [] || _secondary_abi_native_libs_deps != []) { if (_native_libs_deps != [] || _secondary_abi_native_libs_deps != []) {
...@@ -2642,6 +2625,7 @@ if (enable_java_templates) { ...@@ -2642,6 +2625,7 @@ if (enable_java_templates) {
]) ])
if (_is_bundle_module) { if (_is_bundle_module) {
type = "android_app_bundle_module" type = "android_app_bundle_module"
res_size_info_path = _res_size_info_path
} else { } else {
type = "android_apk" type = "android_apk"
} }
...@@ -2817,7 +2801,7 @@ if (enable_java_templates) { ...@@ -2817,7 +2801,7 @@ if (enable_java_templates) {
create_size_info_files(_size_info_target) { create_size_info_files(_size_info_target) {
name = "${invoker.name}.apk" name = "${invoker.name}.apk"
build_config = _build_config build_config = _build_config
packaged_resources_path = _packaged_resources_path res_size_info_path = _res_size_info_path
deps = _deps + [ deps = _deps + [
":$_build_config_target", ":$_build_config_target",
":$_compile_resources_target", ":$_compile_resources_target",
...@@ -2852,9 +2836,12 @@ if (enable_java_templates) { ...@@ -2852,9 +2836,12 @@ if (enable_java_templates) {
"write_asset_list", "write_asset_list",
"uncompress_dex", "uncompress_dex",
]) ])
packaged_resources_path = _packaged_resources_path packaged_resources_path = _arsc_resources_path
if (_optimize_resources) {
optimized_resources_path = _optimized_resources_path if (_optimize_resources && _is_bundle_module) {
optimized_resources_path = _optimized_proto_resources_path
} else if (_optimize_resources) {
optimized_resources_path = _optimized_arsc_resources_path
} }
apk_path = _final_apk_path apk_path = _final_apk_path
......
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