onionwire/android/MONERO_BINARIES.md
sirius e6b6bc8dd6
android: build the SDK with cuprate-daemon and offer the on-device node
Part 3 of "cuprated built in": the chain daemon now ships inside the APK and the
wallet screen can pick it.

Build (android/sdk/build.gradle.kts)
- `cargoBuildAndroid` runs `cargo ndk … build --release --lib --features
  cuprate-daemon`. The Android target only: `cargoHostLib` / `uniffiBindgen` stay
  feature-free, because the host cdylib exists only so `uniffi-bindgen --library`
  can dlopen it and the UniFFI surface does not fork on a cargo feature.
- The link needs the NDK's libc++ and compiler-rt builtins explicitly (RandomX is
  C++ and its aarch64 JIT calls `__clear_cache`; `randomx-rs` links the NDK's stub
  `libstdc++.a`). `cargo ndk --link-builtins` covers the builtins only, so the
  whole set is spelled out as per-ABI `CARGO_TARGET_<TRIPLE>_RUSTFLAGS` — a global
  `RUSTFLAGS` would break the host build scripts. The linker is the NDK clang
  driver at the app's API level, named explicitly, because the dev machines pin
  the API 21 wrapper for `aarch64-linux-android` and API 21 fails this link.
- `ANDROID_NDK` is exported alongside `ANDROID_NDK_HOME`/`NDK_HOME`: without it
  `cmake-rs` aborts and `randomx-rs`'s build script panics.
- The task logs the feature it builds with, so CI carries the request, not just
  the task name.

Wallet screen
- Third chain source, **On-device node (Cuprate)**, beside the daemon field and
  Receive-only. Stored as `ChainChoice.InProcessCuprate` with an empty
  `monerod_datadir`, which the SDK resolves to `$ONIONWIRE_HOME/chain`. Existing
  Save daemon / Receive-only / Start / Stop behaviour is unchanged.
- The status line grows `· node <word>` from `WalletChildStatus.node_state` for
  this source only, fail-closed on an unrecognised word. No sync height: the SDK
  exposes none, so the screen does not show a number it cannot get.
- Storage honesty: free space on the chain store's filesystem, and Start is
  refused below a 3 GiB floor or when that filesystem cannot be read. The floor is
  a floor, not a sync estimate — nothing here exposes one.
