1

How to work with Kotlin nullable type like 'Long?', 'String?' etc. on JNI? JNI type signatures represent the following information:

Signature  |Java Type
-----------|----------------
    Z      | boolean
    B      | byte
    C      | char
    S      | short
    I      | int
    J      | long
    F      | float
    D      | double
    L      | fully-qualified-class;
    [ type | type[]
    (arg-types)| ret-type method type

But how I can use nullable 'var value:Long?' from JNI?

Kotlin Code

class KtClass
{
    var value:Long? = null
}

JNI

jclass ktclass = env->FindClass("package/KtClass");
jmethodID ctorktID = env->GetMethodID(ktclass, "<init>", "()V");
env->NewObject(ktclass, ctorktID);

//J - mean Kotlin Long, but it not nullable 'Long?'
jfieldID valueID = env->GetFieldID(ktclass, "value", "J");
env->SetLongField(ktclass, valueID, NULL); //Error
Anonimys
  • 596
  • 1
  • 5
  • 14

1 Answers1

3

Long? becomes java.lang.Long on JVM, so use

env->GetFieldID(ktclass, "value", "Ljava/lang/Long;")
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487