java_native is a JNI-compatible method signature generator for Rust libraries; it builds upon the
jni_fn by @antonok on Gitlab.
This crate was designed for use with the jni crate, which exposes JNI-compatible type bindings.
Although it's possible to use jni without java_native, the procedural macros defined here will make it easier to write the method signatures correctly.
Check the jni repo to get started with your first Rust JNI bindings.
Note the function signatures in the jni example project, which must be transcribed 100% correctly to avoid runtime panics in your JVM project:
#[no_mangle]
pub extern "system" fn Java_HelloWorld_hello(
// ...Instead, java_native can automatically generate the correct function signature based on the package name (HelloWorld) and function name (hello):
use jni_fn::jni_fn;
#[jni_fn("HelloWorld")]
pub fn hello(
// ...The jni macro is especially useful in more complicated examples - you don't want to figure this out manually! With jni, all you need is:
#[jni("org.signal.client.internal.Native")]
pub unsafe fn IdentityKeyPair_Deserialize(
// ...For hook functions like JNI_OnLoad or JNI_OnLoad_libname, use:
// as a dynamic library:
#[on_load] // becomes `JNI_OnLoad`
pub unsafe fn on_load(vm: JavaVM) -> jint {
// your init code...
JNI_VERSION_1_8
}
// as a static library:
#[on_load(example)] // becomes `JNI_OnLoad_example`
pub unsafe fn on_load(vm: JavaVM) -> jint {
// your init code...
JNI_VERSION_1_8
}There is an on_load and on_unload attribute; pass an attribute name for a static binding.
Visit the docs for more instructions and examples.