android jni demo

一、java类

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 <jni.h>
#include <string>
#include <android/log.h>


#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<void **>(&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 ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值