Back to Comprehensive Rust

Interoperability with Java

src/android/interoperability/java.md

latest1.3 KB
Original Source
<!-- Copyright 2022 Google LLC SPDX-License-Identifier: CC-BY-4.0 -->

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:

rust,compile_fail
# // Copyright 2022 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include java/src/lib.rs:hello}}

interoperability/java/Android.bp:

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

We then call this function from Java:

interoperability/java/HelloWorld.java:

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

interoperability/java/Android.bp:

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

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

shell
{{#include ../build_all.sh:helloworld_jni}}
<details>
  • The unsafe(no_mangle) attribute instructs Rust to emit the Java_HelloWorld_hello symbol exactly as written. This is important so that Java can recognize the symbol as a hello method on the HelloWorld class.

    • By default, Rust will mangle (rename) symbols so that a binary can link in two versions of the same Rust crate.
</details>