I\'m not familiar with the Android NDK and I need help to build a Kotlin application using native part of code written in C++. I\'ve found HelloWorld sample using a basic C
Ok, thanks to @Richard, here is my wrapper
ObjectWrapper.kt
Exposes the API I want to use. The keyword external
of the method indicates that the body is elsewhere. In my case, in the native layer of the wrapper.
class ObjectWrapper {
companion object {
init {
System.loadLibrary("mylibrary")
}
}
external fun sayHello(name: String): String
}
MyLibrary.cpp
Same API as the kotlin part but the methods are named with the completed package name. Here, I have to the translation between the kotlin world and the native world.
#include <jni.h>
#include <string>
#include "Object.h"
static Object *pObject = NULL;
extern "C" {
JNIEXPORT jstring Java_com_packagename_ObjectWrapper_sayHello(
JNIEnv *env,
jobject /* this */,
jstring input) {
pObject = new Object();
const char *msg = env->GetStringUTFChars(input, JNI_FALSE);
std::string hello = pObject->getHelloString(msg);
return env->NewStringUTF(hello.c_str());
}
}