Crate jvm_getter

Crate jvm_getter 

Source
Expand description

⚠️ This library depends on implementation details of Android, not its public APIs. Use at your own risk.

A tiny no_std library for finding JNI_GetCreatedJavaVMs() on Android 24 to 30.

JNI_GetCreatedJavaVMs() is a JNI function that returns the list of Java VM instances that have been created during runtime. Unfortunately, on Android API level 30 or lower, JNI_GetCreatedJavaVMs() is not one of the public APIs. Therefore, the recommended way by the official Android documentation is to use JNI_OnLoad(), which has a JavaVM parameter.

This is painful for cross-platform library developers, especially when the OS feature they want to use is coupled with Java on Android, as they have to provide a way to pass JNIEnv to the library. By using JNI_GetCreatedJavaVMs(), you can retrieve the JavaVM instance, and you can even create JNIEnv instances for threads created on the Rust side.

With jvm-getter, libraries can provide cross-platform interfaces without demaning the consumers to manually handle Java-specific logic for Android. To learn about the strategy to find JNI_GetCreatedJavaVMs() used by jvm-getter, please refer to the documentation of find_jni_get_created_java_vms(). For compatibility, find_jni_get_created_java_vms() is also available on Desktop platforms, including Windows, macOS, and Linux.

Functions§

find_jni_get_created_java_vms
Finds the current process’s JNI_GetCreatedJavaVMs. For compatibility, this function is also available on Desktop platforms other than Android, including Windows, macOS, and Linux.

Type Aliases§

JNI_GetCreatedJavaVMs
The function pointer type of JNI_GetCreatedJavaVMs().