Java Native Interface (JNI)標準是java平臺的一部分,它容許Java代碼和其餘語言寫的代碼進行交互。JNI 是本地編程接口,它使得在 Java 虛擬機 (VM) 內部運行的 Java 代碼可以與用其它編程語言(如 C、C++ 和彙編語言)編寫的應用程序和庫進行交互操做。html
因爲Android的應用層的類都是以Java寫的,這些Java類編譯爲Dex型式的Bytecode以後,必須靠Dalvik虛擬機(VM: Virtual Machine)來執行。VM在Android平臺裏,扮演很重要的角色。java
此外,在執行Java類的過程當中,若是Java類須要與C組件溝通時,VM就會去載入C組件,而後讓Java的函數順利地調用到C組件的函數。此時,VM扮演着橋樑的角色,讓Java與C組件能經過標準的JNI介面而相互溝通。android
應用層的Java類是在虛擬機(VM: Vitual Machine)上執行的,而C件不是在VM上執行,那麼Java程式又如何要求VM去載入(Load)所指定的C組件呢? 可以使用下述指令:編程
System.loadLibrary(*.so的檔案名); 數組
例如,Android框架裏所提供的MediaPlayer.java類,含指令:安全
public class MediaPlayer{ 框架
static {編程語言
System.loadLibrary("media_jni");ide
}函數
}
這要求VM去載入Android的/system/lib/libmedia_jni.so檔案。載入*.so以後,Java類與*.so檔案就匯合起來,一塊兒執行了。
---- JNI_OnLoad()與JNI_OnUnload()函數的用途
當Android的VM(Virtual Machine)執行到System.loadLibrary()函數時,首先會去執行C組件裏的JNI_OnLoad()函數。它的用途有二:
(1)告訴VM此C組件使用那一個JNI版本。若是你的*.so檔沒有提供JNI_OnLoad()函數,VM會默認該*.so檔是使用最老的JNI 1.1版本。因爲新版的JNI作了許多擴充,若是須要使用JNI的新版功能,例如JNI 1.4的java.nio.ByteBuffer,就必須藉由JNI_OnLoad()函數來告知VM。
(2)因爲VM執行到System.loadLibrary()函數時,就會當即先呼叫JNI_OnLoad(),因此C組件的開發者能夠藉由JNI_OnLoad()來進行C組件內的初期值之設定(Initialization)。
例如,在Android的/system/lib/libmedia_jni.so檔案裏,就提供了JNI_OnLoad()函數,其程式碼片斷爲:
//#defineLOG_NDEBUG 0 #define LOG_TAG"MediaPlayer-JNI" jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env = NULL; jint result = -1; if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { LOGE("ERROR: GetEnv failed\n"); goto bail; } assert(env != NULL); if (register_android_media_MediaPlayer(env) < 0) { LOGE("ERROR: MediaPlayer native registration failed\n"); goto bail; } if (register_android_media_MediaRecorder(env) < 0) { LOGE("ERROR: MediaRecorder native registration failed\n"); goto bail; } if (register_android_media_MediaScanner(env) < 0) { LOGE("ERROR: MediaScanner native registration failed\n"); goto bail; } if (register_android_media_MediaMetadataRetriever(env) < 0) { LOGE("ERROR: MediaMetadataRetriever native registrationfailed\n"); goto bail; } /* success -- return valid version number */ result = JNI_VERSION_1_4; bail: return result; }
此函數回傳JNI_VERSION_1_4值給VM,因而VM知道了其所使用的JNI版本了。此外,它也作了一些初期的動做(可呼叫任何本地函數),例如指令:
if(register_android_media_MediaPlayer(env) < 0) {
LOGE("ERROR: MediaPlayer native registration failed\n");
goto bail;
}
就將此組件提供的各個本地函數(NativeFunction)登記到VM裏,以便能加快後續呼叫本地函數的效率。
JNI_OnUnload()函數與JNI_OnLoad()相對應的。在載入C組件時會當即呼叫JNI_OnLoad()來進行組件內的初期動做;而當VM釋放該C組件時,則會呼叫JNI_OnUnload()函數來進行善後清除動做。當VM呼叫JNI_OnLoad()或JNI_Unload()函數時,都會將VM的指針(Pointer)傳遞給它們,其參數以下:
jint JNI_OnLoad(JavaVM* vm, void* reserved){ } jint JNI_OnUnload(JavaVM* vm, void*reserved){ } 在JNI_OnLoad()函數裏,就透過VM之指標而取得JNIEnv之指標值,並存入env指標變數裏,以下述指令: jint JNI_OnLoad(JavaVM* vm, void*reserved){ JNIEnv* env = NULL; jint result = -1; if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { LOGE("ERROR: GetEnv failed\n"); goto bail; } }
因爲VM一般是多執行緒(Multi-threading)的執行環境。每個執行緒在呼叫JNI_OnLoad()時,所傳遞進來的JNIEnv指標值都是不一樣的。爲了配合這種多執行緒的環境,C組件開發者在撰寫本地函數時,可藉由JNIEnv指標值之不一樣而避免執行緒的資料衝突問題,才能確保所寫的本地函數能安全地在Android的多執行緒VM裏安全地執行。基於這個理由,當在呼叫C組件的函數時,都會將JNIEnv指標值傳遞給它,以下:
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
if (register_android_media_MediaPlayer(env) < 0) {
}
}
這JNI_OnLoad()呼叫register_android_media_MediaPlayer(env)函數時,就將env指標值傳遞過去。如此,在register_android_media_MediaPlayer()函數就能藉由該指標值而區別不一樣的執行緒,以便化解資料衝突的問題。
例如,在register_android_media_MediaPlayer()函數裏,可撰寫下述指令:
if ((*env)->MonitorEnter(env, obj) != JNI_OK) {
}
查看是否已經有其餘執行緒進入此物件,若是沒有,此執行緒就進入該物件裏執行了。還有,也可撰寫下述指令:
if ((*env)->MonitorExit(env, obj) != JNI_OK) {
}
查看是否此執行緒正在此物件內執行,若是是,此執行緒就會當即離開。
應用層級的Java類別透過VM而呼叫到本地函數。通常是仰賴VM去尋找*.so裏的本地函數。若是須要連續呼叫不少次,每次都須要尋找一遍,會多花許多時間。此時,組件開發者能夠自行將本地函數向VM進行登記。例如,在Android的/system/lib/libmedia_jni.so檔案裏的代碼段以下:
//#define LOG_NDEBUG 0 #define LOG_TAG "MediaPlayer-JNI" static JNINativeMethod gMethods[] = { {"setDataSource", "(Ljava/lang/String;)V", (void *)android_media_MediaPlayer_setDataSource}, {"setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaPlayer_setDataSourceFD}, {"prepare", "()V", (void*)android_media_MediaPlayer_prepare}, {"prepareAsync", "()V", (void*)android_media_MediaPlayer_prepareAsync}, {"_start", "()V", (void*)android_media_MediaPlayer_start}, {"_stop", "()V", (void*)android_media_MediaPlayer_stop}, {"getVideoWidth", "()I", (void*)android_media_MediaPlayer_getVideoWidth}, {"getVideoHeight", "()I", (void*)android_media_MediaPlayer_getVideoHeight}, {"seekTo", "(I)V", (void*)android_media_MediaPlayer_seekTo}, {"_pause", "()V", (void*)android_media_MediaPlayer_pause}, {"isPlaying", "()Z", (void*)android_media_MediaPlayer_isPlaying}, {"getCurrentPosition", "()I", (void*)android_media_MediaPlayer_getCurrentPosition}, {"getDuration", "()I", (void*)android_media_MediaPlayer_getDuration}, {"_release", "()V", (void*)android_media_MediaPlayer_release}, {"_reset", "()V", (void*)android_media_MediaPlayer_reset}, {"setAudioStreamType","(I)V", (void*)android_media_MediaPlayer_setAudioStreamType}, {"setLooping", "(Z)V", (void*)android_media_MediaPlayer_setLooping}, {"setVolume", "(FF)V", (void*)android_media_MediaPlayer_setVolume}, {"getFrameAt", "(I)Landroid/graphics/Bitmap;", (void *)android_media_MediaPlayer_getFrameAt}, {"native_setup", "(Ljava/lang/Object;)V", (void *)android_media_MediaPlayer_native_setup}, {"native_finalize", "()V", (void*)android_media_MediaPlayer_native_finalize}, }; static intregister_android_media_MediaPlayer(JNIEnv *env){ return AndroidRuntime::registerNativeMethods(env, "android/media/MediaPlayer",gMethods, NELEM(gMethods)); } jint JNI_OnLoad(JavaVM* vm, void*reserved){ if (register_android_media_MediaPlayer(env) < 0) { LOGE("ERROR: MediaPlayer native registration failed\n"); goto bail; } }
當VM載入libmedia_jni.so檔案時,就呼叫JNI_OnLoad()函數。接着,JNI_OnLoad()呼叫register_android_media_MediaPlayer()函數。此時,就呼叫到AndroidRuntime::registerNativeMethods()函數,向VM(即AndroidRuntime)登記gMethods[]表格所含的本地函數了。簡而言之,registerNativeMethods()函數的用途有二:
(1)更有效率去找到函數。
(2)可在執行期間進行抽換。因爲gMethods[]是一個<名稱,函數指針>對照表,在程序執行時,可屢次呼叫registerNativeMethods()函數來更換本地函數之指針,而達到彈性抽換本地函數之目的。
其中很重要的區別是Andorid使用了一種Java 和 C 函數的映射表數組,並在其中描述了函數的參數和返回值。這個數組的類型是JNINativeMethod,定義以下:
typedef struct {
const char* name; /*Java中函數的名字*/
const char* signature; /*描述了函數的參數和返回值*/
void* fnPtr; /*函數指針,指向C函數*/
} JNINativeMethod;
其中比較難以理解的是第二個參數,例如
"()V"
"(II)V"
"(Ljava/lang/String;Ljava/lang/String;)V"
實際上這些字符是與函數的參數類型一一對應的。
"()" 中的字符表示參數,後面的則表明返回值。例如"()V"就表示voidFunc();
"(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"。
typedef union jvalue { jboolean z; jbyte b; jchar c; jshort s; jint i; jlong j; jfloat f; jdouble d; jobject l; } jvalue;
//建立一個jobject對象,jmethodID是方法id,能夠找到該對象的構造函數 jobject (*NewObject)(JNIEnv*, jclass, jmethodID, ...); //建立一個jstring對象 jstring (*NewString)(JNIEnv*, const jchar*, jsize); jstring (*NewStringUTF)(JNIEnv*, const char*); //下面都是建立一個數組,jsize類型是JNI提供的,它最終的類型仍是jint jobjectArray (*NewObjectArray)(JNIEnv*, jsize, jclass, jobject); jbooleanArray (*NewBooleanArray)(JNIEnv*, jsize); jbyteArray (*NewByteArray)(JNIEnv*, jsize); jcharArray (*NewCharArray)(JNIEnv*, jsize); jshortArray (*NewShortArray)(JNIEnv*, jsize); jintArray (*NewIntArray)(JNIEnv*, jsize); jlongArray (*NewLongArray)(JNIEnv*, jsize); jfloatArray (*NewFloatArray)(JNIEnv*, jsize); jdoubleArray (*NewDoubleArray)(JNIEnv*, jsize);
//獲取obj對象的字段名稱爲fieldName,字段類型爲fieldType的字段ID。好比(*env)->GetFieldID(env, personClass, "name","Ljava/lang/String;")。 jfieldID (*GetFieldID)(JNIEnv*, jclass obj, const char* fieldName, const char* fieldType); //獲取obj對象字段ID爲fieldId的值 jobject (*GetObjectField)(JNIEnv*, jobject obj, jfieldID fieldId); jboolean (*GetBooleanField)(JNIEnv*, jobject, jfieldID); jbyte (*GetByteField)(JNIEnv*, jobject, jfieldID); jchar (*GetCharField)(JNIEnv*, jobject, jfieldID); jshort (*GetShortField)(JNIEnv*, jobject, jfieldID); jint (*GetIntField)(JNIEnv*, jobject, jfieldID); jlong (*GetLongField)(JNIEnv*, jobject, jfieldID); jfloat (*GetFloatField)(JNIEnv*, jobject, jfieldID) __NDK_FPABI__; jdouble (*GetDoubleField)(JNIEnv*, jobject, jfieldID) __NDK_FPABI__; //給obj對象字段ID爲fieldId的屬性設值,好比(*env)->SetObjectField(env, mPerson, field_name, (*env)->NewStringUTF(env,"val native")); void (*SetObjectField)(JNIEnv*, jobject obj, jfieldID fieldId, jobject val); void (*SetBooleanField)(JNIEnv*, jobject, jfieldID, jboolean); void (*SetByteField)(JNIEnv*, jobject, jfieldID, jbyte); void (*SetCharField)(JNIEnv*, jobject, jfieldID, jchar); void (*SetShortField)(JNIEnv*, jobject, jfieldID, jshort); void (*SetIntField)(JNIEnv*, jobject, jfieldID, jint); void (*SetLongField)(JNIEnv*, jobject, jfieldID, jlong); void (*SetFloatField)(JNIEnv*, jobject, jfieldID, jfloat) __NDK_FPABI__; void (*SetDoubleField)(JNIEnv*, jobject, jfieldID, jdouble) __NDK_FPABI__; //下面靜態屬性。 jobject (*GetStaticObjectField)(JNIEnv*, jclass, jfieldID); jboolean (*GetStaticBooleanField)(JNIEnv*, jclass, jfieldID); jbyte (*GetStaticByteField)(JNIEnv*, jclass, jfieldID); jchar (*GetStaticCharField)(JNIEnv*, jclass, jfieldID); jshort (*GetStaticShortField)(JNIEnv*, jclass, jfieldID); jint (*GetStaticIntField)(JNIEnv*, jclass, jfieldID); jlong (*GetStaticLongField)(JNIEnv*, jclass, jfieldID); jfloat (*GetStaticFloatField)(JNIEnv*, jclass, jfieldID) __NDK_FPABI__; jdouble (*GetStaticDoubleField)(JNIEnv*, jclass, jfieldID) __NDK_FPABI__; void (*SetStaticObjectField)(JNIEnv*, jclass, jfieldID, jobject); void (*SetStaticBooleanField)(JNIEnv*, jclass, jfieldID, jboolean); void (*SetStaticByteField)(JNIEnv*, jclass, jfieldID, jbyte); void (*SetStaticCharField)(JNIEnv*, jclass, jfieldID, jchar); void (*SetStaticShortField)(JNIEnv*, jclass, jfieldID, jshort); void (*SetStaticIntField)(JNIEnv*, jclass, jfieldID, jint); void (*SetStaticLongField)(JNIEnv*, jclass, jfieldID, jlong); void (*SetStaticFloatField)(JNIEnv*, jclass, jfieldID, jfloat) __NDK_FPABI__; void (*SetStaticDoubleField)(JNIEnv*, jclass, jfieldID, jdouble) __NDK_FPABI__; //獲取數組長度 jboolean* (*GetBooleanArrayElements)(JNIEnv*, jbooleanArray, jboolean*); jbyte* (*GetByteArrayElements)(JNIEnv*, jbyteArray, jboolean*); jchar* (*GetCharArrayElements)(JNIEnv*, jcharArray, jboolean*); jshort* (*GetShortArrayElements)(JNIEnv*, jshortArray, jboolean*); jint* (*GetIntArrayElements)(JNIEnv*, jintArray, jboolean*); jlong* (*GetLongArrayElements)(JNIEnv*, jlongArray, jboolean*); jfloat* (*GetFloatArrayElements)(JNIEnv*, jfloatArray, jboolean*); jdouble* (*GetDoubleArrayElements)(JNIEnv*, jdoubleArray, jboolean*); //獲取方法ID jmethodID (*GetStaticMethodID)(JNIEnv*, jclass, const char*, const char*);
jobject (*CallObjectMethod)(JNIEnv*, jobject, jmethodID, ...); jobject (*CallObjectMethodV)(JNIEnv*, jobject, jmethodID, va_list); jobject (*CallObjectMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); jboolean (*CallBooleanMethod)(JNIEnv*, jobject, jmethodID, ...); jboolean (*CallBooleanMethodV)(JNIEnv*, jobject, jmethodID, va_list); jboolean (*CallBooleanMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); jbyte (*CallByteMethod)(JNIEnv*, jobject, jmethodID, ...); jbyte (*CallByteMethodV)(JNIEnv*, jobject, jmethodID, va_list); jbyte (*CallByteMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); jchar (*CallCharMethod)(JNIEnv*, jobject, jmethodID, ...); jchar (*CallCharMethodV)(JNIEnv*, jobject, jmethodID, va_list); jchar (*CallCharMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); jshort (*CallShortMethod)(JNIEnv*, jobject, jmethodID, ...); jshort (*CallShortMethodV)(JNIEnv*, jobject, jmethodID, va_list); jshort (*CallShortMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); jint (*CallIntMethod)(JNIEnv*, jobject, jmethodID, ...); jint (*CallIntMethodV)(JNIEnv*, jobject, jmethodID, va_list); jint (*CallIntMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); jlong (*CallLongMethod)(JNIEnv*, jobject, jmethodID, ...); jlong (*CallLongMethodV)(JNIEnv*, jobject, jmethodID, va_list); jlong (*CallLongMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); jfloat (*CallFloatMethod)(JNIEnv*, jobject, jmethodID, ...) __NDK_FPABI__; jfloat (*CallFloatMethodV)(JNIEnv*, jobject, jmethodID, va_list) __NDK_FPABI__; jfloat (*CallFloatMethodA)(JNIEnv*, jobject, jmethodID, jvalue*) __NDK_FPABI__; jdouble (*CallDoubleMethod)(JNIEnv*, jobject, jmethodID, ...) __NDK_FPABI__; jdouble (*CallDoubleMethodV)(JNIEnv*, jobject, jmethodID, va_list) __NDK_FPABI__; jdouble (*CallDoubleMethodA)(JNIEnv*, jobject, jmethodID, jvalue*) __NDK_FPABI__; void (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...); void (*CallVoidMethodV)(JNIEnv*, jobject, jmethodID, va_list); void (*CallVoidMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); jobject (*CallStaticObjectMethod)(JNIEnv*, jclass, jmethodID, ...); jobject (*CallStaticObjectMethodV)(JNIEnv*, jclass, jmethodID, va_list); jobject (*CallStaticObjectMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); jboolean (*CallStaticBooleanMethod)(JNIEnv*, jclass, jmethodID, ...); jboolean (*CallStaticBooleanMethodV)(JNIEnv*, jclass, jmethodID,
void ReleaseBooleanArrayElements(jbooleanArray array, jboolean* elems, jint mode) { functions->ReleaseBooleanArrayElements(this, array, elems, mode); } void ReleaseByteArrayElements(jbyteArray array, jbyte* elems, jint mode) { functions->ReleaseByteArrayElements(this, array, elems, mode); } void ReleaseCharArrayElements(jcharArray array, jchar* elems, jint mode) { functions->ReleaseCharArrayElements(this, array, elems, mode); } void ReleaseShortArrayElements(jshortArray array, jshort* elems, jint mode) { functions->ReleaseShortArrayElements(this, array, elems, mode); } void ReleaseIntArrayElements(jintArray array, jint* elems, jint mode) { functions->ReleaseIntArrayElements(this, array, elems, mode); } void ReleaseLongArrayElements(jlongArray array, jlong* elems, jint mode) { functions->ReleaseLongArrayElements(this, array, elems, mode); } void ReleaseFloatArrayElements(jfloatArray array, jfloat* elems, jint mode)