Re: [問題] NDK的helloworld
剛看到一個避免 寫錯jni函式名稱的方法
參考 http://blog.csdn.net/jianguo_liao19840726/article/details/6719224
使用JNI RegisterNatives這個方法
直接將打算在JDK中呼叫的方法 在NDK中註冊
可以避免寫一長串的函數名稱
比如Java_com_test_className_hello(JNIEnv *env, jclass clazz)
透過此方法 只要寫成 hello(JNIEnv *env, jclass clazz)
static JNINativeMethod gMethods[] = {
{ "hello", "()Ljava/lang/String;", (void*)native_hello }
};
//資料結構
typedef struct {
const char* name;//java方法名稱
const char* signature; //java方法簽名
void* fnPtr;//c/c++的函数指標
} JNINativeMethod;
然後使用
static const char *classPathName = "com/test/JDKTest";
static int registerNatives(JNIEnv* env)
{
if (!registerNativeMethods(env, classPathName, gMethods, sizeof(gMethods) / sizeof(gMethods[0])))
{
return JNI_FALSE;
}
return JNI_TRUE;
}
static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int numMethods)
{
jclass clazz;
clazz = (*env)->FindClass(env, className);
if (clazz == NULL)
{
return JNI_FALSE;
}
if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0)
{
return JNI_FALSE;
}
return JNI_TRUE;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.170.121.118
→
06/19 17:33, , 1F
06/19 17:33, 1F
→
06/20 00:39, , 2F
06/20 00:39, 2F
→
06/20 23:42, , 3F
06/20 23:42, 3F
→
06/21 10:57, , 4F
06/21 10:57, 4F
討論串 (同標題文章)
完整討論串 (本文為第 4 之 4 篇):