1 什麼是JNI
JNI是Java Native Interface的縮寫,即Java本地接口.從Java1.1開始,JNI標准成為Java平台的一部分,它允許java代碼和用其它語言編寫的代碼進行交互.JNI是本地編程接口,它使得在Java虛擬機(VM)內部運行的Java代碼能夠與用其他編程語言(如C,C++和匯編語言)的應用程序和庫進行交互操作.
在Android中提供的JNI的方式,讓Java程序可以調用C語言程序。Android中很多Java類都具有native接口,這些native接口就是同本地實現,然後注冊到系統中的.
JNI在Android層次結構中的作用如下圖所示:
在Android中,主要的JNI代碼在以下的路徑中:
Android源碼根目錄/frameworks/base/core/jni/
這個路徑中的內容將被編譯成庫libandroid_runtime.so,這就是一個普通的動態庫,被放置在目標系統的/system/lib目錄中.
除此之外,Android還包含其他的JNI庫,例如,媒體部分的JNI目錄frameworks/base/media/jni/中,被編譯成庫libmedia_jni.so.
JNI中的各個文件實際上就是C++的普通文件,其命名一般和支持的Java類有對應關系。這種關系是習慣上的寫法,而不是強制的。
在Android中實現的JNI庫,需要連接動態庫libnativehelper.so.
2 注冊JNI方法
在Android源碼根目錄/frameworks/base/services/jni/目錄下有一個onload.cpp文件,其內容如下:
- /*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- #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);
- };
-
- 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);
-
- return JNI_VERSION_1_4;
- }
onload.cpp文件上部分為注冊函數的聲明,下部分為調用各種注冊函數,而這些注冊函數就是JNI方法的注冊函數! 正有通過這些注冊函數,上層才有可能調用注冊的JNI方法.
這些注冊函數是由同目錄下的其他.cpp文件中實現,如上面的register_android_server_HelloService(env)這個函數是在com_android_service_HelloService.cpp文件中實現的.那麼編譯器又是如何知道這點的呢? 答案當然是Android.mk這個文件,打開這個文件,其內容如下:
- LOCAL_PATH:= $(call my-dir)
- include $(CLEAR_VARS)
-
- LOCAL_SRC_FILES:= \
- com_android_server_AlarmManagerService.cpp \
- com_android_server_BatteryService.cpp \
- com_android_server_InputApplicationHandle.cpp \
- com_android_server_InputManager.cpp \
- com_android_server_InputWindowHandle.cpp \
- com_android_server_LightsService.cpp \
- com_android_server_PowerManagerService.cpp \
- com_android_server_SystemServer.cpp \
- com_android_server_UsbDeviceManager.cpp \
- com_android_server_UsbHostManager.cpp \
- com_android_server_VibratorService.cpp \
- com_android_server_location_GpsLocationProvider.cpp \
- com_android_server_connectivity_Vpn.cpp \
- com_android_server_HelloService.cpp \
- onload.cpp
-
- LOCAL_C_INCLUDES += \
- $(JNI_H_INCLUDE) \
- frameworks/base/services \
- frameworks/base/core/jni \
- external/skia/include/core
-
- LOCAL_SHARED_LIBRARIES := \
- libandroid_runtime \
- libcutils \
- libhardware \
- libhardware_legacy \
- libnativehelper \
- libsystem_server \
- libutils \
- libui \
- libinput \
- libskia \
- libgui \
- libusbhost
-
- ifeq ($(WITH_MALLOC_LEAK_CHECK),true)
- LOCAL_CFLAGS += -DMALLOC_LEAK_CHECK
- endif
-
- LOCAL_MODULE:= libandroid_servers
-
- include $(BUILD_SHARED_LIBRARY)
在LOCAL_SRC_FILE中給出了所有實現文件(cpp文件)的路徑,因此編譯就能找到各個注冊函數對應的實現文件了.
接下來讓我們來看看其中一個注冊函數的具體實現過程是如何的,比如:register_android_server_HelloService(env),打開com_android_service_HelloService.cpp文件,其下有注冊函數的實現代碼,如下:
- int register_android_server_HelloService(JNIEnv *env) {
- return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));
- }
其中jniRegisterNativeMethods為注冊JNI方法函數,此函數在JNI方法使用中非常重要,此函數的第二個參數為對應著java類即HelloService.java的文件名,第三個參數為注冊的方法表:
- /*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},
- };