onionwire/scripts/build-android-local.sh

121 lines
4.9 KiB
Shell
Raw Permalink Normal View History

#!/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}"
android: start/stop the monero-wallet-rpc child from WalletScreen, bundle the binary Implements the Android half of issue #107. Stacked on `wt/t_8a8ff353` (PR #108) because the UniFFI wallet-child surface is not on `main` yet. Binary ------ `android/app/src/main/jniLibs/arm64-v8a/libmonerowalletrpc.so` is a real `monero-wallet-rpc` cross-compiled for `aarch64-linux-android` from monero-project/monero v0.18.4.4 (516e5355) via upstream's own `contrib/depends` + toolchain.cmake path, then stripped with the target's own strip. 19 954 224 bytes, sha256 d9126788030ecd1ccdb40c7764e66e9a14d158a147ad335b256f0577681b5ad4. It ships through `jniLibs` and not as an asset on purpose: since API 29 the kernel refuses `exec()` on a file in the app's data directory, so the only executable directory an app has is `applicationInfo.nativeLibraryDir`, which the package manager fills from `lib/<abi>/` at install time. That requires `android:extractNativeLibs="true"` (`packaging.jniLibs.useLegacyPackaging`), which `app/build.gradle.kts` now sets, plus `keepDebugSymbols` for this one entry so AGP's strip pass cannot rewrite the bytes CI verifies. `scripts/build-monero-wallet-rpc-android.sh` reproduces the build and `android/MONERO_BINARIES.md` records the four host quirks that cost the most time (CMake must be 3.x; NDK r17b clang60 needs a libtinfo.so.5 shim; the generated toolchain.cmake puts the HOST `/usr` in CMAKE_FIND_ROOT_PATH, which breaks the link with a host libpgm.so and defines HAVE_HIDAPI against host headers; the CMake target is `wallet_rpc_server`, not `monero-wallet-rpc`). CI re-verifies the blob rather than taking it on trust: `scripts/build-android-local.sh` checks `android/monero-binaries.sha256` before Gradle, and after packaging pulls `lib/<abi>/libmonerowalletrpc.so` back out of the APK and compares its sha256 with the committed file. `arti` is NOT bundled, so a `.onion` daemon cannot be started yet. The app says that instead of pretending: `arti_bin` is set to the path the binary would occupy, and the screen refuses an `.onion` daemon while it is absent. Loopback and clearnet daemons work. UI -- `WalletScreen` gains a "wallet daemon" card above the existing Create / Import / Invoice / Tip actions, which are untouched: * a status line from `Wire::wallet_status()` rendered verbatim — `off` / `connecting` / `syncing` / `ready` / `exited` plus the core's chain-source label (`receive-only` / `local node` / `remote onion daemon` / `remote daemon`). A died child reads `exited`; it is never folded into `off` and never into `ready`; * Start / Stop calling `start_wallet_child` / `stop_wallet_child` through `WireViewModel`, on `Dispatchers.IO` (the spawn is synchronous inside the node and the status read re-decrypts the sealed record); * a `host:port` field that saves a remote daemon via `set_chain_source`, plus a Receive-only button; * honest failure states instead of a spinner: no bundled binary, no chain source, a `.onion` daemon with no arti, or a start that failed — each gets its own sentence, and Start is disabled with the reason on screen. `WireViewModel` owns the 2s wallet-child poller, the same way it owns the chat poller, so leaving the wallet screen and coming back cannot stack a second loop. `ui/WalletChildStatusText.kt` is the pure model (mirrors the `ChatRowInput`/`OutboxRowInput` convention) and carries the state mapping, the status line and the Start/Stop gates; `WalletChildStatusTextTest` covers them on the JVM. Verified -------- `./gradlew :app:testDebugUnitTest` passes, `scripts/build-android-local.sh` produces `dist/onionwire-0.1.0-android-arm64-v8a.apk` (27 884 277 bytes, versionName 0.1.0 / versionCode 100) with `lib/arm64-v8a/libmonerowalletrpc.so` inside it at the committed sha256, and `extractNativeLibs="true"` in the merged release manifest. NOT verified: the binary was never executed. The build host has no arm64-v8a AVD (all three existing AVDs are x86_64) and no aarch64 sysroot for qemu-user, so nothing here proves `monero-wallet-rpc` starts, opens its wallet file and answers JSON-RPC under Android's runtime. `android/MONERO_BINARIES.md` says so plainly and lists the emulator steps to close the gap.
2026-09-18 10:19:18 -04:00
# ---------------------------------------------------------------------------
# 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"
android: start/stop the monero-wallet-rpc child from WalletScreen, bundle the binary Implements the Android half of issue #107. Stacked on `wt/t_8a8ff353` (PR #108) because the UniFFI wallet-child surface is not on `main` yet. Binary ------ `android/app/src/main/jniLibs/arm64-v8a/libmonerowalletrpc.so` is a real `monero-wallet-rpc` cross-compiled for `aarch64-linux-android` from monero-project/monero v0.18.4.4 (516e5355) via upstream's own `contrib/depends` + toolchain.cmake path, then stripped with the target's own strip. 19 954 224 bytes, sha256 d9126788030ecd1ccdb40c7764e66e9a14d158a147ad335b256f0577681b5ad4. It ships through `jniLibs` and not as an asset on purpose: since API 29 the kernel refuses `exec()` on a file in the app's data directory, so the only executable directory an app has is `applicationInfo.nativeLibraryDir`, which the package manager fills from `lib/<abi>/` at install time. That requires `android:extractNativeLibs="true"` (`packaging.jniLibs.useLegacyPackaging`), which `app/build.gradle.kts` now sets, plus `keepDebugSymbols` for this one entry so AGP's strip pass cannot rewrite the bytes CI verifies. `scripts/build-monero-wallet-rpc-android.sh` reproduces the build and `android/MONERO_BINARIES.md` records the four host quirks that cost the most time (CMake must be 3.x; NDK r17b clang60 needs a libtinfo.so.5 shim; the generated toolchain.cmake puts the HOST `/usr` in CMAKE_FIND_ROOT_PATH, which breaks the link with a host libpgm.so and defines HAVE_HIDAPI against host headers; the CMake target is `wallet_rpc_server`, not `monero-wallet-rpc`). CI re-verifies the blob rather than taking it on trust: `scripts/build-android-local.sh` checks `android/monero-binaries.sha256` before Gradle, and after packaging pulls `lib/<abi>/libmonerowalletrpc.so` back out of the APK and compares its sha256 with the committed file. `arti` is NOT bundled, so a `.onion` daemon cannot be started yet. The app says that instead of pretending: `arti_bin` is set to the path the binary would occupy, and the screen refuses an `.onion` daemon while it is absent. Loopback and clearnet daemons work. UI -- `WalletScreen` gains a "wallet daemon" card above the existing Create / Import / Invoice / Tip actions, which are untouched: * a status line from `Wire::wallet_status()` rendered verbatim — `off` / `connecting` / `syncing` / `ready` / `exited` plus the core's chain-source label (`receive-only` / `local node` / `remote onion daemon` / `remote daemon`). A died child reads `exited`; it is never folded into `off` and never into `ready`; * Start / Stop calling `start_wallet_child` / `stop_wallet_child` through `WireViewModel`, on `Dispatchers.IO` (the spawn is synchronous inside the node and the status read re-decrypts the sealed record); * a `host:port` field that saves a remote daemon via `set_chain_source`, plus a Receive-only button; * honest failure states instead of a spinner: no bundled binary, no chain source, a `.onion` daemon with no arti, or a start that failed — each gets its own sentence, and Start is disabled with the reason on screen. `WireViewModel` owns the 2s wallet-child poller, the same way it owns the chat poller, so leaving the wallet screen and coming back cannot stack a second loop. `ui/WalletChildStatusText.kt` is the pure model (mirrors the `ChatRowInput`/`OutboxRowInput` convention) and carries the state mapping, the status line and the Start/Stop gates; `WalletChildStatusTextTest` covers them on the JVM. Verified -------- `./gradlew :app:testDebugUnitTest` passes, `scripts/build-android-local.sh` produces `dist/onionwire-0.1.0-android-arm64-v8a.apk` (27 884 277 bytes, versionName 0.1.0 / versionCode 100) with `lib/arm64-v8a/libmonerowalletrpc.so` inside it at the committed sha256, and `extractNativeLibs="true"` in the merged release manifest. NOT verified: the binary was never executed. The build host has no arm64-v8a AVD (all three existing AVDs are x86_64) and no aarch64 sysroot for qemu-user, so nothing here proves `monero-wallet-rpc` starts, opens its wallet file and answers JSON-RPC under Android's runtime. `android/MONERO_BINARIES.md` says so plainly and lists the emulator steps to close the gap.
2026-09-18 10:19:18 -04:00
# 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