Expand description
Minimal helper for jni-rs
, supporting dynamic proxies, Android dex
embedding and broadcast receiver. Used for calling Java code from Rust.
jni
is re-exported here for the user to import jni
functions, avoiding
version inconsistency between jni
and this crate.
This crate uses ndk_context::AndroidContext
on Android, usually initialized
by android_activity
. Examples for Android are provided in the crate page.
Please make sure you are viewing documentation generated for your target.
Re-exports§
pub use jni;
Structs§
- Broadcast
Receiver - Handles
android.content.BroadcastReceiver
object backed byJniProxy
. - Broadcast
Waiter - Waits for intents received by the managed
BroadcastReceiver
. - JniClass
Loader - Runtime class data loader. Wraps a global reference of
java.lang.ClassLoader
. - JniProxy
- Java dynamic proxy with an invocation handler backed by the Rust closure.
Traits§
- Auto
Local Globalize - Converts an
AutoLocal<'_>
to anGlobalRef
. - JObject
Auto Local - Used for calling
jni_clear_ex()
and turning an ownedJObject<'_>
reference (which leaks memory on dropping in a Rust main thread permanently attached to the JVM) into anAutoLocal
which deletes the reference from the environment on dropping. Works withandroid_activity
. Note that borrowed references (&JObject<'_>
) doesn’t cause memory leak. - JObject
Get - Gets the value from the Java object; calls
jni_clear_ex()
for an error. - JObject
New - Creates the Java object (wrapper) for the Rust value.
- JValue
GenGet - Gets the value returned from the Java method; calls
jni_clear_ex()
for an error.
Functions§
- android_
api_ level - Gets the API level (SDK version) of the current Android OS.
- android_
app_ name - Gets the raw name of the current Android application, parsed from the package name.
- android_
app_ package_ name - Gets the package name of the current Android application.
- android_
context - Gets the current
android.content.Context
, usually a reference ofNativeActivity
. This depends on cratendk_context
. - block_
for_ timeout - Convenient blocker for asynchronous functions, based on
futures_lite
andfutures_timer
. Warning: Blocking in theandroid_main()
thread will block the future’s completion if it depends on event processing in this thread (check your glue crate likeandroid_activity
). - jni_
attach_ permanently - Calls
jni_get_vm()
and tries attaching the current thread to the JVM permanently, in order to makejni_with_env
faster. Does nothing and returns false if the thread is currently attached (this behaviour is determined byjni-rs
). - jni_
clear_ ex - It calls
JNIEnv::exception_clear()
which is needed for handling Java exceptions, Not clearing it may cause the native program to crash on the next JNI call. Heavily used inside this crate, withResult::map_err()
. - jni_
clear_ ex_ ignore - It is the same as
jni_clear_ex_silent()
without storing the exception forjni_last_cleared_ex()
. - jni_
clear_ ex_ silent - It is the same as
jni_clear_ex()
without printing error information. Use it withResult::map_err()
prior to functions from this crate to avoid error printing. - jni_
get_ ⚠vm - Gets the
JavaVM
from current Android context. - jni_
last_ cleared_ ex - Takes away the stored reference of
java.lang.Throwable
of the last Java exception cleared inside this crate (current thread). - jni_
with_ env - Workaround for https://github.com/jni-rs/jni-rs/issues/558.
Calls
jni_get_vm()
, attaches the current thread to the JVM and executes the closure. The thread may be dettached if it has not been attached previously.