onionwire/scripts/build-android-local.sh
apk-dev fcdabf8d13
All checks were successful
android-ci / android (pull_request) Successful in 10m27s
android-ci: pass --stacktrace to the release gradle build
Run 414 died in :app:packageRelease with an empty cause chain (no
Caused by: line at all), so a one-off packaging failure leaves nothing
to diagnose. --stacktrace only changes output on failure; it does not
alter the build, the artifact or the timings.

scripts/build-android-local.sh is the single gradle invocation that both
android-ci.yml and android-release.yml build through, so one flag covers
both.
2026-09-18 19:53:41 -04:00

121 lines
4.9 KiB
Shell
Executable file

#!/usr/bin/env bash
# Build the Android artifacts on this host. Offline fallback for
# .forgejo/workflows/android-release.yml (which builds on docker-amd64).
#
# Usage:
# scripts/build-android-local.sh [version] # build + checksums only
# PUBLISH_TAG=v0.1.0 scripts/build-android-local.sh # also upload
#
# Requires: ANDROID_HOME (or android/local.properties), an installed NDK, the
# Rust android targets, cargo-ndk, JDK 17+, and a working `cargo` on PATH.
# Does not create or push tags: CI owns tag creation. This script only uploads
# when PUBLISH_TAG is set.
#
# Signing: if ~/.android/onionwire-release.env exists it is sourced so local
# and CI APKs share the repo-owned keystore. Without it, Gradle falls back to
# the debug keystore and warns. Never commit a keystore or password.
set -euo pipefail
root="$(cd "$(dirname "$0")/.." && pwd)"
cd "$root"
if [ -f "$HOME/.android/onionwire-release.env" ]; then
# shellcheck disable=SC1091
set -a
# shellcheck disable=SC1090
source "$HOME/.android/onionwire-release.env"
set +a
fi
# Arg wins; otherwise the committed Gradle default (today: 0.1.0).
version="${1:-}"
if [ -z "$version" ]; then
version="$(python3 -c "
import re, pathlib
t = pathlib.Path('android/app/build.gradle.kts').read_text()
m = re.search(r'versionName\\s*=.*?:\\s*\"([^\"]+)\"', t)
print(m.group(1) if m else '')
")"
fi
[ -n "$version" ] || { echo "could not determine version" >&2; exit 1; }
if [ -z "${ONIONWIRE_VERSION_CODE:-}" ]; then
ONIONWIRE_VERSION_CODE="$(python3 -c "m,n,p=map(int,'$version'.split('.')); print(m*10000+n*100+p)")"
fi
abi="${ONIONWIRE_ANDROID_ABI:-arm64-v8a}"
# ---------------------------------------------------------------------------
# Bundled native executables. CI must not take a committed blob on trust: the
# wallet screen runs `monero-wallet-rpc` straight out of the APK's `lib/<abi>/`,
# so a corrupted or swapped blob is a wallet-level problem rather than a
# build-level one. `android/monero-binaries.sha256` is the provenance record
# (see android/MONERO_BINARIES.md); this re-verifies it before every build, and
# again against the bytes that ended up inside the APK, below.
# ---------------------------------------------------------------------------
BINARY_MANIFEST="android/monero-binaries.sha256"
[ -f "$BINARY_MANIFEST" ] || { echo "missing $BINARY_MANIFEST" >&2; exit 1; }
echo "==> verifying bundled native executables against $BINARY_MANIFEST"
( cd android && sha256sum -c "$(basename "$BINARY_MANIFEST")" )
echo "==> android build: version $version (versionCode $ONIONWIRE_VERSION_CODE), abi $abi"
# --stacktrace only changes output on failure. Without it a Gradle task failure
# can land in the CI log with an empty cause chain (e.g. run 414:
# `:app:packageRelease ... IncrementalSplitterRunnable` and no `Caused by:`),
# which makes a one-off failure undiagnosable after the fact.
( cd android && "./gradlew" --no-daemon --stacktrace \
"-Ponionwire.abis=$abi" \
"-Ponionwire.versionName=$version" \
"-Ponionwire.versionCode=$ONIONWIRE_VERSION_CODE" \
:sdk:assembleRelease :app:assembleRelease )
mkdir -p dist
apk_src="android/app/build/outputs/apk/release/app-release.apk"
aar_src="android/sdk/build/outputs/aar/sdk-release.aar"
apk="dist/onionwire-$version-android-$abi.apk"
aar="dist/onionwire-sdk-$version.aar"
for f in "$apk_src" "$aar_src"; do
[ -s "$f" ] || { echo "missing or empty build output: $f" >&2; exit 1; }
done
cp "$apk_src" "$apk"
cp "$aar_src" "$aar"
# Refuse 0-byte release assets before they ever reach a release.
for f in "$apk" "$aar"; do
bytes=$(wc -c < "$f")
[ "$bytes" -gt 0 ] || { echo "refusing to ship empty $f" >&2; exit 1; }
( cd dist && sha256sum "$(basename "$f")" > "$(basename "$f").sha256" )
done
echo "==> verify"
( cd dist && sha256sum -c ./*.sha256 )
file "$apk" "$aar"
# The bytes inside the shipped APK, not the bytes we hoped we copied. An AGP
# strip pass would rewrite `lib/<abi>/*.so` between jniLibs and the APK, which
# is why `app/build.gradle.kts` keeps debug symbols for this one entry.
echo "==> bundled wallet binary inside the APK"
bundled_sha="$(unzip -p "$apk" "lib/$abi/libmonerowalletrpc.so" | sha256sum | cut -d' ' -f1)"
committed_sha="$(sha256sum "android/app/src/main/jniLibs/$abi/libmonerowalletrpc.so" | cut -d' ' -f1)"
unzip -l "$apk" | grep "lib/$abi/" || true
echo "lib/$abi/libmonerowalletrpc.so sha256=$bundled_sha (committed $committed_sha)"
[ "$bundled_sha" = "$committed_sha" ] || {
echo "the APK's bundled binary does not match the committed blob" >&2
exit 1
}
ls -l "$apk" "$aar" "$apk.sha256" "$aar.sha256"
if [ -n "${PUBLISH_TAG:-}" ]; then
: "${FORGEJO_TOKEN:?FORGEJO_TOKEN required to publish}"
echo "==> publishing to release $PUBLISH_TAG"
scripts/publish-release.sh "$PUBLISH_TAG" "OnionWire $PUBLISH_TAG" \
scripts/release-body.md \
"$apk" "$apk.sha256" "$aar" "$aar.sha256"
else
echo "==> not publishing (set PUBLISH_TAG=<tag> and FORGEJO_TOKEN to upload)"
fi