In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how to understand the jni- registration native function of the android platform, the content is very detailed, interested friends can refer to, hope to be helpful to you.
There are two ways to register native functions: static registration and dynamic registration.
1. The static registration method finds the corresponding JNI function according to the function name: when the Java layer calls the function, it looks for the function from the corresponding JNI. If there is no function, it will report an error. If it exists, an association will be established, and this function will be directly used when it is called later, and this part of the operation will be done by the virtual machine. The static method traverses the association between the java and jni functions based on the function name, and requires that the name of the jni layer function must follow a specific format. Its disadvantage is:
1) the jni layer functions generated by javah are very long
2) when calling the native function for the first time, it is necessary to search the corresponding jni layer function according to the name to establish the association, which affects the efficiency.
2, dynamic registration method JNI allows you to provide a function mapping table, registered to the Jave virtual machine, so that Jvm can use the function mapping table to call the corresponding function, you do not have to use the function name to find the function you need to call. Java and JNI establish a relationship through the structure of JNINativeMethod, which is defined in jni.h, and its structure is as follows: typedef struct {const char* name; const char* signature; void* fnPtr;} JNINativeMethod; the first variable name is the name of the function in Java. The second variable, signature, uses a string to describe the parameters and return values of the function. The third variable, fnPtr, is the function pointer, pointing to the C function. When java loads the JNI dynamic library through System.loadLibrary, it then looks for a function of JNI_OnLoad and, if any, calls it, and dynamic registration is done here. 1) the JNI_OnLoad () function JNI_OnLoad () function is called when VM executes the System.loadLibrary (xxx) function. It has two important functions: specify the JNI version: tell VM which JNI version the component uses (if the JNI_OnLoad () function is not provided, VM will default to the oldest JNI version 1.1). If you want to use a new version of JNI, such as JNI version 1.4 Then the JNI_OnLoad () function must return the constant JNI_VERSION_1_4, which is defined in jni.h, to tell VM. Initialization setting, when VM executes the System.loadLibrary () function, it will immediately call the JNI_OnLoad () method, so it is most appropriate to initialize various resources in this method. 2) RegisterNatives RegisterNatives defines syntax: jint RegisterNatives (jclass clazz, const JNINativeMethod* methods,jint nMethods) in AndroidRunTime.
3. Add the custom native function JNI to android and its function in Android hierarchy
In Android, the main JNI code is in the following path: the contents of the Android source root directory / frameworks/base/core/jni/ will be compiled into the library libandroid_runtime.so, which is a normal dynamic library that is placed in the target system's / system/lib directory. In addition, Android also contains other JNI libraries, such as the JNI directory frameworks/base/media/jni/ in the media section, which is compiled into the library libmedia_jni.so. The files in JNI are actually ordinary files for C++, and their names generally correspond to the supported Java classes. This relationship is customary, not mandatory. 1) the registration JNI method has an onload.cpp file in the Android source root directory / frameworks/base/services/jni/ directory, adding the jni function declaration and the jni function registration method # 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)
/ / add custom jni function declaration here}
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 method registration
Return JNI_VERSION_1_4
}
The upper part of the onload.cpp file is the declaration of the registration function, and the lower part is to call various registration functions, and these registration functions are the registration functions of the JNI method! It is through these registration functions that the upper layer can call the registered JNI method. Take register_android_server_HelloService as an example to see how a registration function is implemented. Open the com_android_service_HelloService.cpp file 2) add the implementation code of the registration function as follows: int register_android_server_HelloService (JNIEnv * env) {
Return jniRegisterNativeMethods
(env, "com/android/server/HelloService", method_table, NELEM (method_table))
}
# of which jniRegisterNativeMethods
To register the JNI method function, # the second parameter of this function is the file name corresponding to the java class, that is, HelloService.java, and the third parameter is the registered method table 3) add the jni method table
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) the implementation code static void hello_setVal (JNIEnv* env, jobject clazz, jint value) {of each interface in the method table
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
}
The complete code is as follows: 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))
}
}
On how to understand the android platform jni- registration native function to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.