jni的全稱就是Java Native Interface酥夭,顧名思義赐纱,就是Java和C/C++相互通信的接口;
jni開發(fā)的代碼會被編譯成so文件熬北,然后在java中加載so文件疙描;
so文件加載
java 加載so文件主要是兩種方式
-
loadlibrary
調用 System.loadLibrary(libname) 或 Runtime.getRuntime().loadLibrary(libname) 加載 so 文件,只需要指定 so 文件的名字讶隐,就會默認從系統(tǒng)的共享庫目錄里面去查找起胰;
-
load
調用 System.load(filename) 或 Runtime.getRuntime().load(filename)加載so庫,需要指定完整的so文件路徑巫延;
jni.h
jni 開發(fā)需要引用 jni.h效五;jni.h中主要包含以下部分:
-
基本數(shù)據(jù)類型
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" */
-
數(shù)組類型
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;
-
公共體類型
typedef union jvalue { jboolean z; jbyte b; jchar c; jshort s; jint i; jlong j;c jfloat f; jdouble d; jobject l; } jvalue;
-
方法id
struct _jfieldID; /* opaque structure */ typedef struct _jfieldID* jfieldID; /* field IDs */
-
方法id
struct _jmethodID; /* opaque structure */ typedef struct _jmethodID* jmethodID; /* method IDs */
-
引用類型定義
typedef enum jobjectRefType { JNIInvalidRefType = 0, JNILocalRefType = 1, JNIGlobalRefType = 2, JNIWeakGlobalRefType = 3 } jobjectRefType;
-
錯誤類型定義
#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 */
-
JNINativeInterface
struct JNINativeInterface; struct _JNIEnv; typedef _JNIEnv JNIEnv; typedef const struct JNINativeInterface* C_JNIEnv; typedef const struct JNINativeInterface* JNIEnv;
-
JNIInvokeInterface
struct JNIInvokeInterface_; struct JavaVM_; #ifdef __cplusplus typedef JavaVM_ JavaVM; #else typedef const struct JNIInvokeInterface_ *JavaVM;
-
裝載與卸載方法
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved); JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved);