Rust bindings to the Android NDK
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 |
ndk-examplesfor 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-gluewill do the necessary initialization when calling
mainbut requires a few adjustments:
Cargo.toml
toml [lib] crate-type = ["lib", "cdylib"]
Wraps
mainfunction using attribute macro
ndk::glue::main:
src/lib.rs```rust
pub fn main() { println!("hello world"); } ```
src/main.rs
rust fn main() { $crate::main(); }
Install
cargo apkfor building, running and debugging your application:
sh cargo install cargo-apk
We can now directly execute our
Hello Worldapplication on a real connected device or an emulator:
sh 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```rust
pub fn main() { log!("hello world"); } ```
The macro
ndk_glue::maintries to determine crate names from current Cargo.toml. In cases when it is not possible the default crate names will be used. You can override this names with specific paths like so: ```rust
ndkglue = "path::to::ndkglue", logger(androidlogger = "path::to::androidlogger", log = "path::to::log") )] fn main() {} ```
Java Native Interface (JNI) allows executing Java code in a VM from native applications.
ndk-examplescontains an
jni_audioexample which will print out all output audio devices in the log.
jni, JNI bindings for Rust
TODO shameless plug
TODO shameless plug