294 lines
15 KiB
Python
294 lines
15 KiB
Python
#!/usr/bin/env python3
|
|
"""Build, sign and verify Choano's Android APKs on Kryon's Android host.
|
|
|
|
python3 scripts/android.py build [--edition direct|gplay]
|
|
python3 scripts/android.py sign APK --output SIGNED.apk [--edition gplay]
|
|
python3 scripts/android.py verify APK [--edition gplay]
|
|
|
|
The normal APK, also the F-Droid download, holds no Google library: Choano
|
|
has no self-update, so one build serves both. Only the Google Play edition
|
|
applies Kryon's android-gplay.gradle, which adds Play Billing.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import os
|
|
import re
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import zipfile
|
|
|
|
from release import ReleaseError, android_code, release_version
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PACKAGE = 'xyz.waozi.choano'
|
|
ABIS = 'arm64-v8a,armeabi-v7a'
|
|
# The Choano release key (backup/android-signing, GitHub secret
|
|
# CHOANO_ANDROID_KEYSTORE).
|
|
CERTIFICATE_SHA256 = '181c2124b549b190d4bfebddf0335eb276ba71877957decafcb0f6a408f91694'
|
|
ZIRAN = os.environ.get('ZIRAN', 'ziran')
|
|
LOCKED = [] if (ROOT / 'ziran.local.toml').exists() else ['--locked']
|
|
OUT = ROOT / 'build/android'
|
|
|
|
|
|
def run(*args, **kwargs):
|
|
subprocess.run(list(map(str, args)), check=True, **kwargs)
|
|
|
|
|
|
def package(name):
|
|
return Path(subprocess.check_output([ZIRAN, 'pkg', 'path', name, *LOCKED], text=True, cwd=ROOT).strip())
|
|
|
|
|
|
def sdk():
|
|
return Path(os.environ.get('ANDROID_SDK_ROOT') or os.environ.get('ANDROID_HOME') or Path.home() / 'Android/Sdk')
|
|
|
|
|
|
def version():
|
|
"""The newest released changelog version and its versionCode; 0.0.0
|
|
before the first release."""
|
|
try:
|
|
name = release_version((ROOT / 'CHANGELOG.md').read_text())
|
|
except (OSError, ReleaseError):
|
|
return '0.0.0', 1
|
|
return name, android_code(name)
|
|
|
|
|
|
def launcher_icons(res):
|
|
"""The launcher icon at each density: the player amoeba in Choano's water."""
|
|
from PIL import Image
|
|
sheet = Image.open(ROOT / 'assets/choano/organisms-hd.avif').convert('RGBA')
|
|
cell = sheet.crop((0, 0, sheet.width // 4, sheet.height // 4))
|
|
player = cell.crop(cell.getchannel('A').point(lambda a: 255 if a > 32 else 0).getbbox())
|
|
water = Image.open(ROOT / 'assets/choano/icon.png').convert('RGB').getpixel((5, 40))
|
|
for density, size in (('mdpi', 48), ('hdpi', 72), ('xhdpi', 96), ('xxhdpi', 144), ('xxxhdpi', 192)):
|
|
icon = Image.new('RGBA', (size, size), water + (255,))
|
|
scale = 0.72 * size / max(player.size)
|
|
sprite = player.resize((round(player.width * scale), round(player.height * scale)), Image.LANCZOS)
|
|
icon.alpha_composite(sprite, ((size - sprite.width) // 2, (size - sprite.height) // 2))
|
|
folder = res / f'mipmap-{density}'
|
|
folder.mkdir(parents=True, exist_ok=True)
|
|
icon.convert('RGB').save(folder / 'ic_launcher.png', optimize=True)
|
|
|
|
|
|
def scaffold(kryon):
|
|
"""droid/ from the locked Kryon, with Choano's Gradle build and manifest.
|
|
Kryon's scaffold keeps existing files, so stale ones go first; Gradle's
|
|
caches stay."""
|
|
droid = ROOT / 'droid'
|
|
if droid.exists():
|
|
for path in droid.iterdir():
|
|
if path.name in ('.gradle', 'build'):
|
|
continue
|
|
if path.name == 'app':
|
|
for inner in path.iterdir():
|
|
if inner.name != 'build':
|
|
shutil.rmtree(inner) if inner.is_dir() else inner.unlink()
|
|
continue
|
|
shutil.rmtree(path) if path.is_dir() else path.unlink()
|
|
run(sys.executable, kryon / 'scripts/android_scaffold.py', '--root', ROOT, '--name', 'Choano',
|
|
'--package', PACKAGE, '--activity', 'MainActivity', '--kryon', kryon)
|
|
packaging = ROOT / 'packaging/android'
|
|
shutil.copyfile(packaging / 'build.gradle', droid / 'app/build.gradle')
|
|
shutil.copyfile(packaging / 'AndroidManifest.xml', droid / 'app/src/main/AndroidManifest.xml')
|
|
(droid / 'gradle.properties').write_text(
|
|
'org.gradle.daemon=false\norg.gradle.parallel=false\norg.gradle.workers.max=1\n'
|
|
'org.gradle.jvmargs=-Xmx1536m -Dfile.encoding=UTF-8\n')
|
|
|
|
|
|
ARTIFACT_PLATFORMS = {'arm64-v8a': ('android-aarch64-bionic', 'aarch64-linux-android'),
|
|
'armeabi-v7a': ('android-armv7-bionic', 'armv7a-linux-androideabi')}
|
|
ACCOUNT_MARKER = '\n# Choano account provider (scripts/android.py)\n'
|
|
|
|
|
|
def ndk_folder(android):
|
|
found = re.search(r"ndkVersion '([^']+)'", (ROOT / 'packaging/android/build.gradle').read_text())
|
|
folder = android / 'ndk' / found[1]
|
|
if not folder.is_dir():
|
|
raise SystemExit(f'Install NDK {found[1]} in {android}')
|
|
return folder
|
|
|
|
|
|
def account_provider(android, edition, abis):
|
|
"""Entity's account cryptography for each ABI, built by its own recipe with
|
|
the NDK compiler the APK uses; the CMake build links it into libmain."""
|
|
sys.path.insert(0, str(ROOT / 'scripts'))
|
|
import build as choano_build
|
|
ndk = ndk_folder(android)
|
|
tools = ndk / 'toolchains/llvm/prebuilt/linux-x86_64/bin'
|
|
# The minSdk build.gradle compiles for: Google Play needs Android 6.
|
|
api = '23' if edition == 'gplay' else '21'
|
|
rows = []
|
|
for abi in abis.split(','):
|
|
if abi not in ARTIFACT_PLATFORMS:
|
|
raise SystemExit(f'The account provider has no {abi} build; use ' + ', '.join(ARTIFACT_PLATFORMS))
|
|
platform, prefix = ARTIFACT_PLATFORMS[abi]
|
|
env = {k: v for k, v in os.environ.items() if k not in
|
|
('DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'MAKEFLAGS', 'MFLAGS', 'MAKEOVERRIDES')}
|
|
env.update(CC=str(tools / f'{prefix}{api}-clang'), AR=str(tools / 'llvm-ar'), CMAKE_BUILD_PARALLEL_LEVEL='1')
|
|
crypto = choano_build.shared_script('ziran', 'ziran_artifacts').request(
|
|
'entity:native-crypto', platform, project=ROOT, ziran=ZIRAN, env=env,
|
|
options=[f'entity:edition={"play" if edition == "gplay" else "direct"}', f'entity:ndk={ndk}',
|
|
f'entity:android_api={api}'])
|
|
flags = ' '.join('"' + flag.replace('\\', '/').replace('"', '') + '"' for flag in crypto.link_flags(env=env))
|
|
rows.append(f'if(ANDROID_ABI STREQUAL "{abi}")\n target_link_libraries(main PRIVATE {flags})\nendif()\n')
|
|
cmake = ROOT / 'droid/app/src/main/cpp/CMakeLists.txt'
|
|
text = cmake.read_text().split(ACCOUNT_MARKER)[0]
|
|
defines = 'set(APP_ZIRAN_DEFINES --define ANDROID_BUILD'
|
|
if defines not in text:
|
|
raise SystemExit("Kryon's Android CMakeLists.txt no longer sets APP_ZIRAN_DEFINES")
|
|
if 'CHOANO_ACCOUNTS' not in text:
|
|
text = text.replace(defines, defines + ' --define CHOANO_ACCOUNTS', 1)
|
|
# The scaffold's default host owns its own frame loop. Select the
|
|
# NativeActivity runtime for Choano's main, which brackets app audio.
|
|
generator = '--manifest "${APP_ZIRAN_MANIFEST}"'
|
|
host = '--host "${KRYON_DIR}/src/backend/android_runtime.zi"'
|
|
if generator not in text:
|
|
raise SystemExit("Kryon's Android CMakeLists.txt no longer generates the native sources")
|
|
if host not in text:
|
|
text = text.replace(generator, host + '\n ' + generator, 1)
|
|
cmake.write_text(text + ACCOUNT_MARKER + """
|
|
set(OPUS_DIR "$ENV{KRYON_ANDROID_OPUS_DIR}")
|
|
set(SAMPLERATE_DIR "$ENV{KRYON_ANDROID_SAMPLERATE_DIR}")
|
|
add_subdirectory("$ENV{KRYON_ANDROID_AUDIO_DIR}/native" choano-audio EXCLUDE_FROM_ALL)
|
|
target_link_libraries(main PRIVATE opus OpenSLES)
|
|
""" + ''.join(rows))
|
|
|
|
|
|
def build(edition, abis=ABIS):
|
|
run(ZIRAN, 'fetch', *LOCKED, cwd=ROOT)
|
|
kryon, compiler = package('kryon'), package('ziran')
|
|
webp, avif, aom = package('libwebp'), package('libavif'), package('libaom')
|
|
if not all((compiler / 'build/bin' / name).is_file() for name in ('ziran', 'zi2c')):
|
|
run('make', '-s', '-j2', '-C', compiler, 'build/bin/ziran', 'build/bin/zi2c', 'build/bin/zi2zir')
|
|
android = sdk()
|
|
aapt2 = android / 'build-tools/36.0.0/aapt2'
|
|
if not aapt2.is_file() or not (android / 'platforms/android-36').is_dir():
|
|
raise SystemExit(f'Install build-tools 36.0.0, platforms;android-36, cmake 3.22.1 and the NDK in {android}')
|
|
scaffold(kryon)
|
|
# The Profile bundle the account provider opens, as assets/entity.zx.
|
|
run(sys.executable, ROOT / 'scripts/build.py', 'profile-asset', cwd=ROOT)
|
|
account_provider(android, edition, abis)
|
|
# The game reads its art and chimes as assets/choano/... from the APK.
|
|
staged = OUT / 'assets'
|
|
if staged.exists():
|
|
shutil.rmtree(staged)
|
|
shutil.copytree(ROOT / 'assets', staged / 'assets')
|
|
res = OUT / 'res'
|
|
if res.exists():
|
|
shutil.rmtree(res)
|
|
launcher_icons(res)
|
|
# The startup screen shows the title art, as the title screen does.
|
|
# Android before 12 decodes no AVIF resources, so the window background
|
|
# is a JPEG made from the shipped AVIF title.
|
|
from PIL import Image
|
|
(res / 'drawable-nodpi').mkdir()
|
|
with Image.open(ROOT / 'assets/choano/title-portrait.avif') as title:
|
|
title.convert('RGB').save(res / 'drawable-nodpi/startup_art.jpg', quality=90)
|
|
name, code = version()
|
|
command = ['sh', ROOT / 'droid/gradlew', '-p', ROOT / 'droid', '--no-daemon', '--max-workers=1',
|
|
'-Pkryon.dir=' + str(kryon), '-Pziran.dir=' + str(compiler),
|
|
'-Pwebp.dir=' + str(webp), '-Pavif.dir=' + str(avif), '-Paom.dir=' + str(aom),
|
|
'-Papp.abis=' + abis, '-Papp.edition=' + edition, '-Papp.versionName=' + name,
|
|
'-Papp.versionCode=' + str(code), '-Pandroid.aapt2FromMavenOverride=' + str(aapt2)]
|
|
# Kryon's Play Billing needs AndroidX; only the Google Play edition has it.
|
|
if edition == 'gplay':
|
|
command.append('-Pandroid.useAndroidX=true')
|
|
env = dict(os.environ, ANDROID_HOME=str(android), ANDROID_SDK_ROOT=str(android), CMAKE_BUILD_PARALLEL_LEVEL='1',
|
|
GRADLE_USER_HOME=str(OUT / 'gradle-home'))
|
|
env['KRYON_PROJECT_ENTRY'] = 'src/app.zi'
|
|
# Kryon's Android CMake builds its AVIF and Opus codecs from these locked
|
|
# sources, as its own scripts/android-build.sh passes them.
|
|
for variable, name_ in (('KRYON_DAV1D_DIR', 'dav1d'), ('KRYON_AVIF_PACKAGE_DIR', 'avif'),
|
|
('KRYON_WEBP_PACKAGE_DIR', 'webp'), ('KRYON_ANDROID_AUDIO_DIR', 'audio'),
|
|
('KRYON_ANDROID_OPUS_DIR', 'libopus'), ('KRYON_ANDROID_SAMPLERATE_DIR', 'libsamplerate')):
|
|
env[variable] = str(package(name_))
|
|
for name_ in ('MAKEFLAGS', 'MFLAGS', 'MAKEOVERRIDES', 'DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY'):
|
|
env.pop(name_, None)
|
|
run(*command, 'assembleRelease', cwd=ROOT, env=env)
|
|
output = OUT / ('choano-gplay-unsigned.apk' if edition == 'gplay' else 'choano-unsigned.apk')
|
|
shutil.copyfile(ROOT / 'droid/app/build/outputs/apk/release/app-release-unsigned.apk', output)
|
|
print(f'Choano {name} ({code}) for Android, {edition} edition: {output}')
|
|
|
|
|
|
def build_tools():
|
|
folders = sorted((sdk() / 'build-tools').glob('*'), key=lambda p: tuple(int(n) for n in re.findall(r'\d+', p.name)))
|
|
return next(p for p in reversed(folders) if all((p / name).is_file() for name in ('aapt', 'apksigner', 'zipalign')))
|
|
|
|
|
|
def verify(apk, edition, tools):
|
|
name, code = version()
|
|
badging = subprocess.check_output([str(tools / 'aapt'), 'dump', 'badging', str(apk)], text=True)
|
|
found = re.search(r"^package: name='([^']+)' versionCode='(\d+)' versionName='([^']+)'", badging, re.M)
|
|
assert found and found[1] == PACKAGE and found[2] == str(code) and found[3] == name, \
|
|
'APK identity or version differs'
|
|
assert 'application-debuggable' not in badging, 'A debug build cannot be published'
|
|
google_play = edition == 'gplay'
|
|
assert ("'com.android.vending.BILLING'" in badging) == google_play, 'Only the Google Play edition may ask for Play Billing'
|
|
signature = subprocess.check_output([str(tools / 'apksigner'), 'verify', '--verbose', '--print-certs', str(apk)], text=True)
|
|
signers = re.findall(r'^(?:Signer #\d+|V[\d.]+ Signer:) certificate SHA-256 digest: ([0-9a-f]+)$', signature, re.M)
|
|
assert 'Number of signers: 1' in signature and set(signers) == {CERTIFICATE_SHA256}, \
|
|
'APK signer is not the Choano release key'
|
|
run(tools / 'zipalign', '-c', '-P', '16', '4', apk, capture_output=True)
|
|
with zipfile.ZipFile(apk) as archive:
|
|
names = archive.namelist()
|
|
abis = sorted(p.split('/')[1] for p in names if re.fullmatch(r'lib/[^/]+/libmain\.so', p))
|
|
assert abis == sorted(ABIS.split(',')), 'Both supported ARM architectures must be present'
|
|
assert 'assets/assets/choano/organisms-hd.avif' in names, "The game's artwork is missing"
|
|
assert 'assets/assets/choano/audio/eat.wav' in names, "The game's chimes are missing"
|
|
assert 'assets/assets/entity.zx' in names, 'The account Profile bundle is missing'
|
|
dex = b''.join(archive.read(n) for n in names if re.fullmatch(r'classes\d*\.dex', n))
|
|
for marker in (b'Lorg/kryonlabs/play/PlayBilling;', b'Lcom/android/billingclient/'):
|
|
assert (marker in dex) == google_play, 'Only the Google Play edition may include Play Billing'
|
|
print(f'{apk.name}: Choano {name} ({found[2]}), {edition} edition, {", ".join(abis)}, '
|
|
f'signed by {CERTIFICATE_SHA256[:12]}')
|
|
|
|
|
|
def sign(apk, output, edition, keystore, password, alias):
|
|
tools = build_tools()
|
|
output = output.resolve()
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
aligned = output.with_name(output.stem + '.aligned.apk')
|
|
signed = output.with_name(output.stem + '.new.apk')
|
|
try:
|
|
run(tools / 'zipalign', '-P', '16', '-f', '4', apk, aligned)
|
|
run(tools / 'apksigner', 'sign', '--alignment-preserved', '--ks', keystore.resolve(strict=True),
|
|
'--ks-key-alias', alias, '--ks-pass', 'file:' + str(password.resolve(strict=True)),
|
|
'--out', signed, aligned)
|
|
verify(signed, edition, tools)
|
|
signed.replace(output)
|
|
finally:
|
|
aligned.unlink(missing_ok=True)
|
|
signed.unlink(missing_ok=True)
|
|
output.with_name(signed.name + '.idsig').unlink(missing_ok=True)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
parser.add_argument('action', choices=('build', 'sign', 'verify', 'version'))
|
|
parser.add_argument('apk', type=Path, nargs='?')
|
|
parser.add_argument('--edition', choices=('direct', 'gplay'), default='direct')
|
|
parser.add_argument('--output', type=Path)
|
|
# x86_64 only for a local emulator check; releases carry both ARM ABIs.
|
|
parser.add_argument('--abis', default=ABIS)
|
|
parser.add_argument('--keystore', type=Path, default=ROOT / 'backup/android-signing/choano-release.p12')
|
|
parser.add_argument('--password-file', type=Path, default=ROOT / 'backup/android-signing/password')
|
|
parser.add_argument('--alias', default='choano')
|
|
args = parser.parse_args()
|
|
if args.action == 'version':
|
|
print(version()[0])
|
|
elif args.action == 'build':
|
|
build(args.edition, args.abis)
|
|
elif args.action == 'sign':
|
|
if not args.apk or not args.output:
|
|
parser.error('sign needs an APK and --output')
|
|
sign(args.apk.resolve(strict=True), args.output, args.edition, args.keystore, args.password_file, args.alias)
|
|
else:
|
|
if not args.apk:
|
|
parser.error('verify needs an APK')
|
|
verify(args.apk.resolve(strict=True), args.edition, build_tools())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|