#jni #flutter #loaded #jvm #loader #class #applications

irondash_jni_context

Access to JavaVM for Flutter JNI libraries

4 releases

0.2.0 Jul 1, 2023
0.1.2 Mar 29, 2023
0.1.1 Nov 15, 2022
0.1.0 Nov 10, 2022

#8 in #loaded

Download history 1/week @ 2024-02-20 5/week @ 2024-02-27 5/week @ 2024-03-26 20/week @ 2024-04-02

363 downloads per month

MIT license

12KB
270 lines

irondash_jni_context

This crate can be used to get access to JavaVM from Rust code.

It defines JNI_OnLoad function which is called by JVM when the library is loaded. It saves the pointer to JavaVM, which can be accessed later.

It also attempts to store the class loader that loaded Flutter application.

This only works if the dylib that uses this crate is loaded from Java using System.loadLibrary. It will not work if the dylib is loaded from other code (i.e. through dlopen).

This crate also assumes that System.loadLibrary is called from main thread. It will remember main thread looper and provides functionality to schedule callbacks to be run on main thread.

Example usage

    let context = JniContext::get().unwrap();
    let java_vm = context.java_vm();
    let mut env = java_vm.attach_to_current_thread();

    // ...

    context.schedule_on_main_thread(|| {
        // This will be run on main thread
        println!("Hello from main thread!");
    });

Dependencies

~0–11MB
~64K SLoC