This is a Gradle plugin that simplifies building Rust projects for Android using the Android NDK.
It automates the process of setting up the NDK toolchain, configuring the build environment, and compiling the Rust code for various Android architectures.
- Tested with AGP
9.2.1 - Tested with Kotlin
2.4.0 - Tested with Gradle
9.6.1 - Requires JDK
11+
The plugin is designed for Android application modules that package generated jniLibs into the final APK/AAB.
plugins {
...
id("io.github.thatworld.cargondk") version "<version>"
}
cargoNdk {
source = "src/main/rust"
manifestPath = "Cargo.toml"
output = "intermediates/cargoNdk/{variant}/jniLibs"
platform = 21
abiFilters = setOf("arm64-v8a", "x86_64")
args = listOf("--config", "target-feature=+crt-static", "--link-builtins")
environment["RUSTFLAGS"] += listOf("--cfg", "android_build")
cargoCommand = listOf("cargo")
rustupCommand = listOf("rustup")
bootstrapToolchain = true
}Please refer to the app module for the default project structure.
cargoNdk<Variant>Compile: compiles the configured Rust project and contributes the generated directory to the Android variantjniLibssourcescargoNdkClean: optionally runscargo cleanin the configured Rust project whencleanTarget = true
For example, the debug variant creates cargoNdkDebugCompile, and the release variant creates cargoNdkReleaseCompile.
- The plugin generates native libraries before Android merges variant
jniLibs - By default it can bootstrap missing
cargo-ndkand Rust Android targets - It validates
Cargo.tomlbefore invokingcargo ndk - It is intended to generate native packaging inputs, not to mutate the final APK/AAB after dexing or signing
source: Rust project directory, relative to the Android module. Default:src/main/rustmanifestPath:Cargo.tomlpath, relative tosource. Default:Cargo.tomloutput: generatedjniLibsdirectory. Supports{variant}placeholder. Default:intermediates/cargoNdk/{variant}/jniLibsplatform: Android API level passed tocargo ndk --platform. Default:21abiFilters: target Android ABIs. Default:arm64-v8a,armeabi-v7a,x86_64,x86args: extra arguments appended beforebuild, such as--link-builtins; order is preservedenvironment: extra environment variables merged into thecargo ndkprocessndkPath: explicit NDK path. Higher priority thanndkVersionndkVersion: resolves NDK from$ANDROID_HOME/ndk/<version>whenndkPathis not setcleanTarget: whentrue,cargoNdkCleanalso runscargo cleanrelease: force Cargo release profile for every variantcargoCommand: command prefix used to invoke Cargo. Default:listOf("cargo")rustupCommand: command prefix used to invoke Rustup. Default:listOf("rustup")bootstrapToolchain: whentrue, prechecks and may installcargo-ndk/ missing Rust Android targets before build. Default:true
cargoCommand and rustupCommand are mainly useful when you need to route execution through a wrapper script, a pinned toolchain launcher, or a CI-specific bootstrap command. Set bootstrapToolchain = false when you want the plugin to skip preflight bootstrap and invoke cargo ndk directly.
ndkPathhas higher priority thanndkVersionndkVersionresolves to$ANDROID_HOME/ndk/<version>whenndkPathis not setrelease = trueforces Cargo release builds for every Android variantabiFilterscontrols Rust Android targets and the generatedcargo ndk --target ...arguments
This repository uses includeBuild("cargo-ndk-plugin") in the root settings.gradle.kts, so the sample app module resolves the plugin from the local included build instead of downloading it from Maven.
For published consumption, use the plugin id and version in a normal Android project:
plugins {
id("com.android.application")
id("io.github.thatworld.cargondk") version "<version>"
}