2017-07-14 3 views
0

Je suis un novice avec kotlin, j'ai réussi à configurer NDK avec Android Studio (sans kotlin), c'est-à-dire en Java. Mais maintenant, google a introduit Kotlin, donc je veux changer mon projet existant en kotlin avec le support NDK.Comment configurer NDK avec Kotlin dans Android Studio 3.0

ceci est mon code java

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

S'il vous plaît aidez-moi comment faire même code dans Kotlin

+0

Avez-vous essayé la mise en place Kotlin en plus de la NDK? Avez-vous rencontré des problèmes en le faisant? – zsmb13

+0

Avez-vous lu: https://developer.android.com/kotlin/faq.html –

+0

@ zsmb13 Oui, j'ai configuré sans aucune erreur – jarvis

Répondre

0

Vous pouvez lire cet article sur Medium: Android NDK: Interaction of Kotlin and C/C++

Dans cet article, les auteurs ont vu comment faire communiquer Kotlin avec C/C++.

Exemple:

Code Kotlin:

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'; 
    } 
} 

échantillons ici: https://github.com/KucherenkoIhor/KotlinWithAndroidNdk