android jni demo
1940 ワード
一、javaクラス
二、cmakelist内容
三、cppクラス、face_jni.cpp
public class FaceRebuildNative {
static {
System.loadLibrary("face_jni");
System.loadLibrary("faceRecon");
}
public native String stringFromJNI();
}
二、cmakelist内容
cmake_minimum_required(VERSION 3.4.1)
include_directories(src/main/cpp/include)
add_library( # Sets the name of the library.
face_jni
SHARED
src/main/cpp/face_jni.cpp)
add_library( faceRecon SHARED IMPORTED)
set_target_properties( faceRecon
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libfaceRecon.so)
target_link_libraries( # Specifies the target library.
face_jni
faceRecon
# Links the target library to the log library
# included in the NDK.
log )
三、cppクラス、face_jni.cpp
#include
#include
#include
#define REGISTER_CLASS "com/howard/facerebuild/FaceRebuildNative"
jstring stringFromJNIT(JNIEnv *env, jclass){
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
JNINativeMethod jniMethods[] = {
{ "stringFromJNI", "()Ljava/lang/String;", (void*)&stringFromJNIT},
};
JNIEXPORT
jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved){
JNIEnv *env;
if (vm->GetEnv(reinterpret_cast(&env), JNI_VERSION_1_6) != JNI_OK) {
return -1;
}
jclass clz = env->FindClass(REGISTER_CLASS);
env->RegisterNatives(clz, jniMethods, sizeof(jniMethods) / sizeof(JNINativeMethod));
env->DeleteLocalRef(clz);
return JNI_VERSION_1_6;
}
JNIEXPORT
void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved){
return ;
}