Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/launchpad/utils/android/bundletool.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -170,6 +170,10 @@ def build_apks(
with tempfile.TemporaryDirectory(prefix="bundletool-") as temp_dir:
temp_dir_path = Path(temp_dir)
temp_apks_path = temp_dir_path / "apks.apks"
device_spec_path = temp_dir_path / "device-spec.json"
with open(device_spec_path, "w", encoding="utf-8") as device_spec_file:
json.dump(device_spec.model_dump(by_alias=True), device_spec_file)

build_apks_command = [
"build-apks",
f"--bundle={bundle_path}",
Expand All@@ -178,6 +182,10 @@ def build_apks(

if universal_apk:
build_apks_command.append("--mode=universal")
else:
# Restrict bundletool to APKs matching the target device instead
# of generating every possible split before extract-apks filters them.
build_apks_command.append(f"--device-spec={device_spec_path}")

# Generate keystore and sign APKs if requested
if sign_apks:
Expand All@@ -195,10 +203,6 @@ def build_apks(

logger.debug("APKs will be signed with generated keystore")

device_spec_path = temp_dir_path / "device-spec.json"
with open(device_spec_path, "w", encoding="utf-8") as device_spec_file:
json.dump(device_spec.model_dump(by_alias=True), device_spec_file)

self._run_command(build_apks_command)

# Extract APKs for the specified device
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/utils/android/test_bundletool.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
from pathlib import Path

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably this should live at:

  • tests/unit/utils/android/test_bundletool.py rather than:
  • tests/unit/utils/test_bundletool.py so as to match:
  • src/launchpad/utils/android/bundletool.py

...but I'm not sure how closely we've followed that generally and the src/launchpad/utils/android/ dir does not currently exist.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense – will update it


import pytest

from launchpad.utils.android.bundletool import Bundletool, DeviceSpec


@pytest.fixture
def bundletool(mocker) -> Bundletool:
mocker.patch("launchpad.utils.android.bundletool.shutil.which", return_value="/usr/local/bin/bundletool")
tool = Bundletool()
mocker.patch.object(tool, "_generate_keystore", return_value=("password", "alias"))
mocker.patch.object(tool, "_run_command")
return tool


def test_build_apks_targets_device_during_build(bundletool: Bundletool, tmp_path: Path) -> None:
bundletool.build_apks(
bundle_path=tmp_path / "app.aab",
output_dir=tmp_path / "output",
device_spec=DeviceSpec(),
)

build_command = bundletool._run_command.call_args_list[0].args[0]
extract_command = bundletool._run_command.call_args_list[1].args[0]

assert "--mode=universal" not in build_command
device_spec_arg = next(argument for argument in build_command if argument.startswith("--device-spec="))
assert device_spec_arg in extract_command


def test_build_universal_apk_does_not_target_device_during_build(bundletool: Bundletool, tmp_path: Path) -> None:
bundletool.build_apks(
bundle_path=tmp_path / "app.aab",
output_dir=tmp_path / "output",
device_spec=DeviceSpec(),
universal_apk=True,
)

build_command = bundletool._run_command.call_args_list[0].args[0]
extract_command = bundletool._run_command.call_args_list[1].args[0]

assert "--mode=universal" in build_command
assert not any(argument.startswith("--device-spec=") for argument in build_command)
assert any(argument.startswith("--device-spec=") for argument in extract_command)
Loading