Need help with android-ndk-rs?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

rust-windowing
181 Stars 25 Forks Other 122 Commits 17 Opened issues

Description

Rust bindings to the Android NDK

Services available

!
?

Need anything else?

Contributors list

Rust on Android

Rust MIT license APACHE2 license

Libraries and tools for Rust programming on Android targets:

Name

Description Badges

ndk-sys
| Raw FFI bindings to the NDK | crates.io crates.io
ndk
| Safe abstraction of the bindings | crates.io crates.io
ndk-glue
| Startup code | crates.io crates.io
ndk-build
| Everything for building apk's | crates.io crates.io
cargo-apk
| Build tool | crates.io crates.io

See

ndk-examples
for examples using the NDK and the README files of the crates for more details.

Hello world

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
toml
[lib]
crate-type = ["lib", "cdylib"]

Wraps

main
function using attribute macro
ndk::glue::main
:

src/lib.rs
```rust

[cfgattr(targetos = "android", ndk_glue::main(backtrace = "on"))]

pub fn main() { println!("hello world"); } ```

src/main.rs
rust
fn main() {
    $crate::main();
}

Install

cargo apk
for building, running and debugging your application:
sh
cargo install cargo-apk

We can now directly execute our

Hello World
application on a real connected device or an emulator:
sh
cargo apk run

Logging and stdout

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

Android logger can be setup using feature "logger" and attribute macro like so:

src/lib.rs
```rust

[cfgattr(targetos = "android", ndk_glue::main(logger(level = "debug", tag = "my-tag")))]

pub fn main() { log!("hello world"); } ```

Overriding crate paths

The macro

ndk_glue::main
tries 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

[ndk_glue::main(

ndkglue = "path::to::ndkglue", logger(androidlogger = "path::to::androidlogger", log = "path::to::log") )] fn main() {} ```

JNI

Java Native Interface (JNI) allows executing Java code in a VM from native applications.

ndk-examples
contains an
jni_audio
example which will print out all output audio devices in the log.
  • jni
    , JNI bindings for Rust

Winit and glutin

TODO shameless plug

Flutter

TODO shameless plug

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.