Libraries and tools for Rust programming on Android targets:
Name | Description | Badges |
---|---|---|
ndk-sys |
Raw FFI bindings to the NDK | |
ndk |
Safe abstraction of the bindings | |
ndk-glue |
Startup code | |
ndk-build |
Everything for building apk's | |
cargo-apk |
Build tool |
See ndk-examples
for examples using the NDK and the README files of the crates for more details.
Quick start for setting up a new project with support for Android. For communication with the Android framework in our native Rust application we require a NativeActivity
. ndk-glue
will do the necessary initialization when calling main
but requires a few adjustments:
Cargo.toml
[lib]
crate-type = ["lib", "cdylib"]
Wraps main
function using attribute macro ndk::glue::main
:
src/lib.rs
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace))]
pub fn main() {
println!("hello world");
}
src/main.rs
fn main() {
$crate::main();
}
Install cargo apk
for building, running and debugging your application:
cargo install cargo-apk
We can now directly execute our Hello World
application on a real connected device or an emulator:
cargo apk run
Stdout is redirected to the android log api when using ndk-glue
. Any logger that logs to
stdout, like println!
, should therefore work.
Use can filter the output in logcat
adb logcat RustStdoutStderr:D *:S
Android logger can be setup using feature "logger" and attribute macro like so:
src/lib.rs
#[cfg_attr(target_os = "android", ndk_glue::main(logger(debug, "my-tag")))]
pub fn main() {
log!("hello world");
}
TODO: talk more about jni and add some examples
jni
, JNI bindings for Rust
TODO shameless plug
TODO shameless plug