onionwire/android/README.md
agent 7391c5b7a4
All checks were successful
android-ci / android (pull_request) Successful in 12m6s
ci / test (pull_request) Successful in 30m41s
ci / sdk (pull_request) Successful in 14m52s
ci: build the default graph on Rust 1.98, not 1.91
`cuprate-daemon` is a default feature, so `cargo test --locked` in the Pi CI
job compiles `tapes`. That crate calls `VecDeque::pop_front_if`, stable only
since 1.93. `rust:1.91-bookworm` dies with `error[E0658]: use of unstable
library feature 'vec_deque_pop_if'` after the rest of the graph has already
built. The Android workflows were already on 1.98 for this; ci.yml and
release.yml were left on 1.91 when the feature was off, and that is no longer
true.

`rust-version` moves with the image. cargo checks it before features, so a
1.91 compiler must be refused up front instead of fifteen minutes into
`tapes`. `--no-default-features` would still compile on 1.91; the declared
floor is the default build's floor, which is what CI and `cargo build` run.
2026-10-08 17:41:22 -04:00

388 lines
17 KiB
Markdown

# OnionWire for Android
Two artifacts out of one Gradle build:
| Module | Output | What it is |
|---|---|---|
| `:sdk` | `sdk-release.aar` | The reusable library. UniFFI Kotlin bindings + `libonionwire_sdk.so`. Any Android app can depend on this. |
| `:app` | `app-release.apk` / `app-debug.apk` | A Jetpack Compose + Material 3 messenger that depends on `:sdk` and nothing else. |
The Linux TUI in the repository root is a **separate product** with a separate
Cargo workspace. Nothing here turns it into a WebView, and nothing here imports
its TUI code. Same wire protocol: [docs/PROTOCOL.md](../docs/PROTOCOL.md).
Companion records (not duplicated here):
* in-process chain daemon in the APK — [CUPRATE_DAEMON.md](CUPRATE_DAEMON.md)
* bundled `monero-wallet-rpc` blob — [MONERO_BINARIES.md](MONERO_BINARIES.md)
* wallet / chain-source behaviour (core + SDK) — [docs/WALLET.md](../docs/WALLET.md)
* consuming the AAR — [crates/onionwire-sdk/README.md](../crates/onionwire-sdk/README.md)
* how a maintainer actually cuts a release — [docs/RELEASING.md](../docs/RELEASING.md)
## Requirements
* JDK 17+ (built and tested on JDK 21)
* Android SDK with **platform 36** and **build-tools 36.0.0**
* NDK **28.2.13676358** (NDK r28c) — also what Rust's Android link step uses
* Rust with the `aarch64-linux-android` target and `cargo-ndk`
* `ANDROID_HOME` set, or `sdk.dir` in `android/local.properties` (gitignored)
One-time setup:
```bash
sdkmanager --install "platform-tools" "platforms;android-36" \
"build-tools;36.0.0" "ndk;28.2.13676358"
rustup target add aarch64-linux-android x86_64-linux-android
cargo install cargo-ndk
```
## Build
```bash
cd android
./gradlew :sdk:assembleRelease :app:assembleDebug # or :app:assembleRelease
```
The Rust cross-compile and the UniFFI Kotlin bindings are wired into the Gradle
tasks — there is no manual codegen step. `:sdk:preBuild` depends on
`cargoBuildAndroid`, `uniffiBindgen`, and `verifyDynamicLibc`.
Emulator / x86_64 slice:
```bash
./gradlew :app:assembleDebug -Ponionwire.abis=arm64-v8a,x86_64
```
That extra ABI does **not** ship `libmonerowalletrpc.so` for x86_64. The wallet
screen says so (`NO_WALLET_RPC_MESSAGE`) instead of offering a Start that cannot
work. See [MONERO_BINARIES.md](MONERO_BINARIES.md).
Or from the repo root, with checksums and an optional publish step:
```bash
scripts/build-android-local.sh # -> dist/onionwire-<v>-android-arm64-v8a.apk
PUBLISH_TAG=v0.14.0 scripts/build-android-local.sh
```
The script re-verifies `android/monero-binaries.sha256` before Gradle, then
again against the bytes inside the APK. It does not create tags.
## Decisions you should know about
### `minSdk = 26` (Android 8.0)
The floor is not NDK-imposed — Rust's `aarch64-linux-android` std and NDK 28
both happily target API 21. We take 26 deliberately:
* it is the first API level where adaptive icons and notification channels are
unconditional, so there is one icon path instead of two;
* it keeps a single `arm64-v8a` APK inside Play's currently-supported range
without carrying legacy ART/JIT workarounds for pre-O devices;
* the crypto stack (`ring`, `rustls`, `argon2`, `chacha20poly1305`) is
exercised on 4.4+ kernels here, which is what we actually test.
**Devices cut:** Android 7.1 and older. If you need those, the change is
`-Ponionwire.api=24` **and** a real device test — do not bump it silently.
### 16 KB page size
The NDK r28 link step emits `max-page-size=16384` for 64-bit Android, which is
what Play requires for new apps. Verify with:
```bash
readelf -l android/sdk/build/rustJniLibs/arm64-v8a/libonionwire_sdk.so | grep LOAD
```
### Permissions
`INTERNET` is the only permission requested at first launch. No camera (invites
are pasted, not scanned), no contacts, no storage permission — the data
directory is `context.filesDir`, which is already app-private.
The node is **not** process-lifetime-only. After unlock, `KeepAliveService` is
a foreground service (`specialUse`, not `dataSync`) with an ongoing
notification so the onion stays published across Doze and backgrounding. The
manifest therefore also declares:
* `FOREGROUND_SERVICE`
* `FOREGROUND_SERVICE_SPECIAL_USE`
* `POST_NOTIFICATIONS` (API 33+; denial hides the drawer notification, it does
not stop the service)
`NodeHost` owns the single `Wire` handle. The service never does. Lock, or the
notification's Stop action, drops the keep-alive anchor; the node stays up
only while another owner (the UI) still holds it. `START_NOT_STICKY`: a
restarted empty service would show "reachable" over a dead onion.
### Data at rest
`context.filesDir/onionwire/` (mode 0700), holding `onionwire.db` and the Arti
state dir. `android:allowBackup="false"` plus explicit `data_extraction_rules`
exclusions, because an auto-backup of `onionwire.db` would hand the wrapped
message key *and* the plaintext identity keys to Google Drive. Chat bodies are
encrypted at rest; identity keys are not — that is the locked v1 posture.
### The store passphrase at rest
Typing the passphrase on every launch is no longer required. On first unlock
the passphrase can be remembered (checkbox, on by default), and it is then
stored as an AES-256-GCM blob:
* the wrapping key is generated inside the **Android Keystore** under alias
`onionwire.store`, AES-256-GCM, `setUserAuthenticationRequired(false)` —
deliberately **not** biometric-gated, because a fingerprint prompt would turn
"the node opens or tells you why it did not" into "the node is unusable";
* **StrongBox is requested first** (`setIsStrongBoxBacked(true)`, guarded by
`FEATURE_STRONGBOX_KEYSTORE` on API 28+) and falls back to an ordinary
TEE-backed or software Keystore key when the chip refuses AES-256
(`StrongBoxUnavailableException is a ProviderException`). The fallback is
reported, not hidden: the lock screen prints what `KeyInfo` actually said,
and a software key is labelled **NOT hardware-backed**;
* only ciphertext + IV reach disk, at
`filesDir/secrets/store-passphrase.bin` — an `OWSEAL1` envelope of
magic + IV + ciphertext + GCM tag. No second copy, no `SharedPreferences`,
no plaintext in logs, and the typed `CharArray` is zeroed as soon as the
unlock call returns. The text field is a plain `remember`, so the typed
passphrase is never written into saved instance state;
* **Keystore invalidation is a supported path, not a crash.** A factory reset,
an app-data wipe, or a key the platform invalidates leaves a blob that cannot
authenticate: `StoredPassphraseStore.load()` returns `Unavailable`, the lock
screen explains it, and the user types the passphrase again. A failed read
does **not** delete the blob — a transient Keystore failure must not destroy
the only copy;
* `forget` deletes the blob **and** the Keystore key.
The one plaintext copy this cannot erase is the immutable `String` the UniFFI
binding takes (`openWire(home, passphrase: String)`). A follow-up SDK change
should accept the key (or a `ByteArray`) so the passphrase never becomes a
`String` at all.
`app/src/test/.../crypto/` covers the envelope and the file handling on the JVM
(the real `StoreSecret` code, with a JVM AES key standing in). The Keystore
provider only exists on a device:
```bash
./gradlew :app:connectedDebugAndroidTest # prints the KeyInfo/hardware report
```
### Signing
Release APKs are signed with a **repo-owned keystore**, not the debug key.
* CI reads `ANDROID_KEYSTORE_BASE64`, `ANDROID_KEYSTORE_PASSWORD`,
`ANDROID_KEY_ALIAS`, and `ANDROID_KEY_PASSWORD` from Forgejo repo secrets
and writes them to `~/.android/onionwire-release.env` for the build.
* Locally, `scripts/build-android-local.sh` sources that same env file if
present (`~/.android/onionwire-release.jks` + `.env`, mode 600). Local and
CI APKs then verify as the same cert and can upgrade in place.
* If the env file is missing, Gradle **warns** and signs with the debug
keystore so a laptop build still installs. CI's `android-release.yml` sets
`ONIONWIRE_REQUIRE_RELEASE_KEY=true` and **fails closed** instead — a
per-machine debug key would make every CI APK uninstallable over the last.
`android-ci.yml` deliberately does **not** set that: it is a verification
build and `apksigner` reporting the debug cert there is expected.
* Never commit a keystore, password, or `android/local.properties`. This is
not a Play upload key; nothing here should be uploaded to Play.
### Release builds are not minified
`isMinifyEnabled = false` for release. Keeps for the UniFFI/JNI surface already
exist in `sdk/consumer-rules.pro`, but enabling R8 without a mapping-file upload
path and an on-device test of the JNI surface is how you ship an
`UnsatisfiedLinkError` nobody can read. Turn it on together with crash
deobfuscation, not before.
## Wallet child and chain sources (as Android sees them)
Chain traffic is **off** until the user opens Wallet, picks a source, and
presses Start. Receive-only is the default: no `monero-wallet-rpc` child, no
daemon, no P2P.
The app owns `monero-wallet-rpc` (issue #107). It is still a **child process**,
exec'd from `applicationInfo.nativeLibraryDir/libmonerowalletrpc.so`. Cuprate
has no wallet layer — an in-process node does not replace that binary.
Sources the screen offers:
1. **Receive-only** — no child, no chain.
2. **A daemon address you type** — loopback or clearnet. Wallet RPC itself is
loopback only. A `.onion` host is refused while `libarti.so` is absent
(`ONION_NEEDS_ARTI_NOTICE`); this APK does not bundle `arti`.
3. **On-device node (Cuprate)** — opt-in. Selecting it stores
`ChainChoice.InProcessCuprate` with an empty `monerod_datadir`, which the
SDK resolves to `$ONIONWIRE_HOME/chain`. Kotlin never invents an absolute
path. The pre-production warning (`CUPRATE_PREPRODUCTION_WARNING`) sits
**above Start**, not after. The screen can store testnet, mainnet-pruned, or
mainnet-full (and applies the matching storage floor); **mainnet remains
unvalidated** and the warning says so. Cross-compiled is not device-proven.
See [CUPRATE_DAEMON.md](CUPRATE_DAEMON.md).
Start is gated on the bundled wallet-rpc binary, on the storage floor for the
chosen Cuprate mode, and on the `.onion`/arti check. The floors are floors, not
sync estimates — nothing in this stack exposes a chain size:
| Mode | Floor |
|---|---|
| testnet | 3 GiB |
| mainnet pruned | 64 GiB |
| mainnet full | 280 GiB |
Unreadable free space (`StatFs` failed) is fail-closed: Start stays blocked.
### Status line
`ui/WalletChildStatusText.kt` is the pure model. The line renders the core's
words verbatim (`off` / `connecting` / `syncing` / `ready` / `exited`), then
the chain-source label, then — for the in-process source only — `node <word>`
from `WalletChildStatus.node_state`. An unrecognised word is never rendered as
ready.
When the last probe reported a non-zero height or target, the line grows
` · sync h/t`. That is the **daemon's** chain height, not the wallet's scan
height. `0/0` is omitted (unknown), not printed as 0%. There is **no
percentage, no progress bar, and no ETA** anywhere in the UI. The same string
is the row's screen-reader `contentDescription`.
Honest limit of the in-process node word: a daemon that dies *after* the wallet
child came up cannot be reported `exited` — there is no separate pid for the
in-process daemon, the chain probe fails, and the state stays `syncing`.
`exited` still means the `monero-wallet-rpc` child is gone. Details:
[CUPRATE_DAEMON.md](CUPRATE_DAEMON.md#what-has-not-been-proven).
### What the APK refresh loop actually calls
`WireViewModel.openWalletChild()` runs `walletChildRefreshLoop` with a 2 s
tick (`WALLET_CHILD_TICK_MS = 2_000`):
1. **First paint** — `paintWalletChildCached()` → `Wire.walletStatus()`
(`Wire::wallet_status`). Cached, no dial, so the screen has a record
while the first probe is still in flight.
2. **Every tick after that** — `refreshWalletChild()` →
`Wire.walletChildRefresh()` (`Wire::wallet_child_refresh`). That dials
the child's loopback wallet RPC, and the configured local daemon's
`get_info` when the wallet RPC does not answer, which is what makes
`sync h/t` move without another Start. It never spawns a child. With no
live child the core short-circuits to the cached `off` / `exited`
snapshot, so this cannot invent `ready`.
The loop suspends on each read before it sleeps: a probe slower than 2 s
delays the next tick instead of overlapping it. Do not run two concurrent
`wallet_child_refresh` calls against one child.
See [docs/WALLET.md](../docs/WALLET.md) (Android / SDK) for the same two
methods as the UniFFI surface.
## CI and releases
A merge to `main` does **not** publish an APK. Neither does a tag push.
Releases are manual and batched: two dispatches. Full procedure:
[docs/RELEASING.md](../docs/RELEASING.md).
### `android-ci.yml` (verification, not a release)
Runs on `docker-amd64` (T480 dind) for every `push`/`pull_request` whose paths
include `android/**`, `src/**`, `crates/**`, `Cargo.toml`, `Cargo.lock`,
`scripts/build-android-local.sh`, or the workflow itself — plus
`workflow_dispatch`. Job image is `node:20-bullseye` (EOL) — **do not apt-get
there**. The toolchain runs in a sibling `debian:bookworm` container
(stdin-tar + `docker cp`). Both Android workflows install `cmake` in that
container (RandomX).
It calls `scripts/build-android-local.sh` with the **debug** key
(`ONIONWIRE_REQUIRE_RELEASE_KEY` unset). Nothing is published. After the APK
is built it greps `lib/arm64-v8a/libonionwire_sdk.so` for `target_max_memory`
and **fails** if that cuprated-only string is missing.
### `android-release.yml` (the APK that lands on a tag)
`on:` is `workflow_dispatch` (and a declared `workflow_call` that this Forgejo
10.0.3 instance cannot invoke). **No `push` trigger.** Inputs:
| Input | Default | Meaning |
|---|---|---|
| `tag` | empty | Existing tag to check out, build, and attach. |
| `dry_run` | `false` | Build and checksum; skip the Pi publish job. |
Operator path, after `release.yml` has minted the tag (`dry_run=true` on that
workflow first):
1. Actions → **android-release** → Run workflow, `tag=vX.Y.Z` (the tag core
just created), `dry_run=false`.
2. The Android job **checks out that tag** (`ref:` is the tag, not `main`) and
stamps `versionName` / `versionCode` from it.
3. `versionCode` is `major*10000 + minor*100 + patch` (so 0.14.0 → 1400).
4. Publish is a second job on the Pi runner (`docker`). It attaches
`onionwire-<version>-android-arm64-v8a.apk`, `onionwire-sdk-<version>.aar`,
and both `.sha256` files to **that same tag**.
The APK is built from **the tag's tree**. Building from `main` and stamping a
tag you did not check out is how you ship a binary that does not match the
tag.
**Empty `tag` is the foot-gun.** Checkout then falls back to `github.sha`
(usually `main` HEAD). Version is *not* "latest `v*.*.*` plus patch" as a
rule:
* no tags at all → `0.1.0`;
* latest `v*.*.*` release is **missing** the Android APK (or the job cannot
inspect the release) → **join** that version: same `versionName` restamped
onto whatever tree was checked out. Android will refuse to install that APK
over an already-installed build of the same `versionName`/`versionCode`;
* latest tag already has an Android APK → `patch+1` of that version, still
built from `github.sha`, and this workflow may then **create** that new tag
at publish time if it does not exist.
Do not dispatch `android-release` with an empty `tag` to ship a release. Core
owns minting (`release.yml`, default `bump=minor`). This workflow attaches.
`workflow_dispatch` with `dry_run=true` builds and checksums without
publishing. `scripts/build-android-local.sh` remains the offline fallback; it
does not create tags.
The Android job image / bookworm-container story is the same as `android-ci.yml`.
The cuprated graph is built with Rust 1.98 here, the same floor as `ci.yml`
(`rust-version = "1.98"`). Job timeout is **90** minutes on `android-release.yml` and **120**
on `android-ci.yml`.
## Layout
```
android/
settings.gradle.kts
build.gradle.kts # plugin aliases only
gradle/libs.versions.toml # version catalog
gradle/wrapper/
monero-binaries.sha256 # provenance of the bundled wallet-rpc blob
CUPRATE_DAEMON.md
MONERO_BINARIES.md
sdk/ # -> AAR
build.gradle.kts # cargo-ndk + uniffi-bindgen wiring
consumer-rules.pro
src/main/AndroidManifest.xml
app/ # -> APK
build.gradle.kts
src/main/AndroidManifest.xml
src/main/java/com/siriusdevops/onionwire/
MainActivity.kt # NavHost
WireViewModel.kt # UI-side Wire handle; wallet-child poller
crypto/ # Keystore-wrapped store passphrase
keepalive/ # NodeHost, KeepAliveService, policy, lifetime
wallet/ # ChainStorage, WalletChildBinaries
ui/ # Compose screens (M3)
WalletScreen.kt
WalletChildStatusText.kt
ChatScreen.kt
RosterScreen.kt
InviteScreens.kt
InviteAddFlow.kt
OutboxDetail.kt
MessageDelivery.kt
UnlockScreen.kt
UnlockSubmit.kt
RotateScreen.kt
Insets.kt
Theme.kt
src/test/ # JVM: envelope, store, invite, outbox, keepalive, wallet line, refresh loop
src/androidTest/ # device-only: AndroidKeyStore
src/main/jniLibs/arm64-v8a/libmonerowalletrpc.so
```