当前位置:网站首页>Static registration and dynamic registration of JNI
Static registration and dynamic registration of JNI
2022-06-26 17:53:00 【Novice Xiaowang】
Static registration
according to JNI Find according to the standard naming rules , This method is called static registration
JNI Writing function name Java_ Definition native Full path of method _ Method name
extern "C" JNIEXPORT jint JNICALL Java_com_example_opentr069_OpenTR069Native_getNatDetected(JNIEnv * env,
jclass clazz){
return sk_get_nat_stun_flag();
}Dynamic registration
call JNI Provided RegisterNatives function , Register local functions with JVM in , This method is called dynamic registration .
When the library is loaded, it will automatically call JNI_OnLoad() function , Developers often JNI_OnLoad() Function to do some initialization , Dynamic registration is done here . call API yes env->RegisterNatives(clazz, gMethods, numMethods).
env->RegisterNatives(clazz, gMethods, numMethods) Is a function that accepts three parameters , The first parameter is Java The corresponding class , The second parameter is JNINativeMethod Array , The third parameter is JNINativeMethod Length of array , That is, the number of methods that need to be registered .
among JNINativeMethod It represents the mapping relationship of methods , It includes Java Method name in , Corresponding method signature and Native Mapped function method .
Compared with static registration , Dynamic registration is more flexible , If it changes java native Package name or class name of the class where the function is located , Adjust only Java native Function signature information .
// Automatically call... When the class library is loaded
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reversed)
{
JNIEnv *env = NULL;
// initialization JNIEnv
if(vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK){
return JNI_FALSE;
}
// Find the one that needs dynamic registration Jni class
jclass jniClass = env->FindClass("com/fly/jnitest/MainActivity");
if(nullptr == jniClass){
return JNI_FALSE;
}
// Dynamic registration
env->RegisterNatives(jniClass,nativeMethod,sizeof(JNINativeMethod)/sizeof(nativeMethod));
// return JNI Version used
return JNI_VERSION_1_6;
}
jstring stringFromJNI(JNIEnv *jniEnv,jobject jobj){
return jniEnv->NewStringUTF("hello from C++ string");
}
static const JNINativeMethod nativeMethod[] = {
// Java Function name in
{"stringFromJNI",
// Function signature information
"()Ljava/lang/String;",
// native Function pointer of
(void *) (stringFromJNI)
}
};JNI Property description
JNI The attribute descriptor, that is, the variable type, is in JNI The representation in , It is determined by the declaration type of the attribute . For example, using "I" Express int attribute , Use "F" Express float attribute , Use "D" Express double attribute , Use "Z" Express boolean Properties, etc .
For descriptors that refer to various types of attributes , such as java.lang.String, Need to be written in letters "L" start , Analytically, it is JNI Class descriptor and end with a semicolon ,Java In the complete class name ".“ By ”/" Replaced . therefore , about java.lang.String Type requires the following form of property descriptor :
Ljava/lang/String;
The descriptor of array type consists of "[" And the descriptor of array element type , for example ,[I Represents the property descriptor of an integer array
Function Descriptor
Usually we call it function signature , A function descriptor consists of its parameter type and return value type , The parameter type comes first , And use a pair of parentheses , Parameter types are listed in the order they appear in the function declaration , There is no separator between multiple parameter types , If a method has no parameters , Use a pair of empty parentheses to indicate . The return value type of the function is immediately followed by the closing bracket of the wrapped parameter type .
for example (I)V Refers to a function that receives an integer parameter and returns an empty value .()D It means that there are no input parameters , The return value is one double Function of type .
Be careful : Don't be C Functions like "int f(void)“ Such function prototypes are misleading , Misconception ”(V)I" Is its method descriptor , Actually "()I" Talent function f The function descriptor of
边栏推荐
- MySQL index
- LeetCode——226. Flip binary tree (BFS)
- RSA概念详解及工具推荐大全 - lmn
- map和filter方法对于稀缺数组的处理
- sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)
- Analysis of deep security definition and encryption technology
- Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
- Use middleware to record slow laravel requests
- 类型多样的石膏PBR多通道贴图素材,速来收藏!
- 并发之Synchronized说明
猜你喜欢

ACL 2022 | zero sample multilingual extracted text summarization based on neural label search
![[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure](/img/26/da00e70d0955bcdef3362474bc5fc7.png)
[uniapp] the uniapp mobile terminal uses uni Troubleshooting of navigateback failure

sparksql如何通过日期返回具体周几-dayofweek函数

sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)

RSA概念详解及工具推荐大全 - lmn

Number of solutions for knapsack problem

Applet setting button sharing function

Play with Linux and easily install and configure MySQL
![[recommendation system learning] technology stack of recommendation system](/img/ff/afc6f4b0997cfcb9e01ffbebf2a872.png)
[recommendation system learning] technology stack of recommendation system

二分查找法-1
随机推荐
17.13 supplementary knowledge, thread pool discussion, quantity discussion and summary
Halcon's region: features of multiple regions (5)
[npoi] C copy sheet template across workbooks to export Excel
ZCMU--1367: Data Structure
Preparing for the Blue Bridge Cup and ccf-csp
股票开账户如何优惠开户?现在在线开户安全么?
二分查找-2
wechat_ Solve the problem of page Jump and parameter transfer by navigator in wechat applet
The high concurrency system is easy to play, and Alibaba's new 100 million level concurrent design quick notes are really fragrant
Tencent qianzhiming: Exploration and application of pre training methods in information flow business
Discussion and generation of digital signature and analysis of its advantages
在国金证券开户怎么样?保障安全吗?
Play with Linux and easily install and configure MySQL
The difference between round and truncate in SQL (round or truncate)
How about opening an account at Guojin securities? Is it safe?
腾讯钱智明:信息流业务中的预训练方法探索与应用实践
Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
Using redis for user access data statistics hyperloglog and bitmap advanced data types
[recommendation system learning] technology stack of recommendation system
VSCode使用 - Remote-SSH 配置说明