#include "org_bing_testjni_MainActivity.h"
#include <stdio.h>
#include <string>
#include <android/log.h>
using namespace std;
__attribute__((visibility("hidden"))) jstring JNICALL sayHello (JNIEnv *, jobject, jstring);
__attribute__((visibility("hidden"))) JNINativeMethod g_Methods[] = {
"sayHello",
"(Ljava/lang/String;)Ljava/lang/String;",
(void*) sayHello
};
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
__android_log_print(ANDROID_LOG_DEBUG, "__BING__", "JNI_OnLoad");
JNIEnv *pEnv = NULL;
//獲取環境
jint ret = vm->GetEnv((void**) &pEnv, JNI_VERSION_1_6);
if (ret != JNI_OK) {
__android_log_print(ANDROID_LOG_DEBUG, "__BING__", "jni_replace JVM ERROR:GetEnv");
return -1;
}
jclass cls = pEnv->FindClass("org/bing/testjni/MainActivity");
if (cls == NULL) {
__android_log_print(ANDROID_LOG_DEBUG, "__BING__", "jni_replace:FindClass Error");
return -1;
}
//動態註冊本地方法
ret = pEnv->RegisterNatives(cls, g_Methods,sizeof(g_Methods) / sizeof(g_Methods[0]));
if (ret != JNI_OK) {
__android_log_print(ANDROID_LOG_DEBUG, "__BING__", "jni_replace:FindClass Error");
return -1;
}
//返回java版本
return JNI_VERSION_1_6;
}
__attribute__((visibility("hidden"))) jstring JNICALL sayHello(JNIEnv* env,jobject obj, jstring name) {
const char* pname = env->GetStringUTFChars(name, NULL);
string str_info = "replace say hello function:";
str_info += pname;
jstring ret_str = env->NewStringUTF(str_info.c_str());
return ret_str;
}