hfhbd
07/11/2022, 2:46 PMvar GetStringUTFChars: kotlinx.cinterop.CPointer<kotlinx.cinterop.CFunction<
in JNINativeInterface
. But getting this "function" fails and I don't know why. I don't invoke the function yet.package sample
import jni.*
import kotlinx.cinterop.*
@CName("Java_Main_hello")
fun hello(env: JNIEnv, obj: jobject, from: jstring) {
val getStringUTFChars = requireNotNull(env.pointed.GetStringUTFChars) {
"GET FAILED"
}
val cString = getStringUTFChars(env.reinterpret(), from, null)!!
val kString = cString.toKString()
env.pointed.ReleaseStringUTFChars!!(env.reinterpret(), from, cString)
println("HELLO FROM JNI: $kString")
}
Landry Norris
07/11/2022, 2:47 PMhfhbd
07/11/2022, 2:48 PMUncaught Kotlin exception: kotlin.IllegalArgumentException: GET FAILED
at 0 libhelloc.dylib 0x11f7955f3 kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 67
at 1 libhelloc.dylib 0x11f7956f7 kfun:kotlin.IllegalArgumentException#<init>(kotlin.String?){} + 55
at 2 libhelloc.dylib 0x11f7ae533 kfun:sample#hello(kotlinx.cinterop.CPointer<jni.JNINativeInterface_>;kotlinx.cinterop.CPointer<cnames.structs._jobject>;kotlinx.cinterop.CPointer<cnames.structs._jobject>){} + 583
at 3 libhelloc.dylib 0x11f7cd3d7 Java_Main_hello + 55
at 4 ???
fun main() {
Main().hello("Hello from Kotlin")
}
class Main {
init {
System.loadLibrary("helloc")
}
external fun hello(from: String)
}
Landry Norris
07/11/2022, 2:49 PMhfhbd
07/11/2022, 2:50 PMLandry Norris
07/11/2022, 2:50 PM@CName("Java_io_github_landrynorris_sample_JniBridge_methodWithParameters")
fun methodWithParameters(env: CPointer<JNIEnvVar>, thiz: jobject, intValue: Int) {
println("Got int from JVM. Value is $intValue")
}
Here’s how I define a function that does get called.hfhbd
07/11/2022, 2:51 PMLandry Norris
07/11/2022, 2:54 PMhfhbd
07/11/2022, 2:55 PMLandry Norris
07/11/2022, 2:55 PMhfhbd
07/11/2022, 2:58 PMLandry Norris
07/11/2022, 2:59 PMhfhbd
07/11/2022, 3:00 PMLandry Norris
07/11/2022, 3:01 PMval getChars = env.pointed.pointed?.GetStringUTFChars ?: error("Unable to get function to convert jstring")
val cPath = getChars(env, path, null) ?: error("Unable to get chars from jstring")
hfhbd
07/11/2022, 3:04 PMenv.pointed.pointed
Landry Norris
07/11/2022, 3:04 PMhfhbd
07/11/2022, 3:05 PMLandry Norris
07/11/2022, 3:06 PM@CName("Java_io_github_landrynorris_sample_JniBridge_methodWithParameters")
fun methodWithParameters(env: CPointer<JNIEnvVar>, thiz: jobject, intValue: Int)
hfhbd
07/11/2022, 3:07 PMLandry Norris
07/11/2022, 3:08 PMhfhbd
07/11/2022, 3:09 PMnatario1
07/11/2022, 4:28 PMLandry Norris
07/11/2022, 4:45 PMnatario1
07/11/2022, 7:38 PMLandry Norris
07/11/2022, 7:45 PMnatario1
07/11/2022, 7:51 PMLandry Norris
07/11/2022, 8:01 PM