Files
launcher/android/build.sh
T
nandi 2fa7bd745b
Launcher / test (push) Successful in 1m20s
Launcher / apk (push) Successful in 37s
Rewrite the launcher in MoonBit (old code moves to legacy/) (#1)
2026-11-23 01:57:10 +00:00

50 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Builds the APK without Gradle: the MoonBit page (scripts/build-web.sh) goes in as assets,
# the Java shell (android/java) is compiled with javac and d8, resources go through aapt2,
# and the result is zipaligned and signed with the committed android/debug.keystore (the
# same key every earlier build used, so each one installs over the last).
#
# android/build.sh [out.apk] # default: build/ultra-launcher.apk
#
# Needs JDK 17+, the Android SDK's build-tools and platforms;android-35 (ANDROID_SDK_ROOT,
# default ~/android-sdk) and moon (for the page). VERSION_CODE defaults to minutes since the
# epoch (always rising, and above every earlier build's); versionName comes from moon.mod.
set -euo pipefail
cd "$(dirname "$0")/.."
OUT=${1:-build/ultra-launcher.apk}
SDK=${ANDROID_SDK_ROOT:-${ANDROID_HOME:-$HOME/android-sdk}}
BT=$(ls -d "$SDK"/build-tools/* | sort -V | tail -1)
JAR=$SDK/platforms/android-35/android.jar
VERSION_NAME=$(sed -n 's/^version = "\([^"]*\)".*/\1/p' moon.mod | head -1)
VERSION_CODE=${VERSION_CODE:-$(( $(date +%s) / 60 ))}
W=build/android
mkdir -p "$(dirname "$OUT")"
echo "== page"
scripts/build-web.sh
echo "== resources (aapt2)"
rm -rf "$W" && mkdir -p "$W"/{compiled,classes,dex,gen,stage/assets}
"$BT/aapt2" compile --dir android/res -o "$W/compiled"
"$BT/aapt2" link -o "$W/base.apk" -I "$JAR" --manifest android/AndroidManifest.xml \
--min-sdk-version 26 --target-sdk-version 35 \
--version-code "$VERSION_CODE" --version-name "$VERSION_NAME" \
--java "$W/gen" "$W"/compiled/*.flat
echo "== java (javac, d8)"
javac --release 17 -Xlint:-options -classpath "$JAR" -d "$W/classes" \
$(find android/java "$W/gen" -name '*.java')
"$BT/d8" --min-api 26 --lib "$JAR" --output "$W/dex" $(find "$W/classes" -name '*.class')
echo "== package (versionCode $VERSION_CODE, versionName $VERSION_NAME)"
cp "$W/base.apk" "$W/unsigned.apk"
cp -r dist/web/. "$W/stage/assets/"
(cd "$W/dex" && zip -q -j ../unsigned.apk classes.dex)
(cd "$W/stage" && zip -q -r ../unsigned.apk assets)
"$BT/zipalign" -f -p 4 "$W/unsigned.apk" "$W/aligned.apk"
"$BT/apksigner" sign --ks android/debug.keystore --ks-pass pass:android \
--ks-key-alias androiddebugkey --key-pass pass:android --out "$OUT" "$W/aligned.apk"
"$BT/apksigner" verify --print-certs "$OUT" | head -3
echo "built $OUT"
printf '{"versionCode": %s, "commit": "%s"}\n' "$VERSION_CODE" "${GITHUB_SHA:-$(git rev-parse HEAD)}" > "$(dirname "$OUT")/version.json"