Crate android_activity
source ·Expand description
A glue layer for building standalone, Rust applications on Android
This crate provides a “glue” layer for building native Rust
applications on Android, supporting multiple Activity base classes.
It’s comparable to android_native_app_glue.c
for C/C++ applications.
Currently the crate supports two Activity base classes:
NativeActivity- Built in to Android, this doesn’t require compiling any Java or Kotlin code.GameActivity- From the Android Game Development Kit, it has more sophisticated input handling support thanNativeActivity.GameActivityis also based on theAndroidAppCompatclass which can help with supporting a wider range of devices.
Standalone applications based on this crate need to be built as cdylib libraries, like:
[lib]
crate_type=["cdylib"]and implement a #[no_mangle] android_main entry point like this:
#[no_mangle]
fn android_main(app: AndroidApp) {
}Once your application’s Activity class has loaded and it calls onCreate then
android-activity will spawn a dedicated thread to run your android_main function,
separate from the Java thread that created the corresponding Activity.
AndroidApp provides an interface to query state for the application as
well as monitor events, such as lifecycle and input events, that are
marshalled between the Java thread that owns the Activity and the native
thread that runs the android_main() code.
Main Thread Initialization
Before android_main() is called, the following application state
is also initialized:
- An I/O thread is spawned that will handle redirecting standard input
and output to the Android log, visible via
logcat. - A
JavaVMandActivityinstance will be associated with thendk_contextcrate so that other, independent, Rust crates are able to find a JavaVM for making JNI calls. - The
JavaVMwill be attached to the native thread - A Looper is attached to the Rust native thread.
These are undone after android_main() returns
Modules
Structs
ndk::configuration::ConfigurationEnums
AndroidApp::poll_eventsAndroidApp::poll_events