【问题标题】:How to configure NDK with Kotlin in Android Studio 3.0如何在 Android Studio 3.0 中使用 Kotlin 配置 NDK
【发布时间】:2017-07-14 08:47:00
【问题描述】:

我是 kotlin 的新手,我已经成功地使用 Android Studio(没有 kotlin)配置了 NDK,即在 java 中。

但是现在 google 已经引入了 kotlin,所以我想将我现有的项目更改为支持 NDK 的 kotlin。

这是我的java代码

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

请帮助我如何在 kotlin 中执行相同的代码

【问题讨论】:

  • 除了 NDK 之外,您是否尝试过设置 Kotlin?这样做的时候有没有遇到什么问题?
  • @zsmb13 是的,我的设置没有任何错误
  • 那么问题是什么?
  • 请看我编辑的问题

标签: android android-studio android-ndk kotlin


【解决方案1】:

您可以在 Medium 上阅读这篇文章:Android NDK: Interaction of Kotlin and C/C++

在本文中,作者了解了如何使 Kotlin 与 C/C++ 进行通信。

例如:

Kotlin 代码:

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

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 2015-10-31
    • 2017-12-22
    • 2015-03-17
    • 2015-05-21
    • 1970-01-01
    • 2019-10-18
    • 2017-12-16
    • 1970-01-01
    相关资源
    最近更新 更多