作者VVll (信)
看板AndroidDev
标题Re: [问题] NDK的helloworld
时间Tue Jun 19 14:35:34 2012
刚看到一个避免 写错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
1F:→ iincho:这是Android framework JNI的标准注册方式.. 06/19 17:33
2F:→ Killercat:其实直接用javah产生header不是很好吗 XD 06/20 00:39
3F:→ VVll:那个名称 好丑又好长(遮脸 06/20 23:42
4F:→ iincho:据说这种做法效率比较好..... 06/21 10:57