1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
|
#!/usr/bin/env python3
#
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script to add Asset Packs to an already built Android App Bundle."""
import argparse
import fnmatch
import os
import platform
import shutil
import subprocess
import sys
import tempfile
import typing
import xml.dom.minidom
import zipfile
import config_pb2
import google.protobuf.json_format as json_format
FASTFOLLOW = "dist:fast-follow"
ONDEMAND = "dist:on-demand"
UPFRONT = "dist:install-time"
def parse_args() -> argparse.Namespace:
"""Parse input arguments."""
parser = argparse.ArgumentParser(
description="Augments an Android App Bundle with given asset packs.",
add_help=True)
parser.add_argument(
"--androidsdk", required=True, help="Android SDK location")
parser.add_argument("--sdkver", required=True, help="Android SDK version")
parser.add_argument(
"--buildtoolsver", required=True, help="Android Build Tools version")
parser.add_argument(
"--bundletool", required=True, help="Path to Bundletool jar file")
parser.add_argument(
"--inputbundle", required=True, help="App Bundle to augment")
parser.add_argument(
"--packdir", required=True, help="Folder to read assets packs from")
parser.add_argument(
"--packnames",
required=True,
help="Comma separated list of asset pack files")
parser.add_argument("--output", required=True, help="Output App Bundle")
parser.add_argument(
"--overwrite",
required=False,
action="store_true",
help="Overwrite existing files")
parser.add_argument(
"--striptcfsuffixes",
required=False,
action="store_true",
help="Enable removal of #tcf_xxx suffixes in asset pack folder names")
parser.add_argument(
"--compressinstalltimeassets",
required=False,
action="store_true",
help=("Compress assets within install time asset packs."
"This will not apply to on demand or fast follow asset packs"
"Setting is overridden for files matched in the uncompressed glob"))
return parser.parse_args()
def abs_expand_all(path: str) -> str:
return os.path.abspath(os.path.expandvars(os.path.expanduser(path)))
def get_aapt2_bin_path(args: argparse.Namespace) -> str:
"""Retrieve the path for the aapt2 binary."""
android_sdk_path = abs_expand_all(args.androidsdk)
build_tools_version = args.buildtoolsver
aapt2_bin_path: str
if platform.system() == "Windows":
aapt2_bin_path = os.path.join(android_sdk_path, "build-tools",
build_tools_version, "aapt2.exe")
else:
aapt2_bin_path = os.path.join(android_sdk_path, "build-tools",
build_tools_version, "aapt2")
if not os.path.exists(aapt2_bin_path):
print(
"Cannot find AAPT2 at {aapt2_bin_path}".format(
aapt2_bin_path=aapt2_bin_path),
file=sys.stderr)
sys.exit(-1)
return aapt2_bin_path
def get_sdk_jar_path(args: argparse.Namespace) -> str:
"""Retrieve the path for the android SDK JAR file."""
android_sdk_path = abs_expand_all(args.androidsdk)
sdk_ver = args.sdkver
sdk_jar_path = os.path.join(android_sdk_path, "platforms",
"android-" + sdk_ver, "android.jar")
if not os.path.exists(sdk_jar_path):
print(
"Cannot find android.jar at {sdk_jar_path}".format(
sdk_jar_path=sdk_jar_path),
file=sys.stderr)
sys.exit(-1)
return sdk_jar_path
def get_bundletool_path(args: argparse.Namespace) -> str:
"""Retrieve the path for the BundleTool JAR file."""
bundletool_path = abs_expand_all(args.bundletool)
if not os.path.exists(bundletool_path):
print(
"Cannot find Bundletool at {bundletool_path}".format(
bundletool_path=bundletool_path),
file=sys.stderr)
sys.exit(-1)
return bundletool_path
def get_packs(args: argparse.Namespace) -> (str, typing.List[str]):
"""Retrieve the pack directory and pack names from the input flags."""
pack_dir = abs_expand_all(args.packdir)
if not os.path.exists(pack_dir):
print(
"Cannot find asset pack directory at {pack_dir}".format(
pack_dir=pack_dir),
file=sys.stderr)
sys.exit(-1)
pack_names = args.packnames.split(",")
for pack_name in pack_names:
pack_path = os.path.join(pack_dir, pack_name)
if not os.path.exists(pack_path):
print(
"Cannot find asset pack {pack_name} at {pack_path}".format(
pack_name=pack_name, pack_path=pack_path),
file=sys.stderr)
sys.exit(-1)
return (pack_dir, pack_names)
def get_input_bundle_path(args: argparse.Namespace) -> str:
bundle_path = abs_expand_all(args.inputbundle)
if not os.path.exists(bundle_path):
print(
"Cannot find input app bundle_path {bundle_path}".format(
bundle_path=bundle_path),
file=sys.stderr)
sys.exit(-1)
return bundle_path
def get_output_path(args: argparse.Namespace) -> str:
"""Retrieve the output file name."""
output_path = abs_expand_all(args.output)
if os.path.exists(output_path):
if os.path.isdir(output_path):
print(
"Output location {output_path} is a directory. Specify a file path instead."
.format(output_path=output_path))
sys.exit(-1)
if not args.overwrite:
print(
"Output file {output_path} exists. Specify --overwrite to bypass. Exiting."
.format(output_path=output_path))
sys.exit(-1)
else:
os.remove(output_path)
return output_path
def purge_files(directory: str, pattern: str) -> None:
"""Purge the files inside a directory that match the given pattern."""
for root_dir, _, filenames in os.walk(directory):
for filename in fnmatch.filter(filenames, pattern):
try:
os.remove(os.path.join(root_dir, filename))
except OSError as e:
print(
"Error while deleting {filename}: {e}".format(
filename=filename, e=e),
file=sys.stderr)
def purge_subdirs(directory: str, pattern: str) -> None:
"""Purge the subdirectories inside a directory that match the given pattern."""
for root_dir, subdirs, _ in os.walk(directory):
for filename in fnmatch.filter(subdirs, pattern):
try:
shutil.rmtree(os.path.join(root_dir, filename))
except FileNotFoundError as e:
print(
"Error while deleting {filename}: {e}".format(
filename=filename, e=e),
file=sys.stderr)
def parse_bundle_metadata(bundle_folder: str) -> typing.List[str]:
"""Parse the Bundle Metadata from the given bundle directory."""
metadata = []
metadata_folder = os.path.join(bundle_folder, "BUNDLE-METADATA")
if not os.path.isdir(metadata_folder):
return
for folder in os.listdir(metadata_folder):
inner_directory = os.path.join(metadata_folder, folder)
if not os.path.isdir(inner_directory):
continue
for file in os.listdir(inner_directory):
entry = "{path_in_bundle}:{physical_file_path}".format(
path_in_bundle=os.path.join(folder, file),
physical_file_path=os.path.join(inner_directory, file))
metadata.append(entry)
return metadata
def get_min_sdk_version(bundle_path: str, bundletool: str) -> int:
"""Get the minimum supported SDK version from an App Bundle file."""
bundletool_cmd = [
"java", "-jar", bundletool, "dump", "manifest", "--bundle", bundle_path,
"--xpath", "/manifest/uses-sdk/@android:minSdkVersion"
]
print("Running {bundletool_cmd}".format(bundletool_cmd=bundletool_cmd))
min_sdk = subprocess.check_output(bundletool_cmd)
return int(min_sdk.decode("utf-8").rstrip())
def get_strip_tcf_suffixes(args: argparse.Namespace) -> bool:
return args.striptcfsuffixes
def get_compress_install_time_assets(args: argparse.Namespace) -> bool:
return args.compressinstalltimeassets
def get_asset_pack_type(path: str) -> str:
"""Retrieve the Asset Pack delivery type from an AndroidManifest.xml file."""
xmldoc = xml.dom.minidom.parse(path)
tags = xmldoc.getElementsByTagName(ONDEMAND)
if tags.length:
return ONDEMAND
tags = xmldoc.getElementsByTagName(FASTFOLLOW)
if tags.length:
return FASTFOLLOW
tags = xmldoc.getElementsByTagName(UPFRONT)
if tags.length:
return UPFRONT
return None
def extract_bundle_config(bundle_folder: str, add_standalone_config: bool,
strip_tcf_suffixes: bool,
compress_install_time_assets: bool) -> str:
"""Extract the BundleConfig contents and optionally add standalone_config."""
bundle_config = config_pb2.BundleConfig()
with open(os.path.join(bundle_folder, "BundleConfig.pb"), mode="rb") as f:
content = f.read()
bundle_config.ParseFromString(content)
if compress_install_time_assets:
json_format.ParseDict(
{
"compression": {
"install_time_asset_module_default_compression": "COMPRESSED"
}
}, bundle_config)
if add_standalone_config:
json_format.ParseDict(
{
"optimizations": {
"standalone_config": {
"split_dimension": [{
"value": "ABI",
"negate": True
}, {
"value": "TEXTURE_COMPRESSION_FORMAT",
"negate": True
}, {
"value": "LANGUAGE",
"negate": True
}, {
"value": "SCREEN_DENSITY",
"negate": True
}],
"strip_64_bit_libraries": True
}
}
}, bundle_config)
# Check if game already defines any split_config dimensions
dimensions = []
try:
dimensions = list(json_format.MessageToDict(bundle_config)["optimizations"]
["splitsConfig"]["splitDimension"])
except KeyError:
print("No existing split dimensions")
tcf_split_dimension = {
"value": "TEXTURE_COMPRESSION_FORMAT",
"negate": False,
"suffix_stripping": {
"enabled": True,
"default_suffix": ""
}
}
# Add the TCF split dimension, if needed
if strip_tcf_suffixes:
dimensions.append(tcf_split_dimension)
if strip_tcf_suffixes:
json_format.ParseDict(
{
"optimizations": {
"splits_config": {
"split_dimension": dimensions
}
}
}, bundle_config)
output_path = os.path.join(bundle_folder, "BundleConfig.pb.json")
with open(output_path, mode="w") as f:
print(json_format.MessageToJson(bundle_config), file=f)
return output_path
def aapt_link(input_manifest_path: str, output_manifest_folder: str,
aapt2_bin_path: str, sdk_jar_path: str):
"""Run aapt link to convert the manifest to proto format."""
aapt_cmd = [
aapt2_bin_path, "link", "--proto-format", "--output-to-dir", "-o",
output_manifest_folder, "--manifest", input_manifest_path, "-I",
sdk_jar_path
]
print(" Running {aapt_cmd}".format(aapt_cmd=aapt_cmd))
exit_code = subprocess.call(aapt_cmd)
if exit_code != 0:
print(
"Error executing {aapt_cmd}".format(aapt_cmd=aapt_cmd), file=sys.stderr)
sys.exit(-1)
def process_packs(packs_folder: str, bundle_folder: str,
pack_names: typing.List[str], aapt2_bin_path: str,
sdk_jar_path: str) -> bool:
"""Repackage all packs into modules."""
print("Processing packs...")
has_upfront_pack = False
for pack_name in pack_names:
print(" Pack {pack_name}".format(pack_name=pack_name))
pack_basename = os.path.splitext(pack_name)[0]
pack_folder = os.path.join(bundle_folder, pack_basename)
os.makedirs(pack_folder)
print(" Extracting pack {pack_name} to {pack_folder}.".format(
pack_name=pack_name, pack_folder=pack_folder))
pack_zip_path = zipfile.ZipFile(
os.path.join(packs_folder, pack_name), "r")
pack_zip_path.extractall(path=pack_folder)
pack_zip_path.close()
print(" Processing manifest.")
manifest_folder = os.path.join(pack_folder, "manifest")
original_manifest_path = os.path.join(manifest_folder,
"AndroidManifest.xml")
has_upfront_pack = has_upfront_pack or (
get_asset_pack_type(original_manifest_path) == UPFRONT)
tmp_manifest_path = os.path.join(bundle_folder, "manifest.xml")
shutil.move(original_manifest_path, tmp_manifest_path)
aapt_link(tmp_manifest_path, manifest_folder,
aapt2_bin_path, sdk_jar_path)
print(" Cleaning up\n")
os.remove(os.path.join(manifest_folder, "resources.pb"))
os.remove(tmp_manifest_path)
return has_upfront_pack
def clear_autogenerated_bundle_files(bundle_folder: str):
print("Removing old META_INF and BundleConfig.pb")
shutil.rmtree(os.path.join(bundle_folder, "META-INF"), ignore_errors=True)
os.remove(os.path.join(bundle_folder, "BundleConfig.pb"))
print("Removing old __MACOSX folders")
purge_subdirs(bundle_folder, "__MACOSX")
print("Removing old .DS_Store files")
purge_files(bundle_folder, ".DS_Store")
def zip_module(module_folder: str, bundle_folder: str) -> str:
print(" Module {module_folder}".format(module_folder=module_folder))
basename = os.path.join(bundle_folder, module_folder)
module_zip = shutil.make_archive(basename, "zip", root_dir=basename)
return module_zip
def build_bundle(module_zip_files: typing.List[str], output_path: str,
bundle_config_path: str, metadata: typing.List[str],
bundletool_path: str):
"""Build the bundle using bundletool build-bundle."""
bundletool_cmd = [
"java", "-jar", bundletool_path, "build-bundle", "--modules",
",".join(module_zip_files), "--output", output_path, "--config",
bundle_config_path
]
for entry in metadata:
bundletool_cmd.append("--metadata-file")
bundletool_cmd.append(entry)
print("Running {bundletool_cmd}".format(bundletool_cmd=bundletool_cmd))
exit_code = subprocess.call(bundletool_cmd)
if exit_code != 0:
print(
"Error executing {bundletool_cmd}".format(
bundletool_cmd=bundletool_cmd),
file=sys.stderr)
sys.exit(-1)
def main() -> None:
args = parse_args()
bundle_path = get_input_bundle_path(args)
output_path = get_output_path(args)
aapt2_bin_path = get_aapt2_bin_path(args)
sdk_jar_path = get_sdk_jar_path(args)
bundletool_path = get_bundletool_path(args)
(pack_dir, pack_names) = get_packs(args)
strip_tcf_suffixes = get_strip_tcf_suffixes(args)
compress_install_time_assets = get_compress_install_time_assets(args)
with tempfile.TemporaryDirectory() as bundle_folder:
print("Extracting input app bundle to {bundle_folder}".format(
bundle_folder=bundle_folder))
bundle_zip_path = zipfile.ZipFile(bundle_path, "r")
bundle_zip_path.extractall(path=bundle_folder)
bundle_zip_path.close()
has_upfront_pack = process_packs(pack_dir, bundle_folder, pack_names,
aapt2_bin_path, sdk_jar_path)
uses_upfront_pre_l = has_upfront_pack and get_min_sdk_version(
bundle_path, bundletool_path) < 21
bundle_config_path = extract_bundle_config(bundle_folder,
uses_upfront_pre_l,
strip_tcf_suffixes,
compress_install_time_assets)
clear_autogenerated_bundle_files(bundle_folder)
print("Parsing bundle metadata...")
metadata = parse_bundle_metadata(bundle_folder)
print("Zipping module folders...")
metadata = []
module_folders = (
module_folder for module_folder in os.listdir(bundle_folder)
if module_folder != "BUNDLE-METADATA" and
os.path.isdir(os.path.join(bundle_folder, module_folder)))
module_zip_files = [
zip_module(module_folder, bundle_folder)
for module_folder in module_folders
]
bundletool_cmd = [
"java", "-jar", bundletool_path, "build-bundle", "--modules",
",".join(module_zip_files), "--output", output_path, "--config",
bundle_config_path
]
for entry in metadata:
bundletool_cmd.append("--metadata-file")
bundletool_cmd.append(entry)
print("Running {bundletool_cmd}".format(bundletool_cmd=bundletool_cmd))
exit_code = subprocess.call(bundletool_cmd)
if exit_code != 0:
print(
"Error executing {bundletool_cmd}".format(
bundletool_cmd=bundletool_cmd),
file=sys.stderr)
sys.exit(-1)
print("Augmented app bundle is ready at {output_path}".format(
output_path=output_path))
if __name__ == "__main__":
main()
|