美文网首页JNI
JNI (Java Native Interface)

JNI (Java Native Interface)

作者: UNCLE_CHUCK | 来源:发表于2018-03-01 16:44 被阅读0次

    Wiki: https://en.wikipedia.org/wiki/Java_Native_Interface

    The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.

    Design

    In the JNI framework, native functions are implemented in separate .c or .cpp files. (C++ provides a slightly simpler interface with JNI.) When the JVM invokes the function, it passes a JNIEnv pointer, a jobject pointer, and any Java arguments declared by the Java method. For example, the following converts a Java string to a native string:

    extern "C"
    JNIEXPORT void JNICALL Java_ClassName_MethodName
      (JNIEnv *env, jobject obj, jstring javaString)
    {
        const char *nativeString = env->GetStringUTFChars(javaString, 0);
    
        //Do something with the nativeString
    
        env->ReleaseStringUTFChars(javaString, nativeString);
    }
    

    Android JNI 使用的数据结构JNINativeMethod详解

    原文链接: http://blog.csdn.net/bigapple88/article/details/6756204

    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 boolean
    I jint int
    J jlong long
    D jdouble double
    F jfloat float
    B jbyte byte
    C jchar char
    S 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;  
    }  
    

    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),最好参考相应系统中的代码来写。

    相关文章

      网友评论

        本文标题:JNI (Java Native Interface)

        本文链接:https://www.haomeiwen.com/subject/udxnxftx.html