Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Simplify Ensu Android build flow

Manav Rathif5c161bae87b
diff --git a/.github/workflows/ensu-release.yml b/.github/workflows/ensu-release.yml
index 3e0ddda315..3f2dd93025 100644
--- a/.github/workflows/ensu-release.yml
+++ b/.github/workflows/ensu-release.yml
@@ -259,19 +259,11 @@ jobs:
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
- with:
- targets: aarch64-linux-android,armv7-linux-androideabi,x86_64-linux-android,i686-linux-android
-
- - name: Install cargo-ndk
- run: cargo install cargo-ndk
- - name: Build Rust libraries
+ - name: Generate Android bindings
run: |
- cd ../../packages/rust/tool
- ./build_android.sh
- env:
- ANDROID_HOME: ${{ env.ANDROID_HOME }}
- NDK_VERSION: ${{ env.NDK_VERSION }}
+ cd ../../../../../rust
+ cargo codegen ensu-android
- name: Print Android release capabilities
run: |
diff --git a/.gitignore b/.gitignore
index f06b5fbee0..7ef1073faa 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,18 +18,4 @@ rust/cli/exports/
node_modules/
# Build artifacts
-mobile/native/android/packages/rust/build/
target/
-
-# Generated native bindings / binaries (regenerate locally)
-mobile/native/android/apps/ensu/crypto-auth-core/src/main/jniLibs/
-mobile/native/android/apps/ensu/crypto-auth-core/src/main/java/io/ente/ensu/crypto/core_uniffi.kt
-mobile/native/android/apps/ensu/crypto-auth-core/src/main/java/io/ente/ensu/crypto/core.kt
-mobile/native/android/packages/rust/.gradle/
-mobile/native/android/packages/rust/src/main/jniLibs/
-mobile/native/android/packages/rust/src/main/kotlin/io/ente/labs/inference_rs/inference_rs_uniffi.kt
-mobile/native/android/packages/rust/src/main/kotlin/io/ente/labs/inference_rs/inference.kt
-mobile/native/android/packages/rust/src/main/kotlin/io/ente/labs/llmchat_db/llmchat_db_uniffi.kt
-mobile/native/android/packages/rust/src/main/kotlin/io/ente/labs/ensu_db/db.kt
-mobile/native/android/packages/rust/src/main/kotlin/io/ente/labs/llmchat_sync/llmchat_sync_uniffi.kt
-mobile/native/android/packages/rust/src/main/kotlin/io/ente/labs/ensu_sync/sync.kt
diff --git a/mobile/native/android/apps/ensu/.gitignore b/mobile/native/android/apps/ensu/.gitignore
index f4b4254afd..cba25191a7 100644
--- a/mobile/native/android/apps/ensu/.gitignore
+++ b/mobile/native/android/apps/ensu/.gitignore
@@ -11,3 +11,5 @@ local.properties
# OS
.DS_Store
+
+crypto-auth-core/src/main/java/io/ente/ensu/crypto/core.kt
diff --git a/mobile/native/android/apps/ensu/AGENTS.md b/mobile/native/android/apps/ensu/AGENTS.md
index 88986ad800..47a907df8f 100644
--- a/mobile/native/android/apps/ensu/AGENTS.md
+++ b/mobile/native/android/apps/ensu/AGENTS.md
@@ -10,7 +10,10 @@
## Build, Test, and Development Commands
Run commands from `android/apps/ensu`.
-- `./gradlew assembleDebug`: Build a debug APK.
+- Re-run `cd ../../../../../rust && cargo codegen ensu-android` whenever the UniFFI interfaces under `rust/uniffi` change.
+- `./gradlew :app-ui:assembleDebug`: Build a debug APK.
+- `./gradlew :app-ui:assembleRelease`: Build a release APK.
+- `./gradlew :app-ui:bundleRelease`: Build a release AAB.
- `./gradlew test`: Run unit tests for modules that include tests.
- `./gradlew lint`: Run Android lint checks (if configured).
diff --git a/mobile/native/android/apps/ensu/README.md b/mobile/native/android/apps/ensu/README.md
index 51ea757957..c18e56f7c9 100644
--- a/mobile/native/android/apps/ensu/README.md
+++ b/mobile/native/android/apps/ensu/README.md
@@ -6,48 +6,25 @@
- JDK 17
- Android SDK with API level 35 (install in a default location or set `ANDROID_HOME`/`ANDROID_SDK_ROOT`)
- Android NDK (install via `sdkmanager "ndk;<version>"`; optionally set `NDK_VERSION` to select a specific version)
-- Rust toolchain and `cargo-ndk` (`cargo install cargo-ndk`)
-
-## Quick scripts
-
-From `mobile/native/android/apps/ensu`:
-
-```bash
-./build.sh # builds Rust + debug APK
-./build.sh apk # builds Rust + release APK
-./build.sh aab # builds Rust + release AAB (app bundle)
-./run.sh # builds + installs + launches on connected adb device
-```
-
-Helpful flags:
-
-- `--skip-rust` to skip Rust/jni rebuild
-- `--endpoint <url>` to set `ENTE_API_ENDPOINT`
-- `--device <serial>` for `run.sh`
-- legacy aliases still supported: `release`/`release-apk` and `bundle`/`release-aab`
-
-Run `./build.sh --help` or `./run.sh --help` for full options.
+- Rust toolchain
## Building
-### 1. Build Rust Libraries
+### 1. Generate Kotlin bindings
-Before building the Android app, compile the Rust native libraries. Ensure the SDK is installed in a default location or set `ANDROID_HOME`/`ANDROID_SDK_ROOT` to the SDK containing an NDK. Optionally set `NDK_VERSION` to select a specific version.
+Before building the Android app, generate the UniFFI Kotlin bindings:
```bash
-cd mobile/native/android/packages/rust/tool
-./build_android.sh
+cd rust
+cargo codegen ensu-android
```
-This script produces the JNI `.so` artifacts in `android/packages/rust/src/main/jniLibs/` (including `libcore.so`, `libinference.so`, `libdb.so`, and `libsync.so`).
-It also generates the UniFFI Kotlin bindings into:
+This refreshes the generated Kotlin sources in:
- `android/apps/ensu/crypto-auth-core/src/main/java/io/ente/ensu/crypto/core.kt`
- `android/packages/rust/src/main/kotlin/io/ente/labs/ensu_db/db.kt`
- `android/packages/rust/src/main/kotlin/io/ente/labs/ensu_sync/sync.kt`
- `android/packages/rust/src/main/kotlin/io/ente/labs/inference_rs/inference.kt`
-These generated files are gitignored and are refreshed each time `build_android.sh` runs.
-
### 2. Build Debug APK
```bash
@@ -55,6 +32,8 @@ cd mobile/native/android/apps/ensu
./gradlew :app-ui:assembleDebug
```
+Gradle builds the Rust JNI libraries automatically for the relevant ABI.
+
Output: `app-ui/build/outputs/apk/debug/app-ui-debug.apk`
### 3. Build Release APK
@@ -63,8 +42,18 @@ Output: `app-ui/build/outputs/apk/debug/app-ui-debug.apk`
./gradlew :app-ui:assembleRelease
```
+Release builds include the full shipped ABI set.
+
Output: `app-ui/build/outputs/apk/release/app-ui-release.apk`
+### 4. Build Release AAB
+
+```bash
+./gradlew :app-ui:bundleRelease
+```
+
+Output: `app-ui/build/outputs/bundle/release/app-ui-release.aab`
+
Note: Release builds use a debug keystore located at `debug.keystore`. For production releases, configure your own signing keys in `app-ui/build.gradle.kts`.
## Installation
@@ -81,12 +70,20 @@ Or via adb:
adb install -r app-ui/build/outputs/apk/debug/app-ui-debug.apk
```
+Launch the app:
+
+```bash
+adb shell am start -n io.ente.ensu/.MainActivity
+```
+
### Install Release Build
```bash
adb install -r app-ui/build/outputs/apk/release/app-ui-release.apk
```
+To target a specific device, prefix the `adb` commands with `-s <serial>`.
+
Note: If upgrading from a differently signed build, uninstall first:
```bash
diff --git a/mobile/native/android/apps/ensu/build.sh b/mobile/native/android/apps/ensu/build.sh
deleted file mode 100755
index 36d60cfd15..0000000000
--- a/mobile/native/android/apps/ensu/build.sh
+++ /dev/null
@@ -1,109 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-ROOT="$(cd "$(dirname "$0")" && pwd)"
-RUST_BUILD_SCRIPT="$ROOT/../../packages/rust/tool/build_android.sh"
-
-MODE="debug"
-SKIP_RUST=0
-SKIP_APP=0
-ENDPOINT=""
-
-usage() {
- cat <<'EOF'
-Build Ensu Android app.
-
-Usage:
- ./build.sh [debug|apk|aab] [options]
-
-Modes:
- debug Build debug APK
- apk Build release APK
- aab Build release Android App Bundle (.aab)
-
-Aliases (still supported):
- release, release-apk => apk
- bundle, release-aab => aab
-
-Options:
- --skip-rust Skip Rust/jni build step
- --skip-app Skip Gradle app build step
- --endpoint <url> Set ENTE_API_ENDPOINT for this build
- -h, --help Show help
-
-Examples:
- ./build.sh
- ./build.sh apk
- ./build.sh aab
- ./build.sh debug --endpoint https://api.example.com
-EOF
-}
-
-while [[ $# -gt 0 ]]; do
- case "$1" in
- debug|apk|aab|release|release-apk|bundle|release-aab)
- MODE="$1"
- shift
- ;;
- --skip-rust)
- SKIP_RUST=1
- shift
- ;;
- --skip-app)
- SKIP_APP=1
- shift
- ;;
- --endpoint)
- ENDPOINT="${2:-}"
- if [[ -z "$ENDPOINT" ]]; then
- echo "Missing value for --endpoint" >&2
- exit 1
- fi
- shift 2
- ;;
- -h|--help)
- usage
- exit 0
- ;;
- *)
- echo "Unknown argument: $1" >&2
- usage
- exit 1
- ;;
- esac
-done
-
-if [[ -n "$ENDPOINT" ]]; then
- export ENTE_API_ENDPOINT="$ENDPOINT"
-fi
-
-if [[ "$SKIP_RUST" -eq 0 ]]; then
- echo "==> Building Rust Android artifacts"
- "$RUST_BUILD_SCRIPT"
-fi
-
-if [[ "$SKIP_APP" -eq 0 ]]; then
- cd "$ROOT"
-
- case "$MODE" in
- apk|release|release-apk)
- TASK=":app-ui:assembleRelease"
- OUTPUT_LABEL="APK"
- OUTPUT_PATH="app-ui/build/outputs/apk/release/app-ui-release.apk"
- ;;
- aab|bundle|release-aab)
- TASK=":app-ui:bundleRelease"
- OUTPUT_LABEL="AAB"
- OUTPUT_PATH="app-ui/build/outputs/bundle/release/app-ui-release.aab"
- ;;
- *)
- TASK=":app-ui:assembleDebug"
- OUTPUT_LABEL="APK"
- OUTPUT_PATH="app-ui/build/outputs/apk/debug/app-ui-debug.apk"
- ;;
- esac
-
- echo "==> Running Gradle task $TASK"
- ./gradlew "$TASK"
- echo "βœ… $OUTPUT_LABEL: $ROOT/$OUTPUT_PATH"
-fi
diff --git a/mobile/native/android/apps/ensu/run.sh b/mobile/native/android/apps/ensu/run.sh
deleted file mode 100755
index bedfc67774..0000000000
--- a/mobile/native/android/apps/ensu/run.sh
+++ /dev/null
@@ -1,112 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-ROOT="$(cd "$(dirname "$0")" && pwd)"
-MODE="debug"
-SERIAL=""
-SKIP_BUILD=0
-SKIP_RUST=0
-ENDPOINT=""
-
-usage() {
- cat <<'EOF'
-Build + install + launch Ensu Android app.
-
-Usage:
- ./run.sh [debug|release] [options]
-
-Options:
- --device <serial> Target adb device serial
- --skip-build Skip build step
- --skip-rust Pass through to build step
- --endpoint <url> Set ENTE_API_ENDPOINT for this run/build
- -h, --help Show help
-
-Examples:
- ./run.sh
- ./run.sh --device emulator-5554
- ./run.sh release --skip-rust
-EOF
-}
-
-while [[ $# -gt 0 ]]; do
- case "$1" in
- debug|release)
- MODE="$1"
- shift
- ;;
- --device)
- SERIAL="${2:-}"
- if [[ -z "$SERIAL" ]]; then
- echo "Missing value for --device" >&2
- exit 1
- fi
- shift 2
- ;;
- --skip-build)
- SKIP_BUILD=1
- shift
- ;;
- --skip-rust)
- SKIP_RUST=1
- shift
- ;;
- --endpoint)
- ENDPOINT="${2:-}"
- if [[ -z "$ENDPOINT" ]]; then
- echo "Missing value for --endpoint" >&2
- exit 1
- fi
- shift 2
- ;;
- -h|--help)
- usage
- exit 0
- ;;
- *)
- echo "Unknown argument: $1" >&2
- usage
- exit 1
- ;;
- esac
-done
-
-if ! command -v adb >/dev/null 2>&1; then
- echo "adb not found in PATH" >&2
- exit 1
-fi
-
-ADB_CMD=(adb)
-if [[ -n "$SERIAL" ]]; then
- ADB_CMD+=( -s "$SERIAL" )
-fi
-
-if [[ "$SKIP_BUILD" -eq 0 ]]; then
- BUILD_ARGS=("$MODE")
- if [[ "$SKIP_RUST" -eq 1 ]]; then
- BUILD_ARGS+=("--skip-rust")
- fi
- if [[ -n "$ENDPOINT" ]]; then
- BUILD_ARGS+=("--endpoint" "$ENDPOINT")
- fi
- "$ROOT/build.sh" "${BUILD_ARGS[@]}"
-fi
-
-if [[ "$MODE" == "release" ]]; then
- APK_PATH="$ROOT/app-ui/build/outputs/apk/release/app-ui-release.apk"
-else
- APK_PATH="$ROOT/app-ui/build/outputs/apk/debug/app-ui-debug.apk"
-fi
-
-if [[ ! -f "$APK_PATH" ]]; then
- echo "APK not found: $APK_PATH" >&2
- exit 1
-fi
-
-echo "==> Installing $APK_PATH"
-"${ADB_CMD[@]}" install -r "$APK_PATH"
-
-echo "==> Launching io.ente.ensu"
-"${ADB_CMD[@]}" shell monkey -p io.ente.ensu -c android.intent.category.LAUNCHER 1 >/dev/null
-
-echo "βœ… Ensu launched on Android"
diff --git a/mobile/native/android/packages/rust/.gitignore b/mobile/native/android/packages/rust/.gitignore
new file mode 100644
index 0000000000..d749b2b556
--- /dev/null
+++ b/mobile/native/android/packages/rust/.gitignore
@@ -0,0 +1,5 @@
+.gradle/
+build/
+src/main/kotlin/io/ente/labs/inference_rs/inference.kt
+src/main/kotlin/io/ente/labs/ensu_db/db.kt
+src/main/kotlin/io/ente/labs/ensu_sync/sync.kt
diff --git a/mobile/native/android/packages/rust/build.gradle.kts b/mobile/native/android/packages/rust/build.gradle.kts
index 45c7c6ca0b..da9ad23744 100644
--- a/mobile/native/android/packages/rust/build.gradle.kts
+++ b/mobile/native/android/packages/rust/build.gradle.kts
@@ -1,8 +1,95 @@
+import java.io.ByteArrayOutputStream
+
plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
}
+val generatedJniLibsDir = layout.buildDirectory.dir("generated/jniLibs")
+
+fun captureOrNull(vararg command: String): String? {
+ val stdout = ByteArrayOutputStream()
+ return try {
+ exec {
+ commandLine(*command)
+ standardOutput = stdout
+ errorOutput = ByteArrayOutputStream()
+ }
+ stdout.toString().trim()
+ } catch (_: Exception) {
+ null
+ }
+}
+
+fun adbCommand(): List<String> {
+ val sdkRoot = System.getenv("ANDROID_HOME") ?: System.getenv("ANDROID_SDK_ROOT")
+ val sdkAdb = sdkRoot?.let { file("$it/platform-tools/adb") }
+ return when {
+ sdkAdb?.canExecute() == true -> listOf(sdkAdb.absolutePath)
+ else -> listOf("adb")
+ }
+}
+
+fun connectedDeviceAbiOrNull(): String? {
+ val adb = adbCommand()
+ val serial = System.getenv("ANDROID_SERIAL")?.trim().orEmpty().ifBlank {
+ val devices = captureOrNull(*(adb + listOf("devices")).toTypedArray())
+ ?.lineSequence()
+ ?.drop(1)
+ ?.map(String::trim)
+ ?.filter { it.endsWith("\tdevice") }
+ ?.map { it.substringBefore('\t') }
+ ?.toList()
+ .orEmpty()
+ if (devices.size == 1) devices[0] else ""
+ }
+
+ if (serial.isBlank()) return null
+
+ return captureOrNull(*(adb + listOf("-s", serial, "shell", "getprop", "ro.product.cpu.abi")).toTypedArray())
+ ?.trim()
+ ?.takeIf { it in setOf("arm64-v8a", "armeabi-v7a", "x86_64") }
+}
+
+fun defaultLocalAbi(): String =
+ when (System.getProperty("os.arch")) {
+ "aarch64", "arm64" -> "arm64-v8a"
+ "x86_64", "amd64" -> "x86_64"
+ else -> error("Unsupported host architecture: ${System.getProperty("os.arch")}")
+ }
+
+fun requestedRustAbis(): List<String> {
+ val taskNames = gradle.startParameter.taskNames
+ val isReleaseBuild = taskNames.any {
+ it.contains("Release") || it.contains("bundle", ignoreCase = true)
+ }
+
+ return if (isReleaseBuild) {
+ listOf("arm64-v8a", "armeabi-v7a", "x86_64")
+ } else {
+ listOf(connectedDeviceAbiOrNull() ?: defaultLocalAbi())
+ }
+}
+
+val buildRustJni by tasks.registering {
+ outputs.dir(generatedJniLibsDir)
+ outputs.upToDateWhen { false }
+
+ doLast {
+ val abis = requestedRustAbis()
+ exec {
+ workingDir = file("tool")
+ commandLine(
+ "bash",
+ "./build_android.sh",
+ "--out-dir",
+ generatedJniLibsDir.get().asFile.absolutePath,
+ *abis.toTypedArray(),
+ )
+ }
+ }
+}
+
android {
namespace = "io.ente.ensu.rust"
compileSdk = 34
@@ -11,7 +98,7 @@ android {
minSdk = 23
}
- sourceSets["main"].jniLibs.srcDir("src/main/jniLibs")
+ sourceSets["main"].jniLibs.setSrcDirs(listOf("build/generated/jniLibs"))
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
@@ -23,6 +110,10 @@ android {
}
}
+tasks.matching { it.name.startsWith("pre") && it.name.endsWith("Build") }.configureEach {
+ dependsOn(buildRustJni)
+}
+
dependencies {
api("net.java.dev.jna:jna:5.14.0@aar")
api("androidx.annotation:annotation:1.7.1")
diff --git a/mobile/native/android/packages/rust/tool/build_android.sh b/mobile/native/android/packages/rust/tool/build_android.sh
index 8bb0698c0b..0d74d3037e 100755
--- a/mobile/native/android/packages/rust/tool/build_android.sh
+++ b/mobile/native/android/packages/rust/tool/build_android.sh
@@ -7,52 +7,28 @@ CORE_UNIFFI_RUST_DIR="$REPO_ROOT/rust/uniffi/core"
INFERENCE_UNIFFI_RUST_DIR="$REPO_ROOT/rust/uniffi/ensu/inference"
CHATDB_UNIFFI_RUST_DIR="$REPO_ROOT/rust/uniffi/ensu/db"
CHAT_SYNC_UNIFFI_RUST_DIR="$REPO_ROOT/rust/uniffi/ensu/sync"
-OUT_DIR="$ROOT/src/main/jniLibs"
-CORE_KOTLIN_OUT_DIR="$REPO_ROOT/mobile/native/android/apps/ensu/crypto-auth-core/src/main/java"
-RUST_KOTLIN_OUT_DIR="$ROOT/src/main/kotlin"
+OUT_DIR="$ROOT/build/generated/jniLibs"
SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}"
NDK_VERSION="${NDK_VERSION:-}"
NDK_ROOT_PATH=""
-
-if [[ "$(uname -s)" == "Darwin" ]]; then
- HOST_LIB_EXT="dylib"
-elif [[ "$(uname -s)" == "Linux" ]]; then
- HOST_LIB_EXT="so"
-else
- HOST_LIB_EXT="dll"
-fi
-
-ensure_uniffi_bindgen() {
- if command -v uniffi-bindgen >/dev/null 2>&1; then
- local version
- version="$(uniffi-bindgen --version 2>/dev/null || true)"
- if [[ "$version" == *"0.31."* ]]; then
- return
- fi
- echo "Found incompatible $version, installing uniffi-bindgen 0.31.x"
- fi
-
- cargo install --locked --version 0.31.0 uniffi --features cli --bin uniffi-bindgen
-}
-
-generate_kotlin_bindings() {
- local crate_dir="$1"
- local lib_name="$2"
- local out_dir="$3"
-
- pushd "$crate_dir" >/dev/null
- cargo build --release
-
- local lib_path="$crate_dir/target/release/lib${lib_name}.${HOST_LIB_EXT}"
- if [[ ! -f "$lib_path" ]]; then
- echo "Expected $lib_path to exist" >&2
- exit 1
- fi
-
- mkdir -p "$out_dir"
- uniffi-bindgen generate "$lib_path" --language kotlin --out-dir "$out_dir"
- popd >/dev/null
-}
+TOOLCHAIN_BIN=""
+HOST_TAG=""
+ABIS=()
+
+export PATH="${CARGO_HOME:-$HOME/.cargo}/bin:/opt/homebrew/bin:/usr/local/bin:$PATH"
+
+while [[ $# -gt 0 ]]; do
+ case "$1" in
+ --out-dir)
+ OUT_DIR="$2"
+ shift 2
+ ;;
+ *)
+ ABIS+=("$1")
+ shift
+ ;;
+ esac
+done
if [[ -z "$SDK_ROOT" ]]; then
for candidate in "$HOME/Library/Android/sdk" "$HOME/Android/Sdk" "$HOME/Android/sdk"; do
@@ -85,35 +61,127 @@ if [[ -z "$NDK_ROOT_PATH" || ! -d "$NDK_ROOT_PATH" ]]; then
exit 1
fi
+case "$(uname -s):$(uname -m)" in
+ Darwin:arm64)
+ HOST_TAG_CANDIDATES=(darwin-arm64 darwin-x86_64)
+ ;;
+ Darwin:x86_64)
+ HOST_TAG_CANDIDATES=(darwin-x86_64)
+ ;;
+ Linux:aarch64 | Linux:arm64)
+ HOST_TAG_CANDIDATES=(linux-aarch64)
+ ;;
+ Linux:x86_64)
+ HOST_TAG_CANDIDATES=(linux-x86_64)
+ ;;
+ *)
+ echo "Unsupported host for Android NDK builds: $(uname -s):$(uname -m)" >&2
+ exit 1
+ ;;
+esac
+
+for host_tag in "${HOST_TAG_CANDIDATES[@]}"; do
+ candidate="$NDK_ROOT_PATH/toolchains/llvm/prebuilt/$host_tag/bin"
+ if [[ -d "$candidate" ]]; then
+ HOST_TAG="$host_tag"
+ TOOLCHAIN_BIN="$candidate"
+ break
+ fi
+done
+
+if [[ -z "$TOOLCHAIN_BIN" ]]; then
+ echo "Android NDK LLVM toolchain not found under $NDK_ROOT_PATH/toolchains/llvm/prebuilt" >&2
+ exit 1
+fi
+
export ANDROID_NDK="$NDK_ROOT_PATH"
export ANDROID_NDK_ROOT="$NDK_ROOT_PATH"
export NDK_ROOT="$NDK_ROOT_PATH"
-export CARGO_CFG_TARGET_FEATURE="${CARGO_CFG_TARGET_FEATURE:-}"
+export PATH="$TOOLCHAIN_BIN:$PATH"
mkdir -p "$OUT_DIR"
-for CRATE_DIR in "$CORE_UNIFFI_RUST_DIR" "$INFERENCE_UNIFFI_RUST_DIR" "$CHATDB_UNIFFI_RUST_DIR" "$CHAT_SYNC_UNIFFI_RUST_DIR"; do
- pushd "$CRATE_DIR" >/dev/null
- cargo ndk \
- --platform 23 \
- --link-libcxx-shared \
- -t arm64-v8a \
- -t armeabi-v7a \
- -t x86_64 \
- -o "$OUT_DIR" \
- build --release
- popd >/dev/null
-done
+if [[ ${#ABIS[@]} -eq 0 ]]; then
+ echo "Usage: build_android.sh [--out-dir DIR] <abi> [<abi>...]" >&2
+ exit 1
+fi
+
+rust_target_for_abi() {
+ case "$1" in
+ arm64-v8a) echo aarch64-linux-android ;;
+ armeabi-v7a) echo armv7-linux-androideabi ;;
+ x86_64) echo x86_64-linux-android ;;
+ *) return 1 ;;
+ esac
+}
+
+toolchain_for_abi() {
+ case "$1" in
+ arm64-v8a) echo aarch64-linux-android23 ;;
+ armeabi-v7a) echo armv7a-linux-androideabi23 ;;
+ x86_64) echo x86_64-linux-android23 ;;
+ *) return 1 ;;
+ esac
+}
+
+libcxx_dir_for_abi() {
+ case "$1" in
+ arm64-v8a) echo aarch64-linux-android ;;
+ armeabi-v7a) echo arm-linux-androideabi ;;
+ x86_64) echo x86_64-linux-android ;;
+ *) return 1 ;;
+ esac
+}
-ensure_uniffi_bindgen
+build_abi() {
+ local abi="$1"
+ local target clang_triple libcxx_dir linker clangxx suffix
-# Remove stale generated bindings to avoid duplicate symbol conflicts.
-rm -f "$CORE_KOTLIN_OUT_DIR/io/ente/ensu/crypto/core.kt"
-rm -f "$RUST_KOTLIN_OUT_DIR/io/ente/labs/inference_rs/inference.kt"
-rm -f "$RUST_KOTLIN_OUT_DIR/io/ente/labs/ensu_db/db.kt"
-rm -f "$RUST_KOTLIN_OUT_DIR/io/ente/labs/ensu_sync/sync.kt"
+ target=$(rust_target_for_abi "$abi") || {
+ echo "Unsupported Android ABI: $abi" >&2
+ exit 1
+ }
+ clang_triple=$(toolchain_for_abi "$abi")
+ libcxx_dir=$(libcxx_dir_for_abi "$abi")
+ linker="$TOOLCHAIN_BIN/$clang_triple-clang"
+ clangxx="$TOOLCHAIN_BIN/$clang_triple-clang++"
+ suffix=$(echo "$target" | tr '[:lower:]-' '[:upper:]_')
+
+ [[ -x "$linker" ]] || { echo "Expected linker $linker" >&2; exit 1; }
+ [[ -x "$clangxx" ]] || { echo "Expected linker $clangxx" >&2; exit 1; }
+
+ rustup target list --installed | grep -qx "$target" \
+ || rustup target add "$target"
+
+ export "CARGO_TARGET_${suffix}_LINKER=$linker"
+ export "CC_${target//-/_}=$linker"
+ export "CXX_${target//-/_}=$clangxx"
+ export "AR_${target//-/_}=$TOOLCHAIN_BIN/llvm-ar"
+ export "RANLIB_${target//-/_}=$TOOLCHAIN_BIN/llvm-ranlib"
+
+ for CRATE_DIR in "$CORE_UNIFFI_RUST_DIR" "$INFERENCE_UNIFFI_RUST_DIR" "$CHATDB_UNIFFI_RUST_DIR" "$CHAT_SYNC_UNIFFI_RUST_DIR"; do
+ pushd "$CRATE_DIR" >/dev/null
+ cargo build --release --target "$target"
+
+ lib_name=$(basename "$CRATE_DIR")
+ built_lib="$CRATE_DIR/target/$target/release/lib${lib_name}.so"
+ [[ -f "$built_lib" ]] || { echo "Expected $built_lib" >&2; exit 1; }
+
+ mkdir -p "$OUT_DIR/$abi"
+ cp "$built_lib" "$OUT_DIR/$abi/"
+ popd >/dev/null
+ done
-generate_kotlin_bindings "$CORE_UNIFFI_RUST_DIR" "core" "$CORE_KOTLIN_OUT_DIR"
-generate_kotlin_bindings "$INFERENCE_UNIFFI_RUST_DIR" "inference" "$RUST_KOTLIN_OUT_DIR"
-generate_kotlin_bindings "$CHATDB_UNIFFI_RUST_DIR" "db" "$RUST_KOTLIN_OUT_DIR"
-generate_kotlin_bindings "$CHAT_SYNC_UNIFFI_RUST_DIR" "sync" "$RUST_KOTLIN_OUT_DIR"
+ libcxx_shared="$NDK_ROOT_PATH/toolchains/llvm/prebuilt/$HOST_TAG/sysroot/usr/lib/$libcxx_dir/libc++_shared.so"
+ [[ -f "$libcxx_shared" ]] || { echo "Expected $libcxx_shared" >&2; exit 1; }
+ cp "$libcxx_shared" "$OUT_DIR/$abi/"
+}
+
+rm -rf \
+ "$OUT_DIR/arm64-v8a" \
+ "$OUT_DIR/armeabi-v7a" \
+ "$OUT_DIR/x86_64"
+
+for abi in "${ABIS[@]}"; do
+ build_abi "$abi"
+done