In our previous article, Android NDK: The Interaction Between Kotlin and C/C++, we shared the results of our research into the Android NDK (Native Development Kit). In that article, you can find out how to set C/C++ and interaction. We showed how can invoke C/C++ code with any types. We also converted String instances and passed Kotlin objects to native code, called functions, and threw JDK (Java Development Kit) exceptions.

Now it’s time to move to the practical part working with the Android NDK. In this article, we’ll dive deeper into the topic of working with Kotlin in the Android NDK and will tell you how to call Kotlin from native code.

Why do you need to call Kotlin from native code?

The most obvious reason to call Kotlin from native code is for callbacks. When you perform an operation, you need to know when it’s done and receive the result of that operation. This is true whether you work with Kotlin or the JNI (Java Native Interface). In this article, we’ll give examples of how to find out when a new value is persisted to memory. We showed how a new value is created in one of our previous articles.

Read also: Android NDK: The Interaction Between Kotlin and C/C++


Calling static properties

The JNIEnv pointer contains many functions to deal with static fields. First, you have to get the ID of the static field using the jfieldID GetStaticFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig) function.

Kotlin doesn’t have static members, but each Kotlin class can have a companion object, so you can use companion objects’ fields and functions similarly. A companion object is initialized when the corresponding class is loaded, matching the semantics of the Java static initializer. Thus, you can use a companion object to load native libraries:

class Store {

    companion object {
        init {

You can create a field in the companion object:

companion object {

+        val static: Int = 3

        init {


JNIEnv contains different functions to static field values from native code:


[Image source: JavaSE documentation]

You can get the int value as follows:

jclass clazz = env->FindClass("com/ihorkucherenko/storage/Store");
jfieldID fieldId = env->GetStaticFieldID(clazz, "static", "I");
if (fieldId == NULL) {
    __android_log_print(ANDROID_LOG_INFO,  __FUNCTION__, "fieldId == null");
} else {
    jint fieldValue = env->GetStaticIntField(clazz, fieldId);
    __android_log_print(ANDROID_LOG_INFO,  __FUNCTION__, "Field value: %d ", fieldValue); //Field value: 3

To determine a method’s signature, use type codes. The following table summarizes the various types available in the JNI, along with their codes:


In the picture above, the compiler generates a private static field in the Store class. The Store class contains fields with the private modifier. You can’t access those fields from the Store class. But with the JNI, you can access the values of those fields even if the private modifier is set.

// access flags 0x31
public final class com/ihorkucherenko/storage/Store {
  // access flags 0xA
  private static I static

If you just create this field in a simple class, you get an exception: java.lang.NoSuchFieldError: no “I” field “static” in class “Lcom/ihorkucherenko/storage/Store;” or its superclasses. If you use the Int? type, you also receive this error. To this error, follow the instructions below.

Calling static methods

To get the ID of a static method, you have to use jmethodID GetStaticMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig).

JNIEnv contains three types of functions that can call static methods:

1. NativeType CallStatic<type>Method(JNIEnv *env, jclass clazz, jmethodID methodID, ...)

2. NativeType CallStatic<type>MethodA(JNIEnv *env, jclass clazz, jmethodID methodID, jvalue *args)

3. NativeType CallStatic<type>MethodV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args)

These types of functions differ in their arguments. Therefore, we have the following set of functions:


[Image source: Java SE Documantation]

Let’s try to invoke static methods.

class Store {


    companion object {
        fun staticMethod(int: Int) = int

        init {

You have to use @JvmStatic to generate an additional static method. You can see this static metod in bytecode:

// access flags 0x19
  public final static staticMethod(I)I
    GETSTATIC com/ihorkucherenko/storage/Store.Companion : Lcom/ihorkucherenko/storage/Store$Companion;
    ILOAD 0
    INVOKEVIRTUAL com/ihorkucherenko/storage/Store$Companion.staticMethod (I)I
    MAXSTACK = 2

Pay attention to the (I)I. This is the JNI’s representation of the type signature.


Types of arguments are specified in parentheses. Here are several examples:

1. Kotlin:

fun staticMethod(int: Int, floaf: Float) = int

Native code:

public final staticMethod(IF)I

2. Kotlin:

fun staticMethod(int: Int, floaf: Float, string: String) = int

Native code:

public final staticMethod(IFLjava/lang/String;)I

You should point the return type after parentheses.

Here’s how we call a Kotlin function from native code:

jclass clazz = env->FindClass("com/ihorkucherenko/storage/Store");
jmethodID methodId = env->GetStaticMethodID(clazz, "staticMethod", "(I)I");
if (methodId == NULL) {
    __android_log_print(ANDROID_LOG_INFO,  __FUNCTION__, "methodId == null");
} else {
    jint value = env->CallStaticIntMethod(clazz, methodId, 4);
    __android_log_print(ANDROID_LOG_INFO,  __FUNCTION__, "value: %d ", value); //value: 4

Calling an instance’s properties

Similarly to the previous code examples, you should use functions to retrieve the ID of the field and value: jfieldID GetFieldID(JNIEnv *env, jclass clazz, const char *name, const char *sig).


Let’s add private val property = 3, which is equal to private final I property = 3 in bytecode, to the Store class. After that, you have to get jobject, which refers to the instance of the Store class, in order to retrieve the value from property. For example, you can create a new jobject inside the native code and get the value:

jclass clazz = env->FindClass("com/ihorkucherenko/storage/Store");
jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
jobject storeObject = env->NewObject(clazz, constructor);

jfieldID fieldId = env->GetFieldID(clazz, "property", "I");
jint value = env->GetIntField(storeObject, fieldId);
__android_log_print(ANDROID_LOG_INFO,  __FUNCTION__, "Property value: %d ", value); //Property value: 3 

Calling an instance’s methods

There is no significant difference between calling static and instance methods. Let’s look at an example:

jclass clazz = env->FindClass("com/ihorkucherenko/storage/Store");
jmethodID constructor = env->GetMethodID(clazz, "<init>", "()V");
jobject storeObject = env->NewObject(clazz, constructor);

jmethodID methodId = env->GetMethodID(clazz, "getSomething", "()I");
jint value = env->CallIntMethod(storeObject, methodId);
__android_log_print(ANDROID_LOG_INFO,  __FUNCTION__, "value: %d ", value); //value: 3

The difference between calling static and instance methods is that for instance method, you need to have jobject to pass it to CallIntMethod.

Callback example

Let’s add a simple callback to the Store:

interface StoreListener {

    fun onIntegerSet(: String, value: Int)

    fun onStringSet(: String, value: String)

class Store : StoreListener {

    val listeners = mutableListOf<StoreListener>()

    override fun onIntegerSet(: String, value: Int) {
        listeners.forEach { it.onIntegerSet(, value) }

    override fun onStringSet(: String, value: String) {
        listeners.forEach { it.onStringSet(, value) }

You can invoke these methods from proper native functions:

        JNIEnv* pEnv,
        jobject pThis,
        jstring pKey,
        jstring pString) {
    // Turns the Java string into a temporary C string.
    StoreEntry* entry = allocateEntry(pEnv, &gStore, pKey);
    if (entry != NULL) {
        entry->mType = StoreType_String;
        // Copy the temporary C string into its dynamically allocated
        // final location. Then releases the temporary string.
        jsize stringLength = pEnv->GetStringUTFLength(pString);
        entry->mValue.mString = new char[stringLength + 1];
        // Directly copies the Java String into our new C buffer.
        pEnv->GetStringUTFRegion(pString, 0, stringLength, entry->mValue.mString);
        // Append the null character for string termination.
        entry->mValue.mString[stringLength] = '


Please enter your comment!
Please enter your name here