109 lines
4.6 KiB
Bash
Executable File
109 lines
4.6 KiB
Bash
Executable File
#!/bin/sh
|
|
# Builds the Rust cdylib with cargo-ndk, packages it with Gradle, and
|
|
# verifies the resulting APK.
|
|
#
|
|
# Usage: ./build-apk.sh [debug|release] [--abi arm64-v8a|x86_64] [--features "a b c"]
|
|
# debug/release default to debug (matches this-machine-android's "the
|
|
# emulator stays on debug" rule -- pass `release` explicitly for a phone
|
|
# build). --abi defaults to arm64-v8a (a phone/real device); pass
|
|
# x86_64 for this checkout's own AVD. --features defaults to
|
|
# "transcript-screen". Pass "transcript-screen bench" for the retained
|
|
# benchmark app. Never force GLES for a phone or emulator build; Iris's
|
|
# runtime selects the available hardware backend.
|
|
set -eu
|
|
cd "$(dirname "$0")"
|
|
|
|
BUILD_TYPE="debug"
|
|
ABI="arm64-v8a"
|
|
FEATURES="transcript-screen"
|
|
case "${1:-}" in
|
|
debug|release) BUILD_TYPE="$1"; shift ;;
|
|
esac
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--abi) ABI="$2"; shift 2 ;;
|
|
--features) FEATURES="$2"; shift 2 ;;
|
|
*) echo "build-apk.sh: unknown argument: $1" >&2; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
SDK_ROOT="$HOME/Android/Sdk"
|
|
export ANDROID_HOME="$SDK_ROOT"
|
|
export ANDROID_SDK_ROOT="$SDK_ROOT"
|
|
NDK_DIR=$(ls -d "$SDK_ROOT"/ndk/*/ 2>/dev/null | sort -V | tail -1)
|
|
if [ -z "$NDK_DIR" ]; then
|
|
echo "build-apk.sh: no NDK found under $SDK_ROOT/ndk" >&2
|
|
exit 1
|
|
fi
|
|
export ANDROID_NDK_HOME="$NDK_DIR"
|
|
|
|
# Only the ABI asked for goes into the APK. cargo ndk adds its output beside
|
|
# whatever earlier builds left here, and Gradle packages every directory it
|
|
# finds -- a debug x86_64 emulator build left behind made an arm64 "release"
|
|
# 339 MB on 2026-09-06.
|
|
rm -rf android-project/app/src/main/jniLibs
|
|
# ...and Gradle's own copy of them, which `rm -rf jniLibs` does not reach.
|
|
# `mergeReleaseNativeLibs` is *up to date* against its cached inputs, so a
|
|
# build that switches ABI packages the previous ABI: an `--abi x86_64`
|
|
# release APK containing `lib/arm64-v8a/libmain.so` installed fine and
|
|
# aborted at startup with `Could not get adapter!: NotFound {
|
|
# active_backends: VULKAN }` under libndk_translation -- which reads
|
|
# exactly like the phone's own Vulkan problem and is nothing of the kind.
|
|
# Scoped to the merge task's directory rather than all of `app/build`, so
|
|
# an ABI change costs the native merge and not the whole Gradle build.
|
|
rm -rf android-project/app/build/intermediates/merged_native_libs \
|
|
android-project/app/build/intermediates/stripped_native_libs \
|
|
android-project/app/build/intermediates/merged_jni_libs
|
|
echo "build-apk.sh: cargo ndk -t $ABI build ${BUILD_TYPE:+(${BUILD_TYPE})} --features \"$FEATURES\""
|
|
if [ "$BUILD_TYPE" = "release" ]; then
|
|
cargo ndk -t "$ABI" -P 29 -o android-project/app/src/main/jniLibs/ build --lib \
|
|
--profile android-release --no-default-features --features "$FEATURES"
|
|
else
|
|
cargo ndk -t "$ABI" -P 29 -o android-project/app/src/main/jniLibs/ build --lib \
|
|
--profile android-dev --no-default-features --features "$FEATURES"
|
|
fi
|
|
|
|
GRADLE_TASK="assembleDebug"
|
|
APK_DIR="android-project/app/build/outputs/apk/debug"
|
|
APK_NAME="app-debug.apk"
|
|
if [ "$BUILD_TYPE" = "release" ]; then
|
|
GRADLE_TASK="assembleRelease"
|
|
APK_DIR="android-project/app/build/outputs/apk/release"
|
|
APK_NAME="app-release.apk"
|
|
export AI_APP_KEYSTORE="${XDG_CONFIG_HOME:-$HOME/.config}/ai-app/release.jks"
|
|
if [ ! -f "$AI_APP_KEYSTORE" ]; then
|
|
KEYTOOL="${JAVA_HOME:+$JAVA_HOME/bin/keytool}"
|
|
KEYTOOL="${KEYTOOL:-keytool}"
|
|
if ! command -v "$KEYTOOL" >/dev/null 2>&1; then
|
|
echo "build-apk.sh: no release key and no keytool to create one" >&2
|
|
exit 1
|
|
fi
|
|
mkdir -p "$(dirname "$AI_APP_KEYSTORE")"
|
|
AI_APP_KEYSTORE_PASSWORD=$(head -c 24 /dev/urandom | base64 | tr -d '/+=')
|
|
(umask 077 && printf '%s\n' "$AI_APP_KEYSTORE_PASSWORD" > "$AI_APP_KEYSTORE.password")
|
|
(umask 077 && "$KEYTOOL" -genkeypair -keystore "$AI_APP_KEYSTORE" \
|
|
-alias ai-app -keyalg RSA -keysize 2048 -validity 10000 \
|
|
-storepass "$AI_APP_KEYSTORE_PASSWORD" -keypass "$AI_APP_KEYSTORE_PASSWORD" \
|
|
-dname "CN=ai-app" >/dev/null 2>&1)
|
|
fi
|
|
export AI_APP_KEYSTORE_PASSWORD
|
|
AI_APP_KEYSTORE_PASSWORD=$(cat "$AI_APP_KEYSTORE.password")
|
|
fi
|
|
|
|
unset AI_APP_BENCH
|
|
case " $FEATURES " in
|
|
*" bench "*) export AI_APP_BENCH=1 ;;
|
|
esac
|
|
|
|
(cd android-project && gradle ":app:$GRADLE_TASK" --console=plain)
|
|
|
|
APK_PATH="$(pwd)/$APK_DIR/$APK_NAME"
|
|
BUILD_TOOLS=$(ls -d "$SDK_ROOT"/build-tools/*/ | sort -V | tail -1)
|
|
echo "--- aapt2 dump badging ---"
|
|
"${BUILD_TOOLS}aapt2" dump badging "$APK_PATH" | head -5
|
|
if [ "$BUILD_TYPE" = "release" ]; then
|
|
echo "--- apksigner verify ---"
|
|
"${BUILD_TOOLS}apksigner" verify --print-certs "$APK_PATH"
|
|
fi
|
|
echo "$APK_PATH"
|