Andoird 中使用了一种不同传统Java JNI的方式来定义其native的函数。其中很重要的区别是Andorid使用了一种Java 和 C 函数的映射表数组,并在其中描述了函数的参数和返回值。这个数组的类型是JNINativeMethod,定义如下:
typedef struct {
const char* name;const char* signature;void* fnPtr;} JNINativeMethod;第一个变量name是Java中函数的名字。
第二个变量signature,用字符串是描述了函数的参数和返回值
第三个变量fnPtr是函数指针,指向C函数。
其中比较难以理解的是第二个参数,例如
"()V"
"(II)V"
"(Ljava/lang/String;Ljava/lang/String;)V"
实际上这些字符是与函数的参数类型一一对应的。
"()" 中的字符表示参数,后面的则代表返回值。例如"()V" 就表示void Func();
"(II)V" 表示 void Func(int, int);
具体的每一个字符的对应关系如下
字符 Java类型 C类型
V void void
Z jboolean booleanI jint intJ jlong longD jdouble doubleF jfloat floatB jbyte byteC jchar charS jshort short
数组则以"["开始,用两个字符表示
[I jintArray int[]
[F jfloatArray float[][B jbyteArray byte[][C jcharArray char[][S jshortArray short[][D jdoubleArray double[][J jlongArray long[][Z jbooleanArray boolean[]
上面的都是基本类型。如果Java函数的参数是class,则以"L"开头,以";"结尾中间是用"/" 隔开的包及类名。而其对应的C函数名的参数则为jobject. 一个例外是String类,其对应的类为jstring
Ljava/lang/String; String jstring
Ljava/net/Socket; Socket jobject
如果JAVA函数位于一个嵌入类,则用$作为类名间的分隔符。
例如 "(Ljava/lang/String;Landroid/os/FileUtils$FileStatus;)Z"
下边是我在做串口通信时的代码:
- static const char *classPathName = "android/serialport/SerialPort";
- //注意Ljava/io/FileDescriptor;最后的分号,刚开始做时漏了这个分号,查了两天时间,汗
- static JNINativeMethod methods[] = {
- { "open", "(Ljava/lang/String;I)Ljava/io/FileDescriptor;", (void*)android_serialport_SerialPort_open },
- { "close", "()V", (void*)android_serialport_SerialPort_close },
- };
- /*
- * Register several native methods for one class.
- */
- 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)
- {
- LOGE("register nativers error");
- return JNI_FALSE;
- }
- return JNI_TRUE;
- }
- /*
- * Register native methods for all classes we know about.
- *
- * returns JNI_TRUE on success.
- */
- static int registerNatives(JNIEnv* env)
- {
- if (!registerNativeMethods(env, classPathName,
- methods, sizeof(methods) / sizeof(methods[0]))) {
- return JNI_FALSE;
- }
- return JNI_TRUE;
- }
- /*
- * This is called by the VM when the shared library is first loaded.
- */
- jint JNI_OnLoad(JavaVM* vm, void* reserved)
- {
- JNIEnv* env = NULL;
- jint result = -1;
- LOGI("Entering JNI_OnLoad\n");
- if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK)
- goto bail;
- assert(env != NULL);
- if (!registerNatives(env))
- goto bail;
- /* success -- return valid version number */
- result = JNI_VERSION_1_4;
- bail:
- LOGI("Leaving JNI_OnLoad (result=0x%x)\n", result);
- return result;
- }
static const char *classPathName = "android/serialport/SerialPort";//注意Ljava/io/FileDescriptor;最后的分号,刚开始做时漏了这个分号,查了两天时间,汗static JNINativeMethod methods[] = { {"open", "(Ljava/lang/String;I)Ljava/io/FileDescriptor;", (void*)android_serialport_SerialPort_open }, {"close", "()V", (void*)android_serialport_SerialPort_close },};/* * Register several native methods for one class. */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) { LOGE("register nativers error"); return JNI_FALSE; } return JNI_TRUE;}/* * Register native methods for all classes we know about. * * returns JNI_TRUE on success. */static int registerNatives(JNIEnv* env){ if (!registerNativeMethods(env, classPathName, methods, sizeof(methods) / sizeof(methods[0]))) { return JNI_FALSE; } return JNI_TRUE;}/* * This is called by the VM when the shared library is first loaded. */jint JNI_OnLoad(JavaVM* vm, void* reserved){ JNIEnv* env = NULL; jint result = -1; LOGI("Entering JNI_OnLoad\n"); if ((*vm)->GetEnv(vm, (void**) &env, JNI_VERSION_1_4) != JNI_OK) goto bail; assert(env != NULL); if (!registerNatives(env)) goto bail; /* success -- return valid version number */ result = JNI_VERSION_1_4;bail: LOGI("Leaving JNI_OnLoad (result=0x%x)\n", result); return result;}
android_serialport_SeriaPort_open的函数原型为:
static jobject android_serialport_SerialPort_open(JNIEnv *env, jobject thiz, jstring path, jint baudrate);
static void android_serialport_SerialPort_close(JNIEnv *env, jobject thiz);
另外还要注意一点,如果是C++,使用的是env, 如果是C,使用的是(*env),最好参考相应系统中的代码来写。