[][src]Crate jni_android_sys


Work in progress, only barely kinda partially usable, APIs not yet stabilized

Uses jni-bindgen to export Android's Java APIs to Rust. Only tested against Android API level 28 so far.

Example: example_android_studio Android Studio Example

app\src\main\java\MainActivity.javaJava Source
rust\src\lib.rsRust Source
app\build.gradleApp Build Config
build.gradleRoot Build Config

Example: Inline


jni-android-sys = { version = "0.0.10", features = ["api-level-28", "android-view-KeyEvent"] }


package com.example;

import androidx.appcompat.app.AppCompatActivity;
import android.view.KeyEvent;

public class MainActivity extends AppCompatActivity {
    static { System.loadLibrary("example"); }
    @Override public native boolean dispatchKeyEvent(KeyEvent keyEvent);


use jni_sys::{jboolean, jobject, JNI_TRUE};
use jni_glue::{Argument, Env};
use jni_android_sys::android::view::KeyEvent;

#[no_mangle] pub extern "system" fn Java_com_example_MainActivity_dispatchKeyEvent(
    env:        &Env,
    _this:      jobject,
    key_event:  Argument<KeyEvent>,
) -> jboolean {
    let key_event = unsafe { key_event.with_unchecked(env) }; // Unsafe boilerplate not yet autogenerated.

    // Err = Java exception was thrown.
    // Ok(None) = Java object is null.
    // Ok(Some(...)) = Real java object!
    if let Some(key_event) = key_event {
        let is_enter = if let Ok(r) = key_event.getKeyCode() { r == KeyEvent::KEYCODE_ENTER } else { false };
        let is_down  = if let Ok(r) = key_event.getAction()  { r == KeyEvent::ACTION_DOWN   } else { false };
        if is_enter && is_down {
            println!("ENTER pressed"); // Not that you can see this...

    JNI_TRUE // JNI boilerplate not yet autogenerated


Licensed under either of

  • Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Build Features

"api-level-7"Define android APIs as they were defined in API level 7 or greater
"api-level-28"Define android APIs as they were defined in API level 28 or greater
"android-view-KeyEvent"Define the android.view.KeyEvent class
"android-view-KeyEvent_Callback"Define the android.view.KeyEvent.Callback interface
...thousands of other features...Define other android.*, androidx.*, dalvik.*, java.*, javax.*, and org.* APIs.
"all"Define all the available android/java APIs
"force-define"Define android APIs on non-android targets (for use in custom targets, docs, etc.)
"force-define-x86_64-unknown-linux-gnu"Define android APIs on x86_64-unknown-linux-gnu specifically (for use in docs.rs)
"nightly"Define some stuff which may only work on nightly compilers (right now just for docs.)