如何理解android平臺的jni—注冊native函數(shù)-創(chuàng)新互聯(lián)

這篇文章給大家介紹如何理解android平臺的jni—注冊native函數(shù),內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

成都創(chuàng)新互聯(lián)公司從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元米易做網(wǎng)站,已為上家服務(wù),為米易各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108

注冊native函數(shù)有兩種方法:靜態(tài)注冊和動態(tài)注冊。

1、靜態(tài)注冊方法根據(jù)函數(shù)名找到對應(yīng)的JNI函數(shù):Java層調(diào)用函數(shù)時,會從對應(yīng)的JNI中尋找該函數(shù),如果沒有就會報錯,如果存在則會建立一個關(guān)聯(lián)聯(lián)系,以后在調(diào)用時會直接使用這個函數(shù),這部分的操作由虛擬機完成。 靜態(tài)方法就是根據(jù)函數(shù)名來遍歷java和jni函數(shù)之間的關(guān)聯(lián),而且要求jni層函數(shù)的名字必須遵循 特定的格式,其缺點在于:

 1)javah生成的jni層函數(shù)特別長;

 2)初次調(diào)用native函數(shù)時要根據(jù)名字搜索對應(yīng)的jni層函數(shù)來建立關(guān)聯(lián)聯(lián)系,這樣影響效率。

2、動態(tài)注冊方法JNI 允許你提供一個函數(shù)映射表,注冊給Jave虛擬機,這樣Jvm就可以用函數(shù)映射表來調(diào)用相應(yīng)的函數(shù), 就可以不必通過函數(shù)名來查找需要調(diào)用的函數(shù)了。 Java與JNI通過JNINativeMethod的結(jié)構(gòu)來建立聯(lián)系,它在jni.h中被定義,其結(jié)構(gòu)內(nèi)容如下: typedef struct { const char* name; const char* signature; void* fnPtr; } JNINativeMethod; 第一個變量name是Java中函數(shù)的名字。 第二個變量signature,用字符串是描述了函數(shù)的參數(shù)和返回值 第三個變量fnPtr是函數(shù)指針,指向C函數(shù)。 當java通過System.loadLibrary加載完JNI動態(tài)庫后,緊接著會查找一個JNI_OnLoad的函數(shù),如果有,就調(diào)用它, 而動態(tài)注冊的工作就是在這里完成的。 1)JNI_OnLoad()函數(shù) JNI_OnLoad()函數(shù)在VM執(zhí)行System.loadLibrary(xxx)函數(shù)時被調(diào)用,它有兩個重要的作用: 指定JNI版本:告訴VM該組件使用那一個JNI版本(若未提供JNI_OnLoad()函數(shù),VM會默認該使用最老的JNI 1.1版),如果要使用新版本的JNI, 例如JNI 1.4版,則必須由JNI_OnLoad()函數(shù)返回常量JNI_VERSION_1_4(該常量定義在jni.h中) 來告知VM。 初始化設(shè)定,當VM執(zhí)行到System.loadLibrary()函數(shù)時,會立即先呼叫JNI_OnLoad()方法,因此在該方法中進行各種資源的初始化操作最為恰當, 2)RegisterNatives RegisterNatives在AndroidRunTime里定義 syntax: jint RegisterNatives(jclass clazz, const JNINativeMethod* methods,jint nMethods)

3、在android中加入自定義的native函數(shù)JNI在Android層次結(jié)構(gòu)中的作用


在Android中,主要的JNI代碼在以下的路徑中: Android源碼根目錄/frameworks/base/core/jni/ 這個路徑中的內(nèi)容將被編譯成庫libandroid_runtime.so,這就是一個普通的動態(tài)庫,被放置在目標系統(tǒng)的/system/lib目錄中.除此之外,Android還包含其他的JNI庫,例如,媒體部分的JNI目錄frameworks/base/media/jni/中,被編譯成庫libmedia_jni.so. JNI中的各個文件實際上就是C++的普通文件,其命名一般和支持的Java類有對應(yīng)關(guān)系。 這種關(guān)系是習(xí)慣上的寫法,而不是強制的。 1)注冊JNI方法 在Android源碼根目錄/frameworks/base/services/jni/目錄下有一個onload.cpp文件,加入 jni函數(shù)申明和jni函數(shù)注冊方法 #include "JNIHelp.h"

#include "jni.h"

#include "utils/Log.h"

#include "utils/misc.h"

