当前位置:网站首页>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
边栏推荐
- Ansible及playbook的相关操作
- After 5 years of software testing in didi and ByteDance, it's too real
- Domain Driven Design and coding
- 干接点和湿接点
- [Solved] Public key for mysql-community-xxx. rpm is not installed
- Ott marketing is booming. How should businesses invest?
- Some examples of MgO operating database in go
- In the past 5 years, from "Diandian" to the current test development, my success is worth learning from.
- Scrollview height cannot fill full screen
- How to use promise Race() and promise any() ?
猜你喜欢

Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download

Svg+js keyboard control path

有趣的checkbox计数器

Collective example

Why do more and more physical stores use VR panorama? What are the advantages?

传输层 以字节为单位的滑动窗口技术

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

Zed acquisition

What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis

Discrete mathematics and its application detailed explanation of exercises in the final exam of spring and summer semester of 2018-2019 academic year
随机推荐
Analysis report on operation pattern and supply and demand situation of global and Chinese cyano ketoprofen industry from 2022 to 2028
水库大坝安全监测
微搭低代码中实现增删改查
Is it so difficult to calculate the REM size of the web page according to the design draft?
Encryption and encoding resolution
Fuxin Kunpeng joins in, and dragon lizard community welcomes a new partner in format document technical service
[issue 25] face to face experience of golang Engineer in the rightmost social recruitment
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
Current situation analysis and development trend prediction report of hesperidase industry in the world and China from 2022 to 2028
【Proteus仿真】定时器0作为16位计数器使用示例
A small program written this week
C WinForm maximizes occlusion of the taskbar and full screen display
Dynamic effect of canvas lines
Time unified system
Adding, deleting, modifying and checking in low build code
Domain Driven Design and coding
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency
Meta & Berkeley proposed a universal multi-scale visual transformer based on pooled self attention mechanism. The classification accuracy in Imagenet reached 88.8%! Open source
