Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple JNI with Kotlin and Rust
package rust
external fun test(): String
fun main(args: Array<String>): Unit {
name = "hello"
version = "0.1.0"
authors = ["adelnizamutdinov"]
crate-type = ["dylib"]
jni-sys = "0.1.0"
cesu8 = "1.1.0"
extern crate jni_sys;
extern crate cesu8;
use jni_sys::*;
struct ExceptionThrown;
#[derive(Copy, Clone)]
struct SafeJNI(*mut jni_sys::JNIEnv);
impl SafeJNI {
fn string(&self, s: &str) -> Result<jni_sys::jstring, ExceptionThrown> {
let s = cvt(s);
let s = unsafe { ((**self.0).NewStringUTF)(self.0, s.as_ptr() as *const _) };
if s.is_null() {
return Err(ExceptionThrown);
fn cvt(s: &str) -> Vec<u8> {
let mut s = cesu8::to_java_cesu8(s).to_vec();
pub extern "C" fn Java_rust_AppKt_test(env: *mut JNIEnv, class: jclass) -> jstring {
SafeJNI(env).string("this is rust mate").unwrap()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.