Exploring a Gradle Plugin for Automatic Rust ↔ Kotlin Bindings on Android
Hi everyone,
I’m an Android developer currently learning Rust and experimenting with Rust integration in Android applications.
While working with Rust and Android, I noticed that even simple Rust functions require a significant amount of repetitive integration work:
-
Writing JNI bridge functions
-
Creating Kotlin
external fundeclarations -
Loading native libraries
-
Building Rust for Android targets
-
Managing and copying generated
.sofiles -
Keeping Rust and Kotlin bindings synchronized
For example, a developer might write:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
but still needs to manually create JNI wrappers and Kotlin bridge code before these functions can be used from Android.
Motivation
My long-term goal is to make Rust feel like a first-class citizen in Android projects, similar to how Kotlin developers can consume generated APIs without worrying about the underlying implementation details.
Rather than requiring developers to learn JNI, manage native builds, and maintain bindings manually, I would like to explore whether these steps can be automated through tooling.
Proposed Goal
I am considering a Gradle-based solution (and potentially an Android Studio plugin in the future) that would allow developers to place Rust source files directly inside an Android project.
Example project structure:
app/
rust/
├── math.rs
└── greetings.rs
Developer-written Rust:
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
Automatically generated Kotlin API:
Rust.add(1, 2)
Rust.greet("World")
without manually writing:
-
JNI functions
-
Kotlin bridge classes
-
Cargo build commands
-
Native library copy logic
Proposed Architecture
My current idea is:
-
Scan Rust source files inside a dedicated
rust/directory. -
Parse Rust ASTs using the
syncrate. -
Discover public Rust functions.
-
Generate JNI bridge code automatically.
-
Generate Kotlin wrappers automatically.
-
Build Rust libraries for Android targets.
-
Copy generated native libraries into the Android build.
-
Integrate everything into the Gradle build lifecycle.
Current Status
So far, I have successfully integrated Rust into Android manually using JNI and generated Android-compatible .so libraries.
I have not yet built the generator or plugin itself.
Before investing significant time into a proof of concept, I would like feedback on whether this architecture is reasonable and whether there are existing tools, APIs, or approaches I should investigate first.
Questions
-
Does this architecture seem reasonable from a Gradle/Android tooling perspective?
-
Would a Gradle plugin be a better starting point than an Android Studio plugin?
-
Are there existing AGP or Kotlin APIs that would simplify source generation and build integration?
-
Are there existing projects that already solve this problem in a similar way?
-
How does this compare with UniFFI or other Rust binding-generation approaches?
-
Are there any major technical challenges, limitations, or pitfalls that I may be overlooking?
I would greatly appreciate feedback, architectural suggestions, references to existing tooling, or advice from anyone who has worked on Gradle plugins, Android build tooling, code generation, or Rust integration projects.
Thank you!