当前位置:网站首页>Registration method of native method in JNI
Registration method of native method in JNI
2022-06-25 00:21:00 【BY-91】
List of articles
JNI in native Methods registration
jni yes Java Native Interface Abbreviation ,Java Local interface , yes Java With other languages (native Language ) A bridge of communication
Native Method registration is divided into static registration ( be chiefly used in NDK Development ) And dynamic registration ( be chiefly used in framework Development )
native Method static registration
- AndroidStudio Create a new one in lib package , This example follows MediaRecorder.java give an example ,
public class MediaRecorder {
static {
System.loadLibrary("media_jni");
//Native Method static registration , According to the method name Java Methods and JNI Function to establish association
native_init();
}
private static native final void native_init();
public native void start() throws IllegalStateException;
}
- Enter the terminal to execute the command under the current package path :
javac com/by/jnilib/MediaRecorder.java
javah com.by.jnilib.MediaRecorder


- jni Method definitions in the document
/*
* Native Methods are statically registered. According to the method name Java Methods and JNI Function to establish association
* Class: com_by_jnilib_MediaRecorder
* Method: native_init
* Signature: ()V Java Method signature information because Java Method overload , ( Parameter type ) return type V representative void,
* Method signature reason : because Java Method overload , ( Parameter type ) return type V representative void,
* com_by_jnilib_MediaRecorder_native_1init Package name + Class name + Method name ,
* Java Of native_init The method contains _ Turn into jni The method will become _1
* JNIEnv * The pointer can be in native In the world of Java The code in , Only valid in the thread that created it , Cannot pass... Across threads
* jclass yes JNI Data type of , Corresponding Java Medium class class
*/
JNIEXPORT void JNICALL Java_com_by_jnilib_MediaRecorder_native_1init
(JNIEnv *, jclass);
native Method dynamic registration
Reference system MediaRecorder Registration method
JNI There is a structure for recording Java in native Methods and JNI The connection of methods , Namely JNINativeMethod
- Definition JNINativeMethod Type of gMethods Array , What is stored inside is MediaRecorder Of Native Methods and JNI The correspondence of layer functions .

- gMethods Array in register_android_media_MediaRecoder Method is called , This register_android_media_MediaRecoder The method is in JNI_OnLoad Call in method , And back to registerNativeMethods Method ,JNI_OnLoad The method is described above System.loadLibrary(“media_jni”) After the method is executed , also JNI_OnLoad The registration of the entire multimedia framework is invoked in the function. JNINativeMethod Array functions , Finally by registerNativeMethods Method returns jniRegisterNativeMethods Function RegisterNatives Function completion JNI Dynamic registration of . So the order is :
System.loadLibrary ——> JNI_OnLoad ——> register_android_media_MediaRecoder——> return registerNativeMethods ——> return jniRegisterNativeMethods function ——> call RegisterNatives(…,gMethods,…) Function complete registration .


Data type conversion


Method signature :
because Java Method overload , That is, the method name is the same , Parameters and returns are different ;JNI You can't find it just by the method name Java The corresponding method in
/*
* Class: com_by_jnilib_MediaRecorder
* Method: start
* Signature: ()V Java Method signature information ( Parameter type ) return type V representative void
*/
Use javap Automatic generation of method signature
public class MediaRecorder {
static {
System.loadLibrary("media_jni");
//Native Method static registration , According to the method name Java Methods and JNI Function to establish association
native_init();
}
private static native final void native_init();
public native void start() throws IllegalStateException;
//javap Command test auto generate method signature
private native final void native_setup(Object media_recorder_this,
String clientName,String opPackageName) throws IllegalStateException;
}
javac -encoding UTF-8 MediaRecorder.java
javap -s -p MediaRecorder.class
-s Represents the output internal type signature ,p Indicates that all methods and members are printed ( The default is public member )
JNIEnv
JNIEnv yes Native In the world Java The representative of the environment , adopt JNIEnv * The pointer can be in native Visit... In the world Java The code in ( call Java Method , operation Java Variables and objects in ) To operate , Only valid in the thread that created it , Cannot pass... Across threads , Different JNIEnv It's independent of each other .
PS: There is only one virtual machine process JavaVM
边栏推荐
- Related operations of ansible and Playbook
- Simple collation of Web cache
- ArcGIS loads free online historical images as the base map (no plug-ins are required)
- JDBC —— 数据库连接
- Tape SVG animation JS effect
- 融合模型权限管理设计方案
- 干接点和湿接点
- Reservoir dam safety monitoring
- How can I persuade leaders to use DDD to construct the liver project?
- Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
猜你喜欢

The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply

Related operations of ansible and Playbook

∞ symbol line animation canvasjs special effect
@mysql

技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放

Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model

Reservoir dam safety monitoring

ArcGIS loads free online historical images as the base map (no plug-ins are required)

时间统一系统

人体改造 VS 数字化身
随机推荐
Dynamic effect of canvas lines
[proteus simulation] example of using timer 0 as a 16 bit counter
JPA learning 2 - core annotation, annotation addition, deletion, modification and query, list query result return type, one to many, many to one, many to many
∞ symbol line animation canvasjs special effect
im即时通讯开发应用保活之进程防杀
Requests Library
Ott marketing is booming. How should businesses invest?
技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放
【Proteus仿真】定时器0作为16位计数器使用示例
vim使用命令
C program design topic 15-16 final exam exercise solutions (Part 1)
Overview of medium and low speed aerospace electronic bus
The drawableleft of the custom textview in kotlin is displayed in the center together with the text
Signal integrity (SI) power integrity (PI) learning notes (XXV) differential pair and differential impedance (V)
The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply
Collection of software testing and game testing articles
Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
Unimportant tokens can be stopped in advance! NVIDIA proposes an efficient visual transformer network a-vit with adaptive token to improve the throughput of the model
5G dtu无线通信模块的电力应用
VIM use command
