如何在Android Studio 3.0中使用Kotlin配置NDK
我与kotlin新手,我已经成功地配置NDK与Android Studio(无kotlin),即在Java中。
但现在谷歌已经介绍了kotlin,所以我想改变我的现有项目kotlin与NDK的支持。
这是我的java代码
static { System.loadLibrary("native-lib"); } public native String stringFromJNI(int i);
请帮助我如何在kotlin中执行相同的代码
你可以阅读这篇文章: Android NDK:Kotlin和C / C ++的交互
在本文中,作者看到了如何让Kotlin与C / C ++进行通信。
例如:
科特林代码:
class Store { companion object { init { System.loadLibrary("Store") } } @Throws(IllegalArgumentException::class) external fun getString(pKey: String): String }
C ++代码:
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'; } }
这里的示例: https : //github.com/KucherenkoIhor/KotlinWithAndroidNdk