Rust + NDK on Android, Part 14 of 15. This series teaches Android developers how to ship Rust in production apps, no prior Rust required. Hub: Rust on Android: How to Use It, How to Debug It, and Why It's Worth It. Previous: Binary Size and the 16KB Page Requirement: Optimizing Rust .so Files for Release. Next: Rust in Kotlin Multiplatform: One Native Core for Android and iOS, and When to Skip Rust Entirely.
A CI pipeline for a Rust + Android repo has five jobs: pin the compiler and the NDK so every machine links the same bits, install the Android targets, cache the cargo registry and the target directory with keys that actually hit, run the Rust tests on the host and the instrumented tests on an emulator, and assemble the release APK. This post builds that pipeline as one GitHub Actions workflow file, the one the companion repo will run on every push when it ships.
It assumes the toolchain from Part 1 and the Gradle wiring from Part 4, because Gradle already drives cargo-ndk through an Exec task, the CI build entry point is ./gradlew :app:assembleRelease. CI adds pins, caches, and a device; the build system stays the one you already have. The release profile from Part 13 rides along for free: it lives in rust/Cargo.toml, so the size-optimized, 16KB-aligned libraries you tuned there are what CI assembles.
Key Takeaways
- The hosted Ubuntu runner ships rustup and an Android SDK, but versions drift as the image updates. Reproducibility comes from two committed pins:
rust-toolchain.tomland one NDK version string.- Cache two stores separately: the cargo registry keyed on
Cargo.lock, andrust/targetkeyed on the toolchain file plus the lock file. A repository gets 10 GB of cache before eviction (GitHub Docs), so keys that never hit are worse than no cache.cargo teston the host needs no NDK and no emulator, so it is the fastest failure signal you have. Only the JNI border needs the emulator, hardware-accelerated through KVM on free Linux runners.- The emulator job reuses Part 4’s
-PrustAbisproperty to compile a single ABI, because an x86_64 emulator has no use for three ARM libraries.
Pin the toolchain before touching CI
The runner image ships a Rust toolchain and an Android SDK. Trust neither: image updates move the preinstalled versions without anything in your history changing. Both pins live in your repository instead.
The Rust pin is rust-toolchain.toml at the repo root, which rustup discovers from any subdirectory (rustup book, Overrides):
[toolchain]
channel = "1.90.0"
components = ["clippy", "rustfmt"]
targets = [
"aarch64-linux-android",
"armv7-linux-androideabi",
"x86_64-linux-android",
"i686-linux-android",
]
profile = "minimal"
This file does three jobs. It pins the compiler for CI and for every laptop that clones the repo. It declares the Android targets, so Part 1’s rustup target add becomes self-serve: the first cargo command installs the pinned toolchain, targets included. And it stabilizes your caches, because cargo’s fingerprints embed the compiler version, so an unpinned compiler silently invalidates every cached artifact.
The NDK pin is the same string Part 1 exported as ANDROID_NDK_HOME, kept in one env entry at the top of the workflow. Each job installs that exact version through sdkmanager (Android Developers, sdkmanager) and points ANDROID_NDK_HOME at it, the only discovery mechanism cargo-ndk needs (cargo-ndk). AGP never touches the NDK here: Part 4’s Exec task compiles, AGP packages.
The GitHub Actions CI workflow for Rust on Android
Three jobs run in parallel: rust-checks proves the pure Rust core on the host, build assembles the release APK, and instrumented exercises the JNI border on an emulator. That is the hub’s debugging principle as a job graph: logic bugs fail on the host in minutes, before an NDK download even starts. Here is .github/workflows/android.yml, starting with the header and the host-side job:
name: android
on:
push:
branches: [main]
pull_request:
env:
NDK_VERSION: "28.2.13676358"
CARGO_TERM_COLOR: always
jobs:
rust-checks:
runs-on: ubuntu-latest
defaults:
run:
working-directory: rust
steps:
- uses: actions/checkout@v4
- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
key: cargo-registry-${{ hashFiles('rust/Cargo.lock') }}
restore-keys: cargo-registry-
- name: Cache target directory
uses: actions/cache@v4
with:
path: rust/target
key: rust-host-${{ hashFiles('rust-toolchain.toml') }}-${{ hashFiles('rust/Cargo.lock') }}
restore-keys: rust-host-${{ hashFiles('rust-toolchain.toml') }}-
- name: Check formatting
run: cargo fmt --check
- name: Lint
run: cargo clippy --workspace --all-targets -- -D warnings
- name: Test on the host
run: cargo test --workspace
There is no Rust install step, on purpose: rustup sees rust-toolchain.toml and the first cargo invocation installs 1.90.0 before running. The tests compile for the host’s own Linux target, which is the point: the cores are pure Rust libraries, so their tests need nothing Android-shaped.
The build job continues in the same file:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install the pinned NDK
run: |
echo "y" | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "ndk;$NDK_VERSION"
echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/$NDK_VERSION" >> "$GITHUB_ENV"
- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: "17"
- name: Set up Gradle
uses: gradle/actions/setup-gradle@v4
- name: Cache cargo registry
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
key: cargo-registry-${{ hashFiles('rust/Cargo.lock') }}
restore-keys: cargo-registry-
- name: Cache target directory
uses: actions/cache@v4
with:
path: rust/target
key: rust-android-${{ hashFiles('rust-toolchain.toml') }}-${{ hashFiles('rust/Cargo.lock') }}
restore-keys: rust-android-${{ hashFiles('rust-toolchain.toml') }}-
- name: Cache cargo-ndk
id: cargo-ndk
uses: actions/cache@v4
with:
path: ~/.cargo/bin/cargo-ndk
key: cargo-ndk-${{ hashFiles('rust-toolchain.toml') }}
- name: Install cargo-ndk
if: steps.cargo-ndk.outputs.cache-hit != 'true'
run: cargo install cargo-ndk --locked
- name: Assemble release
run: ./gradlew :app:assembleRelease
- name: Upload APK
uses: actions/upload-artifact@v4
with:
name: app-release
path: app/build/outputs/apk/release/
./gradlew :app:assembleRelease. Gradle runs cargoBuildRelease from Part 4, which runs cargo ndk for all four ABIs and drops the libraries into app/build/rustJniLibs/release. Local and CI builds are the same code path, so a CI failure reproduces on your laptop with one command.
The setup-gradle step brings its own caching for the Gradle side, so the workflow only hand-manages cargo’s caches. And cargo install cargo-ndk sits behind a small binary cache, because compiling it from source every run is a silly tax; the key ties it to the toolchain file so a compiler bump rebuilds it.
Cross-compilation caching that actually hits
Cargo wants two different things cached, and mixing them up is how Rust CI caching usually goes wrong.
The first store is the registry: the crates.io index, downloaded .crate archives, and git dependencies. It is identical across host and Android builds and changes only with Cargo.lock, so the key hashes the lock file, and the restore-keys prefix lets a lockfile change restore the previous cache and download only the delta. Deliberately absent: ~/.cargo/registry/src/, which cargo re-extracts faster than the cache restores it, and ~/.cargo/bin/, which would clobber the runner’s own cargo.
The second store is rust/target, the compiled artifacts. Its key has three parts. The lock file, because dependency artifacts are what make the cache worth having. The toolchain file, because fingerprints embed the compiler version, so artifacts from an old rustc restore, never match, and still count against your budget. And the job’s own prefix, rust-host- against rust-android-, because Linux host artifacts are ballast for a job compiling Android targets.
10 GB per repository by default, entries unused for 7 days are removed, and past the limit GitHub evicts by last access date, oldest first (GitHub Docs, caching dependencies). Four ABIs in release mode fill that, which is why keys that restore useless artifacts are worse than no cache: they evict the caches that were working.
If you would rather not hand-maintain the keys, the community action rust-cache automates the same scheme, compiler version and lock file hashed into the keys, target directory pruned before saving. I show the manual version because the day the cache misbehaves, you debug it by understanding this key structure.
A matrix over ABIs, and when it pays
The build job compiles all four ABIs sequentially inside one Gradle invocation. The alternative is a matrix, one runner per ABI, in the same workflow file. Add the registry and cargo-ndk cache steps you have already seen; only the per-target pieces are new:
rust-targets:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- abi: arm64-v8a
triple: aarch64-linux-android
- abi: armeabi-v7a
triple: armv7-linux-androideabi
- abi: x86_64
triple: x86_64-linux-android
- abi: x86
triple: i686-linux-android
steps:
- uses: actions/checkout@v4
- name: Install the pinned NDK
run: |
echo "y" | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "ndk;$NDK_VERSION"
echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/$NDK_VERSION" >> "$GITHUB_ENV"
- name: Cache target directory
uses: actions/cache@v4
with:
path: rust/target
key: rust-${{ matrix.triple }}-${{ hashFiles('rust-toolchain.toml') }}-${{ hashFiles('rust/Cargo.lock') }}
restore-keys: rust-${{ matrix.triple }}-${{ hashFiles('rust-toolchain.toml') }}-
- name: Install cargo-ndk
run: cargo install cargo-ndk --locked
- name: Build ${{ matrix.abi }}
working-directory: rust
run: cargo ndk -t ${{ matrix.abi }} build --release --locked
Be honest about the arithmetic. Four jobs each compile the entire dependency graph for their target, so total runner minutes go up even when wall clock comes down, and four per-target caches eat the 10 GB budget four times faster. For this workspace, the single Gradle job is the right default.
The matrix earns its keep twice: when the workspace grows enough that one job crowds the runner’s time or memory, and for its failure signal. armeabi-v7a is a 32-bit target, so code that quietly assumes usize is eight bytes, the trap flagged in Part 6, fails in a job named after the guilty ABI instead of deep in a twenty-minute log.
Instrumented tests on an emulator, with KVM
Free Linux runners run the Android emulator hardware-accelerated, since GitHub enabled KVM on them in 2023 (GitHub changelog, hardware accelerated Android virtualization, 2023). The device node needs its permissions opened first. Here is the last job, minus the registry, target and cargo-ndk cache steps you have seen twice already:
instrumented:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Enable KVM
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm
- name: Install the pinned NDK
run: |
echo "y" | "$ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager" "ndk;$NDK_VERSION"
echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/$NDK_VERSION" >> "$GITHUB_ENV"
- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: "17"
- name: Set up Gradle
uses: gradle/actions/setup-gradle@v4
- name: Install cargo-ndk
run: cargo install cargo-ndk --locked
- name: Run instrumented tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 34
arch: x86_64
emulator-options: -no-window -no-audio -no-boot-anim
disable-animations: true
script: ./gradlew :app:connectedDebugAndroidTest -PrustAbis=x86_64
Two details carry this job. The udev rule makes /dev/kvm writable by the runner user; without it the emulator falls back to software emulation and your timeout budget evaporates. And -PrustAbis=x86_64 is Part 4’s iteration property doing CI duty: the emulator is an x86_64 device, so three ARM libraries would be pure waste.
Keep the suite small on purpose. Everything the host can prove belongs in rust-checks. What belongs here is the border: the library loads, the JNI names resolve instead of throwing UnsatisfiedLinkError, the bug class Part 2 dissected, and a Kotlin string survives the round trip.
Reproducible builds: what you can pin, and what you cannot
Everything above pins the toolchain. Reproducibility also needs pinned inputs, which starts with committing rust/Cargo.lock. The Cargo book says to commit the lock file so every build uses identical dependency versions (Cargo book, Cargo.toml vs Cargo.lock), and for an artifact you ship that is not negotiable.
The lock file only protects you if nothing rewrites it silently, which is what --locked enforces: cargo fails the build if Cargo.lock disagrees with the manifests instead of updating it. The matrix job already passes it. The Gradle-driven build gets it through one line in Part 4’s task registration in app/build.gradle.kts:
val cargoBuildRelease = registerCargoBuild("release", listOf("--release", "--locked"))
The workflow file is an input too. Tags like actions/cache@v4 are mutable; GitHub’s hardening guide notes that pinning an action to a full commit SHA is the only way to use it as an immutable release (GitHub Docs, security hardening). For a personal repo the major tags are a reasonable trade; when secrets or signing keys enter the workflow, switch to SHAs.
Be precise about what this buys. Same sources, same rustc, same NDK, same flags. That is the reproducibility most teams need, and the payoff this series cares about, because symbolicating a production tombstone with ndk-stack means rebuilding the exact .so that shipped. Byte-for-byte output is a deeper project, because debug info and panic messages embed absolute build paths that differ per machine; rustc’s --remap-path-prefix flag flattens them, and it is where you start if bit-exact becomes a requirement.
Measuring what the cache buys you
The methodology is built in. Every run’s summary reports wall time per job, and each cache step logs which key it restored, exact match or restore-keys fallback. So: delete the caches from the Actions caches view, push a commit, record the cold times; push a trivial change, record the warm times; change one line of Rust, record the incremental times.
The cold run pays for everything: registry download, full dependency graph per target, cargo-ndk compile. The warm run with an unchanged lock file pays only for your workspace crates, and a Kotlin-only change should cost cargo nothing, because Part 4’s input declarations let Gradle skip the Exec task entirely. The honest numbers, cold against warm per job, land when the companion repo ships, and this section is where that chart will slot in.
Frequently Asked Questions
Can you run Android instrumented tests on free GitHub runners?
Yes. Linux runners have KVM, so the x86_64 emulator runs hardware-accelerated once a udev rule opens /dev/kvm. Boot it with the android-emulator-runner action and build only the x86_64 ABI for that job. ARM system images remain the gap; those need a physical device farm.
Where this leaves you
Every command in it appeared earlier in the series; CI just runs them on a machine nobody has touched, with every version written down. When it breaks, it breaks for a readable reason: a cache key stopped matching, or an image update moved a version you never pinned.
One part remains. The series closes with Part 15, Rust in Kotlin Multiplatform: One Native Core for Android and iOS, and When to Skip Rust Entirely, where the same crate compiles for iOS too. The full series map lives at the hub.
Sources
- GitHub Docs, “Caching dependencies to speed up workflows”, retrieved 2026-10-25, https://docs.github.com/en/actions/using-workflows/caching-dependencies-to-speed-up-workflows
- GitHub Docs, “Security hardening for GitHub Actions”, retrieved 2026-10-25, https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions
- The rustup book, “Overrides”, retrieved 2026-10-25, https://rust-lang.github.io/rustup/overrides.html
- Cargo book, “Cargo.toml vs Cargo.lock”, retrieved 2026-10-25, https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
- Android Developers, “sdkmanager”, retrieved 2026-10-25, https://developer.android.com/tools/sdkmanager
- crates.io, cargo-ndk, retrieved 2026-10-25, https://crates.io/crates/cargo-ndk
