JNI 简介

jni 简介

jni的全称就是Java Native Interface,顾名思义,就是Java和C/C++相互通信的接口;

jni开发的代码会被编译成so文件,然后在java中加载so文件;

so文件加载

java 加载so文件主要是两种方式

  1. loadlibrary

    调用 System.loadLibrary(libname) 或 Runtime.getRuntime().loadLibrary(libname) 加载 so 文件,只需要指定 so 文件的名字,就会默认从系统的共享库目录里面去查找;

  2. load

    调用 System.load(filename) 或 Runtime.getRuntime().load(filename)加载so库,需要指定完整的so文件路径;

jni.h

jni 开发需要引用 jni.h;jni.h中主要包含以下部分:

  1. 基本数据类型

    typedef uint8_t         jboolean;       /* unsigned 8 bits */
    typedef int8_t          jbyte;          /* signed 8 bits */
    typedef uint16_t        jchar;          /* unsigned 16 bits */
    typedef int16_t         jshort;         /* signed 16 bits */
    typedef int32_t         jint;           /* signed 32 bits */
    typedef int64_t         jlong;          /* signed 64 bits */
    typedef float           jfloat;         /* 32-bit IEEE 754 */
    typedef double          jdouble;        /* 64-bit IEEE 754 */
    typedef unsigned char   jboolean;       /* unsigned 8 bits */
    typedef signed char     jbyte;          /* signed 8 bits */
    typedef unsigned short  jchar;          /* unsigned 16 bits */
    typedef short           jshort;         /* signed 16 bits */
    typedef int             jint;           /* signed 32 bits */
    typedef long long       jlong;          /* signed 64 bits */
    typedef float           jfloat;         /* 32-bit IEEE 754 */
    typedef double          jdouble;        /* 64-bit IEEE 754 */
    
    typedef jint            jsize;          /* "cardinal indices and sizes" */
  2. 数组类型

    class _jobject {};
    class _jclass : public _jobject {};
    class _jstring : public _jobject {};
    class _jarray : public _jobject {};
    class _jobjectArray : public _jarray {};
    class _jbooleanArray : public _jarray {};
    class _jbyteArray : public _jarray {};
    class _jcharArray : public _jarray {};
    class _jshortArray : public _jarray {};
    class _jintArray : public _jarray {};
    class _jlongArray : public _jarray {};
    class _jfloatArray : public _jarray {};
    class _jdoubleArray : public _jarray {};
    class _jthrowable : public _jobject {};
    
    typedef void*           jobject;
    typedef jobject         jclass;
    typedef jobject         jstring;
    typedef jobject         jarray;
    typedef jarray          jobjectArray;
    typedef jarray          jbooleanArray;
    typedef jarray          jbyteArray;
    typedef jarray          jcharArray;
    typedef jarray          jshortArray;
    typedef jarray          jintArray;
    typedef jarray          jlongArray;
    typedef jarray          jfloatArray;
    typedef jarray          jdoubleArray;
    typedef jobject         jthrowable;
    typedef jobject         jweak;
  3. 公共体类型

    typedef union jvalue {
       jboolean    z;
       jbyte       b;
       jchar       c;
       jshort      s;
       jint        i;
       jlong       j;c
       jfloat      f;
       jdouble     d;
       jobject     l;
    } jvalue;
  4. 方法id

    struct _jfieldID;                       /* opaque structure */
    typedef struct _jfieldID* jfieldID;     /* field IDs */
  5. 方法id

    struct _jmethodID;                      /* opaque structure */
    typedef struct _jmethodID* jmethodID;   /* method IDs */
  6. 引用类型定义

    typedef enum jobjectRefType {
       JNIInvalidRefType = 0,
       JNILocalRefType = 1,
       JNIGlobalRefType = 2,
       JNIWeakGlobalRefType = 3
    } jobjectRefType;
  7. 错误类型定义

    
    #define JNI_FALSE   0
    
    
    #define JNI_TRUE    1
    
    
    
    #define JNI_OK          (0)         /* no error */
    
    
    #define JNI_ERR         (-1)        /* generic error */
    
    
    #define JNI_EDETACHED   (-2)        /* thread detached from the VM */
    
    
    #define JNI_EVERSION    (-3)        /* JNI version error */
    
    
    
    #define JNI_COMMIT      1           /* copy content, do not free buffer */
    
    
    #define JNI_ABORT       2           /* free buffer w/o copying back */
    
  8. JNINativeInterface

    struct JNINativeInterface;
    struct _JNIEnv;
    
    typedef _JNIEnv JNIEnv;
    typedef const struct JNINativeInterface* C_JNIEnv;
    typedef const struct JNINativeInterface* JNIEnv;
  9. JNIInvokeInterface

    struct JNIInvokeInterface_;  
    struct JavaVM_;  
    
    #ifdef __cplusplus  
    
    typedef JavaVM_ JavaVM;  
    
    #else  
    
    typedef const struct JNIInvokeInterface_ *JavaVM;  
  10. 装载与卸载方法

    JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved);
    JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved);

猜你喜欢

转载自blog.csdn.net/qq_20404903/article/details/80662195
JNI