Here is what I do :
- create a Native C++ project use android studio template
- create a class com.jnitest.app.JNIInterface
package com.jnitest.app;
public class JNIInterface {
public static native String getString();
public static native String getName();
}
with the native-lib.cpp
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_jnitest_app_JNIInterface_getString(JNIEnv *env, jclass thiz) {
std::string name = "return String from JNIInterface";
return env->NewStringUTF(name.c_str());
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_jnitest_app_JNIInterface_getName(JNIEnv *env, jclass clazz) {
std::string name = "return name from JNIInterface";
return env->NewStringUTF(name.c_str());
}
- create a test class com.jnitest.app.JNITest
package com.jnitest.app;
public class JNITest {
{
System.loadLibrary("app");
}
public static void main(String[] args) {
System.out.println("Hello from JNITest");
System.out.println("String from JNI: " + JNIInterface.getString());
}
}
- build push and run
adb push .\build\intermediates\apk\debug\app-debug.apk /data/local/tmp/app-debug.jar
adb shell CLASSPATH=/data/local/tmp/app-debug.jar app_process ./ com.jnitest.app.JNITest
- get output
Hello from JNITest
Killed
Why I cannot get the right result ?