#jvm #jni #bindgen #android

jni-android-sys

Autogenerated glue code for access Android JVM APIs from Rust

5 releases

✓ Uses Rust 2018 edition

0.0.10 Jan 14, 2020
0.0.9 Aug 28, 2019

#29 in Operating systems

Download history 1/week @ 2019-10-07 15/week @ 2019-10-14 34/week @ 2019-10-21 5/week @ 2019-10-28 7/week @ 2019-11-04 6/week @ 2019-11-11 11/week @ 2019-11-18 6/week @ 2019-11-25 76/week @ 2019-12-02 74/week @ 2019-12-09 1/week @ 2019-12-16 8/week @ 2019-12-23 1/week @ 2019-12-30 5/week @ 2020-01-06 142/week @ 2020-01-13

106 downloads per month

MIT/Apache

218MB
1.5M SLoC

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

..\example_android_studio\ What
app\src\main\java\MainActivity.java Java Source
rust\src\lib.rs Rust Source
app\build.gradle App Build Config
build.gradle Root Build Config

Example: Inline

Cargo.toml

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

MainActivity.java

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);
}

main_activity.rs

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
}

License

Licensed under either of

at your option.

Contribution

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

feature description
"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.)

Dependencies