namespace android {

int register_android_server_AlarmManagerService(JNIEnv* env);

int register_android_server_BatteryService(JNIEnv* env);

int register_android_server_InputApplicationHandle(JNIEnv* env);

int register_android_server_InputWindowHandle(JNIEnv* env);

int register_android_server_InputManager(JNIEnv* env);

int register_android_server_LightsService(JNIEnv* env);

int register_android_server_PowerManagerService(JNIEnv* env);

int register_android_server_UsbDeviceManager(JNIEnv* env);

int register_android_server_UsbHostManager(JNIEnv* env);

int register_android_server_VibratorService(JNIEnv* env);

int register_android_server_SystemServer(JNIEnv* env);

int register_android_server_location_GpsLocationProvider(JNIEnv* env);

int register_android_server_connectivity_Vpn(JNIEnv* env);

int register_android_server_HelloService(JNIEnv *env);

//此處加入自定義jni函數(shù)申明};

using namespace android;

extern "C" 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("GetEnv failed!");

 return result;

 }

 LOG_ASSERT(env, "Could not retrieve the env!");

 register_android_server_PowerManagerService(env);

 register_android_server_InputApplicationHandle(env);

 register_android_server_InputWindowHandle(env);

 register_android_server_InputManager(env);

 register_android_server_LightsService(env);

 register_android_server_AlarmManagerService(env);

 register_android_server_BatteryService(env);

 register_android_server_UsbDeviceManager(env);

 register_android_server_UsbHostManager(env);

 register_android_server_VibratorService(env);

 register_android_server_SystemServer(env);

 register_android_server_location_GpsLocationProvider(env);

 register_android_server_connectivity_Vpn(env);

 register_android_server_HelloService(env); //jni方法注冊

 return JNI_VERSION_1_4;

}

onload.cpp文件上部分為注冊函數(shù)的聲明,下部分為調(diào)用各種注冊函數(shù),而這些注冊函數(shù)就是JNI方法的注冊函數(shù)! 正是通過這些注冊函數(shù),上層才能調(diào)用注冊的JNI方法. 以register_android_server_HelloService為例,來看一個注冊函數(shù)的具體實現(xiàn)過程是如何的。 打開com_android_service_HelloService.cpp文件 2)加入注冊函數(shù)的實現(xiàn)代碼,如下: int register_android_server_HelloService(JNIEnv *env) {

 return jniRegisterNativeMethods

(env, "com/android/server/HelloService", method_table, NELEM(method_table));

}

#其中jniRegisterNativeMethods

為注冊JNI方法函數(shù),#此函數(shù)的第二個參數(shù)為對應(yīng)著java類即HelloService.java的文件名,第三個參數(shù)為注冊的方法表 3)加入jni方法表

static const JNINativeMethod method_table[] = {

 {"init_native", "()Z", (void*)hello_init},

 {"setVal_native", "(I)V", (void*)hello_setVal},

 {"getVal_native", "()I", (void*)hello_getVal},

};

4)方法表內(nèi)各個接口的實現(xiàn)代碼 static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {

val = value;

 LOGI("Hello JNI: set value %d to device.", val);

}

static jint hello_getVal(JNIEnv* env, jobject clazz) {

 LOGI("Hello JNI: get value %d from device.", val);

 return val;

 }

static jboolean hello_init(JNIEnv* env, jclass clazz) {

 LOGI("Hello JNI: initializing......");

 return -1;

 }

完整代碼如下: namespace android {

 int val;

 static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {

val = value;

 LOGI("Hello JNI: set value %d to device.", val);

 }

 static jint hello_getVal(JNIEnv* env, jobject clazz) {

 LOGI("Hello JNI: get value %d from device.", val);

 return val;

 }

static jboolean hello_init(JNIEnv* env, jclass clazz) {

 LOGI("Hello JNI: initializing......");

 return -1;

 }

 static const JNINativeMethod method_table[] = {

 {"init_native", "()Z", (void*)hello_init},

 {"setVal_native", "(I)V", (void*)hello_setVal},

 {"getVal_native", "()I", (void*)hello_getVal},

};

int register_android_server_HelloService(JNIEnv *env) {

 return jniRegisterNativeMethods

(env, "com/android/server/HelloService", method_table, NELEM(method_table));

}

}



關(guān)于如何理解android平臺的jni—注冊native函數(shù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享標題:如何理解android平臺的jni—注冊native函數(shù)-創(chuàng)新互聯(lián)
文章路徑:http://bm7419.com/article32/hcesc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計公司、電子商務(wù)、網(wǎng)站排名面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作