A Gradle plugin for building Rust libraries with Cargo for Android projects.
New cargoClean feature allows you to run cargo clean on your Rust modules:
Manual clean (always available):
./gradlew cargoClean # Clean all Rust modules
./gradlew cargoCleanMyLib # Clean specific module "mylib"Auto-clean with Gradle clean:
Enable cargoClean to automatically run cargo clean when you run ./gradlew clean:
androidRust {
module("mylib") {
path = file("src/main/jni")
cargoClean = true // Enable auto-clean for this module
buildType("release") {
cargoClean = true // Or enable only for specific build types
}
}
// Or enable globally for all modules
cargoClean = true
}New Tasks:
cargoClean- Runs cargo clean for all Rust modulescargoClean<ModuleName>- Runs cargo clean for a specific module
The plugin now uses cargo-ndk instead of raw cargo commands for building Android libraries. This eliminates common linking errors and simplifies the build process.
Benefits:
- Automatic NDK environment configuration
- No manual environment variable setup needed
- Fewer linking errors and build failures
- Better compatibility with Android NDK
- Automatic rust home finding
- Fixed bugs
The plugin will automatically install cargo-ndk if it's not already available.
Complete support for Windows development environment:
- Automatic rustup installation on Windows
- Proper handling of Windows executable paths (.cmd wrappers)
- Windows-specific rustup installation via PowerShell
Proper Gradle task input/output annotations for intelligent caching:
@InputFiles- Tracks Rust source files (*.rs, Cargo.toml, Cargo.lock)@OutputDirectory- Tracks JNI libs output directory- Incremental builds - Only rebuilds when Rust sources change
- Better build performance in CI/CD environments
Multiple ABIs now build in parallel when using Gradle's --parallel flag:
- Significantly faster builds when targeting multiple architectures
- Optimal CPU utilization during compilation
- No sequential dependency chains between ABI builds
Detailed, actionable error messages when builds fail:
- Clear indication of what went wrong
- Specific suggestions for common issues
- Direct guidance on how to fix problems
- Better developer experience
Pre-build validation to catch configuration errors early:
- Validates Rust project paths exist
- Checks for Cargo.toml presence
- Verifies NDK installation
- Ensures module configurations are complete
androidRust {
module("mylib") {
path = file("../rust/mylib")
targets = listOf("arm", "arm64", "x86", "x86_64")
buildType("debug") {
profile = "dev"
runTests = true
}
buildType("release") {
profile = "release"
}
}
}androidRust {
minimumSupportedRustVersion = "1.70.0"
module("mylib") {
path = file("../rust/mylib")
targets = listOf("arm64")
runTests = true
disableAbiOptimization = false
buildType("debug") {
profile = "dev"
}
buildType("release") {
profile = "release"
}
}
}| Option | Description | Default |
|---|---|---|
minimumSupportedRustVersion |
Minimum Rust version required | "" (no check) |
path |
Path to Rust project directory | Required |
targets |
List of target ABIs | ["arm", "arm64", "x86", "x86_64"] |
profile |
Rust build profile | "release" |
runTests |
Run cargo test before building |
null (disabled) |
disableAbiOptimization |
Disable IDE ABI injection | null (false) |
cargoClean |
Run cargo clean with ./gradlew clean |
null (disabled) |
| Rust Name | Android Name | Architecture |
|---|---|---|
arm |
armeabi-v7a |
32-bit ARM |
arm64 |
arm64-v8a |
64-bit ARM |
x86 |
x86 |
32-bit x86 |
x86_64 |
x86_64 |
64-bit x86 |
- Android Gradle Plugin 7.0+
- Rust toolchain (will be auto-installed if missing)
- cargo-ndk (will be auto-installed if missing)
- Android NDK (install via Android Studio SDK Manager)
- Auto-Installation: Plugin automatically installs rustup, Rust toolchain, cargo-ndk, and required target triples
- Validation: Pre-build validation ensures all paths and configurations are correct
- Parallel Building: cargo-ndk builds each target ABI, potentially in parallel
- Output Placement: Compiled .so files are automatically placed in jniLibs directories
- Integration: Android build system picks up the libraries automatically
The puglin is avaiable here : https://plugins.gradle.org/plugin/io.github.rodroidmods.android-rust
Your Rust library must be configured as a C dynamic library:
[package]
name = "mylib"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
# your dependencies hereIf you have Rust installed in a custom location, create local.properties:
cargo.bin=/custom/path/to/cargo/binThe plugin will look for cargo, cargo-ndk, rustc, and rustup in this directory.
The plugin creates tasks for each build type and ABI combination:
clean<BuildType>RustJniLibs- Clean Rust build artifactstest<Module>Rust- Run Rust tests (if enabled)build<BuildType><Module>Rust[<ABI>]- Build specific ABIcargoClean- Run cargo clean for all Rust modulescargoClean<Module>- Run cargo clean for a specific module
Example tasks:
buildReleaseMyLibRust[arm64-v8a]buildDebugMyLibRust[x86_64]testMyLibRustcargoCleancargoCleanMyLib
The plugin fully supports Gradle's build cache. To enable:
./gradlew build --build-cacheOr add to gradle.properties:
org.gradle.caching=trueTo build multiple ABIs in parallel:
./gradlew build --parallelOr add to gradle.properties:
org.gradle.parallel=trueThe plugin will automatically install cargo-ndk, but if you see errors, manually install:
cargo install cargo-ndk
and if you have issues with rust beign not founded, than just ad cargo.bin path in local.prop and problem will be fixedInstall NDK via Android Studio: Tools β SDK Manager β SDK Tools β NDK (Side by side)
If you see "library not found" errors when running from Android Studio, set:
androidRust {
module("mylib") {
disableAbiOptimization = true
}
}This forces building all ABIs instead of just the IDE-injected target.
Ensure PowerShell execution policy allows running scripts:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserThe plugin now uses cargo-ndk internally. No configuration changes are required, but you may need to install cargo-ndk:
cargo install cargo-ndkAll existing configurations will continue to work.
It is recommended to use the latest version 0.8.0, as previous versions have bugs that have been fixed.
- Rodroid Mods
- Matrix dev
MIT License
Contributions welcome! Please open an issue or pull request on GitHub.