cinterop question - trying to emulate: ```JavaVM* ...
# kotlin-native
cinterop question - trying to emulate:
Copy code
JavaVM* vm;
With kotlin.cinterop types and my solution seems so verbose. Are there better APIs that I'm missing?
Copy code
val vm: CPointerVar<JavaVMVar> = allocPointerTo()
val vm2: CPointerVar<CPointerVar<JavaVMVar>> = allocPointerTo()
vm2.pointed = vm
Depends on what you are trying to do?
Copy code
fun JNI_OnLoad(
    vm: kotlinx.cinterop.CPointer<JavaVMVar>,
    reserved: kotlinx.cinterop.CValuesRef<*>
): {
    mJniBridge = JniBridge(vm)
    return JNI_VERSION_1_6

fun JNI_OnUnload(vm: kotlinx.cinterop.CPointer<JavaVMVar>?, reserved: kotlinx.cinterop.CValuesRef<*>?): kotlin.Unit {
    mJniBridge = null
Is available if you are using code that loads your/a library from java
Yes, thanks! I'm aware of that, but we can't use it.
itself is working fine, but I was wondering if kotlin.cinterop had some better APIs especially for doing
, which is three characters in C and three lines in Kotlin.
Ah if that is your question, thats basically how it is
👍 1
hey, I have a question, I got this code
Copy code
    fun JNI_OnLoad(
        vm: CPointer<JavaVMVar>,
        preserved: COpaquePointer
    ): jint {
        logInfo("JNI On Load")
        return memScoped {
            val envStorage = alloc<CPointerVar<JNIEnvVar>>()
            val vmValue = vm.pointed.pointed!!
I don’t know why but the
throws a NullPointerException in an Android Emulator (X86)
Copy code
I/KonanActivity: JNI On Load
I/Konan_main: Uncaught Kotlin exception: 
I/Konan_main:     at  (0xeb2ee03f)
        at  (0xeb2f460d)
        at JNI_OnLoad (0xeb325703)
        at _ZN3art9JavaVMExt17LoadNativeLibraryEP7_JNIEnvRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEP8_jobjectP7_jclassPS9_ (0xeeda4609)
        at JVM_NativeLoad (0x9c26e2a3)
        at Runtime_nativeLoad (0x9c2309d7)
        at  (0x6f9988db)
A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 14634 (, pid 14634 (
I don’t know what I’m doing wrong, but at this point I’m crying for some pointers since I’ve been fighting this issue for days
my apologies… I finally got what the error was, apparently it’s NO USE to make those functions within a kotlin class. Apparently they NEED TO BE top level functions
👍 1