Skip to content

Repository files navigation

Cargo NDK Plugin

maven-central

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.

Compatibility

  • 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.

Examples

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.

Tasks

  • cargoNdk<Variant>Compile: compiles the configured Rust project and contributes the generated directory to the Android variant jniLibs sources
  • cargoNdkClean: optionally runs cargo clean in the configured Rust project when cleanTarget = true

For example, the debug variant creates cargoNdkDebugCompile, and the release variant creates cargoNdkReleaseCompile.

Build behavior

  • The plugin generates native libraries before Android merges variant jniLibs
  • By default it can bootstrap missing cargo-ndk and Rust Android targets
  • It validates Cargo.toml before invoking cargo ndk
  • It is intended to generate native packaging inputs, not to mutate the final APK/AAB after dexing or signing

DSL

  • source: Rust project directory, relative to the Android module. Default: src/main/rust
  • manifestPath: Cargo.toml path, relative to source. Default: Cargo.toml
  • output: generated jniLibs directory. Supports {variant} placeholder. Default: intermediates/cargoNdk/{variant}/jniLibs
  • platform: Android API level passed to cargo ndk --platform. Default: 21
  • abiFilters: target Android ABIs. Default: arm64-v8a, armeabi-v7a, x86_64, x86
  • args: extra arguments appended before build, such as --link-builtins; order is preserved
  • environment: extra environment variables merged into the cargo ndk process
  • ndkPath: explicit NDK path. Higher priority than ndkVersion
  • ndkVersion: resolves NDK from $ANDROID_HOME/ndk/<version> when ndkPath is not set
  • cleanTarget: when true, cargoNdkClean also runs cargo clean
  • release: force Cargo release profile for every variant
  • cargoCommand: command prefix used to invoke Cargo. Default: listOf("cargo")
  • rustupCommand: command prefix used to invoke Rustup. Default: listOf("rustup")
  • bootstrapToolchain: when true, prechecks and may install cargo-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.

Notes

  • ndkPath has higher priority than ndkVersion
  • ndkVersion resolves to $ANDROID_HOME/ndk/<version> when ndkPath is not set
  • release = true forces Cargo release builds for every Android variant
  • abiFilters controls Rust Android targets and the generated cargo ndk --target ... arguments

Local development

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>"
}

Reference

About

Cargo NDK Plugin.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages