简体   繁体   中英

How to configure NDK with Kotlin in Android Studio 3.0

I'm newbie with kotlin, I have successfully configured NDK with Android Studio (without kotlin) ie in java.

but now google has introduced kotlin so I want to change my existing project to kotlin with NDK support.

this is my java code

 static
 {
     System.loadLibrary("native-lib");
 }
 public native String stringFromJNI(int i);

Please help me how to do same code in kotlin

You can read this post on Medium: Android NDK: Interaction of Kotlin and C/C++

In this article, authors saw how to make Kotlin communicate with C/C++.

For Example:

Kotlin code:

class Store {

    companion object {
        init {
            System.loadLibrary("Store")
        }
    }

    @Throws(IllegalArgumentException::class)
    external fun getString(pKey: String): String
}

C++ code:

extern "C"
JNIEXPORT void JNICALL
Java_com_ihorkucherenko_storage_Store_setString(
        JNIEnv* pEnv,
        jobject pThis,
        jstring pKey,
        jstring pString) {
    StoreEntry* entry = allocateEntry(pEnv, &gStore, pKey);
    if (entry != NULL) {
        entry->mType = StoreType_String;
        jsize stringLength = pEnv->GetStringUTFLength(pString);
        entry->mValue.mString = new char[stringLength + 1];
        pEnv->GetStringUTFRegion(pString, 0, stringLength, entry->mValue.mString);
        entry->mValue.mString[stringLength] = '\0';
    }
}

Samples here: https://github.com/KucherenkoIhor/KotlinWithAndroidNdk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM