如何从 Kotlin/Native 调用 JNIEnv 函数
How can I call JNIEnv function from Kotlin/Native
jni.h
提供这个
struct JNINativeInterface_ {
...
jint (JNICALL *GetVersion)(JNIEnv *env);
...
}
在C
中调用可以写成
void test(JNIEnv *env){
// C
jint version = (*env)->GetVersion(env);
// C++
// jint version = env->GetVersion();
}
然后我该如何在 kotlin 中做到这一点?
fun test(env: CPointer<JNIEnvVar>){
val version = // how?
}
在 google 中搜索答案后,Kotlin/Native
和 JNI
的示例很少,但它们只是基本示例,请帮助。
提前致谢。
感谢迈克尔。
长答案是
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)!!
}
希望这可以帮助别人理解
Kotlin/Native.
jni.h
提供这个
struct JNINativeInterface_ {
...
jint (JNICALL *GetVersion)(JNIEnv *env);
...
}
在C
中调用可以写成
void test(JNIEnv *env){
// C
jint version = (*env)->GetVersion(env);
// C++
// jint version = env->GetVersion();
}
然后我该如何在 kotlin 中做到这一点?
fun test(env: CPointer<JNIEnvVar>){
val version = // how?
}
在 google 中搜索答案后,Kotlin/Native
和 JNI
的示例很少,但它们只是基本示例,请帮助。
提前致谢。
感谢迈克尔。
长答案是
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)!!
}
希望这可以帮助别人理解 Kotlin/Native.