1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-29 17:10:46 +02:00
2022-12-21 16:38:28 +01:00

835 B

Interoperability with Java

Java can load shared objects via Java Native Interface (JNI). The jni crate allows you to create a compatible library.

First, we create a Rust function to export to Java:

interoperability/java/src/lib.rs:

{{#include java/src/lib.rs:hello}}

interoperability/java/Android.bp:

{{#include java/Android.bp:libhello_jni}}

Finally, we can call this function from Java:

interoperability/java/HelloWorld.java:

{{#include java/HelloWorld.java:HelloWorld}}

interoperability/java/Android.bp:

{{#include java/Android.bp:helloworld_jni}}

Finally, you can build, sync, and run the binary:

{{#include ../build_all.sh:helloworld_jni}}