- The pre-production warning (quoted user book, testnet only, Cuprate issue #278)
  is a sentence above Start, assertable in a test. Opt-in, never a default.
- The daemon field's hint no longer suggests a loopback daemon on the phone.

CI
- `android-ci.yml` extracts `lib/<abi>/libonionwire_sdk.so` from the built APK and
  fails the job unless it contains `target_max_memory`, a rodata literal compiled
  only under `cuprate-daemon`. v0.7.0's shipped `.so` has zero of them, so this
  separates "in the artifact" from "requested".

Docs
- `android/CUPRATE_DAEMON.md`: the build wiring, the link line, the CI gate, and
  an explicit "not verified on a device" section. `MONERO_BINARIES.md` points at
  it for the chain source that is not a binary.
2026-09-18 17:16:05 -04:00

178 lines
9 KiB
Markdown

# Bundled native executables (`jniLibs`)
The wallet screen starts an **app-owned `monero-wallet-rpc` child** (issue #107,
`src/wallet_child.rs`). Monero ships no Android binaries, so this repository
carries one, built here.
| | |
|---|---|
| File in the tree | `android/app/src/main/jniLibs/arm64-v8a/libmonerowalletrpc.so` |
| Bytes | 19 954 224 (stripped) |
| sha256 | `d9126788030ecd1ccdb40c7764e66e9a14d158a147ad335b256f0577681b5ad4` |
| Format | `ELF 64-bit LSB shared object, ARM aarch64, dynamically linked, interpreter /system/bin/linker64, for Android 21, built by NDK r17b` |
| Needed libs | `libc.so`, `libm.so`, `libdl.so` — everything else is static |
| Source | `https://github.com/monero-project/monero` |
| Tag | `v0.18.4.4` |
| Commit | `516e5355a103a2bf0b7e10328ebcaa2945019445` |
| Reproduce with | `scripts/build-monero-wallet-rpc-android.sh` |
| Verified by | `android/monero-binaries.sha256`, checked by `scripts/build-android-local.sh` on every build (local and CI) |
Only `arm64-v8a` is built. `-Ponionwire.abis=x86_64` produces an APK with no
wallet binary in it — the screen says so (`NO_WALLET_RPC_MESSAGE`) instead of
offering a Start that cannot work.
## How it is built
`scripts/build-monero-wallet-rpc-android.sh` runs upstream's own Android
cross-compile path — `contrib/depends` (boost, openssl, libunbound, libzmq,
libsodium, ncurses, readline, libiconv, expat for `aarch64-linux-android`) plus
the `toolchain.cmake` it emits — and then builds the single CMake target
`wallet_rpc_server`. It takes ~40 min on 12 cores and needs ~15 GB of scratch
space. It is **not** part of the CI path: CI here runs on a Raspberry Pi and the
Gradle build is already ~9 minutes. CI verifies the committed bytes instead.
Four things about the build are worth writing down, because each one is a
failure that looks like something else:
1. **CMake must be 3.x.** `contrib/depends` emits a *standalone-NDK-toolchain*
`toolchain.cmake` (`CMAKE_ANDROID_STANDALONE_TOOLCHAIN`). CMake 4.x no longer
honours that, silently falls back to the host `clang`, and the configure step
dies with `ld.lld: cannot open /usr/lib/clang/.../libclang_rt.builtins.a`.
The script installs 3.22.6 into a venv when the host CMake is 4.x.
2. **The NDK r17b prebuilt `clang60` needs `libtinfo.so.5`.** `android_ndk.mk`
pins NDK r17b and drives the long-removed `make_standalone_toolchain.py`
(r17b still has it). Its `clang60` binary is linked against ncurses 5;
current distros ship ncurses 6 only, so every compile fails with
`error while loading shared libraries: libtinfo.so.5`. A shim directory with
`libtinfo.so.5 -> libtinfo.so.6` on `LD_LIBRARY_PATH` fixes it. The script
creates it.
3. **`/usr` is in `CMAKE_FIND_ROOT_PATH`, and `/usr` is the host.** This is the
one that costs the most time. `CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY` does
not help: `/usr` is listed as a root, so `find_library()` returns host
libraries. Two consequences in this tree:
* `find_package(HIDAPI)` finds the **host** hidapi, defines `HAVE_HIDAPI`,
and `src/device` then fails to compile `hidapi/hidapi.h` — the include dir
it found is `/usr/include/hidapi`, so `<hidapi/hidapi.h>` resolves to
`/usr/include/hidapi/hidapi/hidapi.h`.
* Monero's optional zmq extras (`pgm`, `norm`, `gssapi_krb5`, `protolib`,
`bsd`, `md`, `protokit`) are probed with `find_library` and appended to
`ZMQ_LIB`, so the final link gets `/usr/lib/libpgm.so` — an x86-64 shared
object in an aarch64 link line: `error adding symbols: File in wrong
format`. `libzmq.a` does not actually reference openpgm.
The script rewrites the generated `toolchain.cmake` to
`SET(CMAKE_FIND_ROOT_PATH <depends-prefix>)`. This is also why upstream's
`make depends target=aarch64-linux-android` is not reproducible: it happens
to work only on a build host with none of those libraries installed.
A stale `CMakeCache.txt` keeps the host paths after the toolchain is fixed,
and nothing complains until the link, so the script also passes
`-U PGM_LIBRARY -U GSSAPI_LIBRARY …` and configures in a fresh build dir.
4. **The CMake target is `wallet_rpc_server`, not `monero-wallet-rpc`.** The
latter is the *output* name. `make monero-wallet-rpc` fails with
`No rule to make target`.
### Not bit-reproducible
Build paths and timestamps land in the binary, so a fresh run of the script
will not reproduce the sha256 above. That hash is a **provenance record of the
shipped blob** and the thing CI re-verifies — not a reproducible-build
assertion. If you rebuild, update both `android/monero-binaries.sha256` and this
table in the same commit, and say what changed in the tag or the toolchain.
## Why it is a `jniLibs` `.so` and not an asset
Since Android 10 (API 29) an app may not `exec()` a file in its own data
directory: the SELinux policy carries no `execute` on `app_data_file`, so the
familiar "ship it in `assets/`, copy it to `filesDir`, `chmod 700`" recipe is
refused by the kernel regardless of the mode bits. The one directory Android
hands out executable is `ApplicationInfo.nativeLibraryDir`, which the package
manager populates from the APK's `lib/<abi>/` entries at install time.
So the executable is named `libmonerowalletrpc.so` and shipped through
`jniLibs`, and the path the core is handed is
```
<applicationInfo.nativeLibraryDir>/libmonerowalletrpc.so
```
resolved at runtime by `wallet/WalletChildBinaries.kt`. This only works with
`android:extractNativeLibs="true"`; with the AGP 8 default
(`extractNativeLibs="false"`) the file stays inside the APK and there is nothing
on disk to run. `app/build.gradle.kts` sets `packaging.jniLibs.useLegacyPackaging
= true` for exactly that reason, and `keepDebugSymbols` for this one entry so
AGP's strip pass cannot rewrite the bytes that CI verifies.
Cost: the release APK grows from ~9 MB to ~28 MB (the binary is deflate-stored
at 6.9 MB) and installs ~20 MB into the app's native library directory.
## `arti` is NOT bundled
`WalletChild` runs `arti proxy -p <port>` as the SOCKS child for a **`.onion`**
daemon, and `ChildConfig::validate` requires a non-empty `arti_bin` for any
chain source that is not receive-only. There is no `arti` executable in this
build, and producing one (a standalone `arti` binary for
`aarch64-linux-android`, from a crate the Rust side does not currently build as
a bin) is a follow-up, not something this card could do without editing
`Cargo.toml`/`crates/**`.
What the app does about it, honestly:
* `arti_bin` is set to `<nativeLibraryDir>/libarti.so` — the path the binary
*would* occupy. For a loopback or clearnet daemon the core never spawns it, so
the value is inert. It is deliberately **not** set to a real binary:
`monero-wallet-rpc` named as the SOCKS child would be spawned as the proxy the
moment the user typed an `.onion` host.
* The wallet screen refuses an `.onion` daemon while `libarti.so` is absent, and
says why (`ONION_NEEDS_ARTI_NOTICE`) instead of letting the spawn fail.
So today: **loopback and clearnet daemons work; `.onion` daemons do not.**
## Verification in CI
`.forgejo/workflows/android-ci.yml` builds the APK on every `android/**` PR and
`scripts/build-android-local.sh` does the checking, so both CI jobs and local
builds get it:
1. before Gradle, `sha256sum -c android/monero-binaries.sha256` against the
committed blob;
2. after packaging, `unzip -p` the `lib/<abi>/libmonerowalletrpc.so` entry out
of the produced APK and compare its sha256 with the committed file. A
mismatch fails the build — the bytes that ship are the bytes that were
verified, not the bytes that were hoped for.
## Not verified
The binary was **not executed on a device or emulator**. There is no
`arm64-v8a` AVD on the build host (all three existing AVDs are `x86_64`), and no
aarch64 sysroot to run it under `qemu-user`. What is proven: it cross-compiles,
it is a valid `aarch64` Android PIE executable, it packages into the APK with
the expected bytes, and the Kotlin that drives it compiles. What is not:
that `monero-wallet-rpc` starts, opens its wallet file and answers JSON-RPC
under Android's runtime.
To close that gap:
```bash
sdkmanager "system-images;android-35;google_apis;arm64-v8a"
avdmanager create avd -n ow-arm64 -k "system-images;android-35;google_apis;arm64-v8a"
emulator -avd ow-arm64 # software-emulated on an x86_64 host: slow
adb install -r dist/onionwire-<version>-android-arm64-v8a.apk
```
then unlock the node, open Wallet, type a daemon as `host:port`, Save, and watch
the status line for `connecting → syncing → ready`. A `.onion` daemon will stop
at the arti notice above.
## The other chain source: an in-process daemon, not a binary
The third chain source — Cuprate's chain daemon running inside the OnionWire
process — is a **linked feature of the SDK's `.so`**, not a `jniLibs` executable,
so it has its own record: `android/CUPRATE_DAEMON.md` (feature wiring, the NDK C++
/ compiler-rt link line, the CI check that greps the shipped `.so` for a
cuprated-only string, and what has not been verified). `monero-wallet-rpc` above
stays a child process for it — Cuprate has no wallet layer.