Взаємодія з Java
Java може завантажувати спільні об’єкти через Java Native Interface (JNI). Крейт jni дозволяє створити сумісну бібліотеку.
Спочатку ми створюємо функцію Rust для експорту в Java:
interoperability/java/src/lib.rs:
#![allow(unused)]
fn main() {
//! Rust <-> Java FFI демонстрація.
use jni::objects::{JClass, JString};
use jni::sys::jstring;
use jni::JNIEnv;
/// Реалізація методу HelloWorld::hello.
// БЕЗПЕКА: Не існує іншої глобальної функції з таким іменем.
#[unsafe(no_mangle)]
pub extern "system" fn Java_HelloWorld_hello(
mut env: JNIEnv,
_class: JClass,
name: JString,
) -> jstring {
let input: String = env.get_string(&name).unwrap().into();
let greeting = format!("Привіт, {input}!");
let output = env.new_string(greeting).unwrap();
output.into_raw()
}
}
interoperability/java/Android.bp:
rust_ffi_shared {
name: "libhello_jni",
crate_name: "hello_jni",
srcs: ["src/lib.rs"],
rustlibs: ["libjni"],
}
Потім ми можемо викликати цю функцію з Java:
interoperability/java/HelloWorld.java:
class HelloWorld {
private static native String hello(String name);
static {
System.loadLibrary("hello_jni");
}
public static void main(String[] args) {
String output = HelloWorld.hello("Alice");
System.out.println(output);
}
}
interoperability/java/Android.bp:
java_binary {
name: "helloworld_jni",
srcs: ["HelloWorld.java"],
main_class: "HelloWorld",
required: ["libhello_jni"],
}
Нарешті, ви можете створити, синхронізувати та запустити бінарний файл:
m helloworld_jni
adb sync # requires adb root && adb remount
adb shell /system/bin/helloworld_jni
-
The
unsafe(no_mangle)attribute instructs Rust to emit theJava_HelloWorld_hellosymbol exactly as written. This is important so that Java can recognize the symbol as ahellomethod on theHelloWorldclass.- By default, Rust will mangle (rename) symbols so that a binary can link in two versions of the same Rust crate.