android: build the Rust library for the device's ABI, x86_64 included
Build Debug APK / build (push) Failing after 3m48s

The x86_64 emulator was running the arm64 .so through the native bridge,
which returns JNI jbooleans with junk in the register's upper bits. Since
jni-sys 0.4 reads jboolean as a Rust bool, every ExceptionCheck reported a
pending exception, so rustls-platform-verifier could neither initialize
nor verify a certificate on the emulator.

Still one Rust cross-compile per build: -PrustAbi wins, else the ABI
Android Studio injects for the device it deploys to, else arm64-v8a. The
.so is now synced into build/rustJniLibs rather than src/main/jniLibs, so
clean removes it and a previously built ABI can't ride along in the APK.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Greg ShuflinandClaude Opus 5.5 committed 2026-10-04 02:29:29 -07:00
1 parent 7e7f331fd8
commit 3a08adea65
5 files changed
+85 -29

No files matched your search

+5
View File
@@ -23,6 +23,11 @@ under `android/`. The root `CLAUDE.md` holds the project-wide rules.
`aarch64-linux-android` the first time is ~15 min. Once `rust/target` has
it, that step is a no-op and `unit-tests-kotlin`/`just lint-kotlin` take
seconds — see "Two Kotlin lint recipes" below.
- **Which ABI**: the Rust library is built for one ABI per build —
arm64-v8a by default, whatever Android Studio's Run targets, or
`-PrustAbi=x86_64` (e.g. `just gradlew installDebug -PrustAbi=x86_64`).
Installing on an x86_64 emulator needs the x86_64 build; the arm64 one
runs there through the native bridge and every HTTPS connection fails.
- **Neither**: Android/Kotlin changes can't be compiled or verified at all;
say so explicitly rather than guessing whether a change compiles.
- **Two Kotlin lint recipes, and the difference matters** (see ADR 0022):
+42 -16
View File
@@ -10,6 +10,33 @@ plugins {
alias(libs.plugins.ktfmt)
}
// The one ABI the Rust library is built for, since cross-compiling it is the
// expensive part of a build. An explicit `-PrustAbi=` wins; otherwise the ABI
// Android Studio injects when it deploys to a device (its first entry is the
// device's preferred ABI), so Run builds x86_64 for the emulator and arm64 for
// a phone. A command-line build with neither gets arm64, which is what ships.
//
// Never run the arm64 library on an x86_64 emulator: the native bridge breaks
// JNI `jboolean` returns — see `build-synchronicity-lib-android` in the
// justfile.
val supportedRustAbis = listOf("arm64-v8a", "x86_64")
val rustAbi: String =
providers.gradleProperty("rustAbi").orNull
?: providers.gradleProperty("android.injected.build.abi").orNull?.split(",")?.firstOrNull {
it in supportedRustAbis
}
?: "arm64-v8a"
require(rustAbi in supportedRustAbis) { "rustAbi=$rustAbi is not one of $supportedRustAbis" }
val rustTriple = mapOf("arm64-v8a" to "aarch64-linux-android", "x86_64" to "x86_64-linux-android")
val cargoTargetDir = System.getenv("CARGO_TARGET_DIR") ?: "${rootDir.parentFile}/rust/target"
val rustLibraryPath = "$cargoTargetDir/${rustTriple.getValue(rustAbi)}/release/libsynchronicity.so"
// buildRustLib syncs the .so here, under build/ so `clean` removes it and so
// an ABI built earlier can't linger in the APK beside the current one.
val rustJniLibsDir = layout.buildDirectory.dir("rustJniLibs")
android {
namespace = "com.gregshuflin.synchronicity"
compileSdk = 37
@@ -23,7 +50,7 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
ndk { abiFilters += listOf("arm64-v8a") }
ndk { abiFilters += listOf(rustAbi) }
}
buildTypes {
@@ -41,7 +68,7 @@ android {
}
buildFeatures { compose = true }
sourceSets { getByName("main") { jniLibs.directories += "src/main/jniLibs" } }
sourceSets { getByName("main") { jniLibs.directories += rustJniLibsDir.get().asFile.path } }
// Explicitly set the NDK version to one that's installed
ndkVersion = "28.0.13004108"
@@ -60,9 +87,8 @@ tasks.register<Exec>("buildRustLib") {
"Builds the Rust library via `just build-synchronicity-lib-android` and copies it to the jniLibs directory"
val repoRoot = project.rootDir.parentFile
val cargoTargetDir = System.getenv("CARGO_TARGET_DIR") ?: "$repoRoot/rust/target"
val builtLibrary = file("$cargoTargetDir/aarch64-linux-android/release/libsynchronicity.so")
val jniLibsDir = file("src/main/jniLibs/arm64-v8a")
val builtLibrary = file(rustLibraryPath)
val jniLibsDir = rustJniLibsDir.get().asFile
// Everything that decides what ends up in the .so, so Gradle can skip this
// task outright rather than shelling out to cargo on every single build —
@@ -81,15 +107,15 @@ tasks.register<Exec>("buildRustLib") {
// The recipe this shells out to: it sets RUSTFLAGS and the NDK linker, and
// pins ANDROID_NDK_VERSION, so a toolchain change lands here as an edit.
inputs.file("$repoRoot/justfile")
outputs.file(File(jniLibsDir, "libsynchronicity.so"))
inputs.property("abi", rustAbi)
outputs.dir(jniLibsDir)
workingDir = repoRoot
commandLine("just", "build-synchronicity-lib-android")
commandLine("just", "build-synchronicity-lib-android", rustAbi)
doLast {
jniLibsDir.mkdirs()
copy {
from(builtLibrary)
sync {
from(builtLibrary) { into(rustAbi) }
into(jniLibsDir)
}
}
@@ -162,6 +188,9 @@ abstract class GenerateUniffiBindings : DefaultTask() {
@get:PathSensitive(PathSensitivity.NONE)
abstract val rustLibrary: RegularFileProperty
// Which ABI's .so the recipe reads — and builds first, if it is stale.
@get:Input abstract val abi: Property<String>
// Repo root: `just` is invoked from there, and the recipe's paths are
// relative to it.
@get:Internal abstract val repoRoot: DirectoryProperty
@@ -178,7 +207,7 @@ abstract class GenerateUniffiBindings : DefaultTask() {
outDir.mkdirs()
execOperations.exec {
workingDir = repoRoot.get().asFile
commandLine("just", "uniffi-bindgen-generate", outDir.absolutePath)
commandLine("just", "uniffi-bindgen-generate", outDir.absolutePath, abi.get())
}
}
}
@@ -195,11 +224,8 @@ androidComponents {
description = "Regenerates Kotlin uniffi bindings from the compiled Rust library"
dependsOn("buildRustLib")
val cargoTargetDir =
System.getenv("CARGO_TARGET_DIR") ?: "${project.rootDir.parentFile}/rust/target"
rustLibrary.set(
file("$cargoTargetDir/aarch64-linux-android/release/libsynchronicity.so")
)
rustLibrary.set(file(rustLibraryPath))
abi.set(rustAbi)
repoRoot.set(file("${project.rootDir.parentFile}"))
}
// Wiring the task rather than a path carries the dependency with it, so
+12 -6
View File
@@ -7,17 +7,27 @@
Requires:
- JDK 17 (or Android Studio's bundled JBR at `/opt/android-studio/jbr`)
- Android SDK 35, NDK 28.0.13004108
- Rust stable with `aarch64-linux-android` target
- Rust stable with the `aarch64-linux-android` target, plus `x86_64-linux-android`
to run on an x86_64 emulator
Set `JAVA_HOME` and run:
```sh
cd android && ./gradlew assembleDebug
```
The Rust library is built for one ABI per build — cross-compiling it is the
slow part. Android Studio's Run builds whatever the target device uses
(x86_64 for an emulator on an x86 host, arm64-v8a for a phone); a
command-line build defaults to arm64-v8a, and `-PrustAbi=x86_64` overrides
it. Don't install an arm64 build on an x86_64 emulator: its native bridge
breaks JNI boolean returns, and TLS stops working (see the
`build-synchronicity-lib-android` recipe).
Or use the justfile recipes:
```sh
just android-build # build debug APK
just build-synchronicity-lib-android # build Rust library only
just build-synchronicity-lib-android # build Rust library only (arm64-v8a)
just build-synchronicity-lib-android x86_64 # ...for an x86_64 emulator
just fmt-kotlin # format Kotlin code
just fmt-kotlin-check # check Kotlin formatting
```
@@ -192,10 +202,6 @@ cleans it automatically.
- It removes `android/app/build` (1.8 GB) and `android/build`, but not
`android/.gradle`, the per-project incremental state.
- It does **not** remove `android/app/src/main/jniLibs/arm64-v8a/libsynchronicity.so`
(112 MB). `buildRustLib` copies the Rust `.so` into `src/`, not `build/`, so it
is outside everything `clean` considers — and it is gitignored, so it does not
show up in `git status` either. It survives every clean until deleted by hand.
- `~/.gradle` is shared and untouched: 4.2 GB of `caches`, plus a `wrapper`
directory holding every Gradle distribution ever used, not just the current one.
+1 -1
View File
@@ -49,7 +49,7 @@
androidSdk = androidComposition.androidsdk;
rustToolchain = pkgs.rust-bin.stable."${rust-version}".default.override {
targets = ["aarch64-linux-android"];
targets = ["aarch64-linux-android" "x86_64-linux-android"];
};
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
+25 -6
View File
@@ -63,11 +63,20 @@ doctor:
# layout, but are left alone if already set — `nix develop` (flake.nix's
# devShell) exports all three pointing at its own hermetic JDK/SDK/NDK, which
# `build-android-nix` relies on this recipe not clobbering.
[doc: "Build the synchronicity_lib Rust crate for Android (also invoked by Gradle's buildRustLib task)"]
#
# `abi` is an Android ABI name: `arm64-v8a` for phones, `x86_64` for the
# emulator on an x86 host. Don't run arm64 code on that emulator: it goes
# through the native bridge, which hands JNI `jboolean` returns back with junk
# in the register's upper bits, and jni-sys 0.4 reads `jboolean` as a Rust
# `bool` — every `ExceptionCheck` then reports a pending exception, and
# rustls-platform-verifier (so every HTTPS connection) fails. Each ABI has
# its own tree under rust/target, so switching between them stays incremental.
[doc: "Build the synchronicity_lib Rust crate for Android, for `abi` arm64-v8a (default) or x86_64 (also invoked by Gradle's buildRustLib task)"]
[group: "build"]
build-synchronicity-lib-android:
build-synchronicity-lib-android abi="arm64-v8a":
#!/usr/bin/env bash
set -euo pipefail
triple="$(just --quiet _android-triple {{abi}})"
export JAVA_HOME="${JAVA_HOME:-/opt/android-studio/jbr}"
export ANDROID_HOME="${ANDROID_HOME:-$HOME/Android/Sdk}"
export NDK_HOME="${NDK_HOME:-$ANDROID_HOME/ndk/{{ANDROID_NDK_VERSION}}}"
@@ -77,7 +86,7 @@ build-synchronicity-lib-android:
exit 1
fi
export PATH="$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH"
export TARGET_CC="$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android31-clang"
export TARGET_CC="$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/${triple}31-clang"
export TARGET_AR="$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar"
if [ ! -x "$TARGET_CC" ]; then
echo "Android NDK compiler not found at: $TARGET_CC" >&2
@@ -88,7 +97,16 @@ build-synchronicity-lib-android:
exit 1
fi
export RUSTFLAGS="-C linker=$TARGET_CC -C link-arg=-Wl,-z,max-page-size=16384"
cd rust && cargo build --target aarch64-linux-android --package synchronicity_lib --release
cd rust && cargo build --target "$triple" --package synchronicity_lib --release
# The Rust target triple for an Android ABI name.
_android-triple abi:
#!/usr/bin/env bash
case "{{abi}}" in
arm64-v8a) echo aarch64-linux-android ;;
x86_64) echo x86_64-linux-android ;;
*) echo "unsupported Android ABI: {{abi}} (expected arm64-v8a or x86_64)" >&2; exit 1 ;;
esac
# Generate Kotlin types using uniffi. Depends on build-synchronicity-lib-android
# so it never silently regenerates bindings from a stale .so.
@@ -101,9 +119,10 @@ build-synchronicity-lib-android:
# is deliberately under `build/` too, so `gradlew clean` clears it.
[doc: "Generate Kotlin types from the Rust FFI surface using uniffi"]
[group: "build"]
uniffi-bindgen-generate out_dir="android/app/build/generated/source/uniffi": build-synchronicity-lib-android
uniffi-bindgen-generate out_dir="android/app/build/generated/source/uniffi" abi="arm64-v8a": (build-synchronicity-lib-android abi)
#!/usr/bin/env bash
set -euo pipefail
triple="$(just --quiet _android-triple {{abi}})"
mkdir -p "{{out_dir}}"
# Absolute, because the cargo invocation below runs from rust/.
out_abs="$(cd "{{out_dir}}" && pwd)"
@@ -112,7 +131,7 @@ uniffi-bindgen-generate out_dir="android/app/build/generated/source/uniffi": bui
# leave uniffi's raw output as-is rather than running a second, conflicting
# formatter over it.
cd rust && cargo run --bin uniffi-bindgen -- generate \
--library "${CARGO_TARGET_DIR:-target}/aarch64-linux-android/release/libsynchronicity.so" \
--library "${CARGO_TARGET_DIR:-target}/$triple/release/libsynchronicity.so" \
--language kotlin \
--no-format \
--out-dir "$out_abs"