How can I call JNIEnv function from Kotlin/Native

Viewed 146

jni.h provide this

struct JNINativeInterface_ {
    ...
    jint (JNICALL *GetVersion)(JNIEnv *env);
    ...
}

to call it in C can be written as

void test(JNIEnv *env){
    // C
    jint version = (*env)->GetVersion(env);

    // C++
    // jint version = env->GetVersion(); 
}

and then how can I do it in kotlin?

fun test(env: CPointer<JNIEnvVar>){
    val version = // how?
}

After searching answer in google there are few example for Kotlin/Native with JNI but they're just basic example please help.

Thanks in advance.

1 Answers

Thanks to Michael.

Long answer is

fun test(env: CPointer<JNIEnvVar>){
    // getting "JNINativeInterface_" reference from CPointer<JNIEnvVar> by 
    val jni:JNINativeInterface_ = env.pointed.pointed!!

    // get function reference from JNINativeInterface_
    // IntelliJ can help to find existing methods
    val func = jni.GetVersion!! 

    // call a function
    var version = func.invoke(env)

    // above expression can be simplify as
    version = env.pointed.pointed!!.GetVersion!!(env)!!
}

hope this can help somebody to understand Kotlin/Native .

Related