当前位置:网站首页>NDK beginner's study (1)
NDK beginner's study (1)
2022-07-07 14:11:00 【v_ three billion four hundred and eighty-three million six hund】
NDK Knowledge
1. Static registration .
Why should there be static registration , Because java Layer and the so The functions of the layer need to be mapped one by one , You can't get it wrong . Like the following .
public native String stringFromJNI();
How does that correspond .
Is the name of the function through jump , Very regular
Java_com_example_demonndk_MainActivity_stringFromJNI
yes java, Then the package name , Then the class name , Then the name . This is static registration .
JNIEnv env: Like jni Environment , It can be used to call c Layer of java. Or is it java Data type to c Data type of ,c Data to java Data conversion of data type .
The first parameter ,jobject: Like this The pointer , Know which place called so This function of layer
The second parameter ,jclass and jobject The distinction between : What should be declared Native When the method is static , Corresponding parameter jclass
, Because static methods don't rely on object instances , It depends on the class , So the parameter passed is a jclass
type . contrary , If it says Native Method time is not static method time , So the corresponding parameter is jobject
.
extern "C" JNIEXPORT jstring JNICALL
Tell me so c By
The static registration function must be an export function JNIEXPORT
LOGD Output Head introduction #include <android/log.h>
You can use __android_log_print(3,“moting”,__VA_ARGS__)
了 ;
Simplified can be logd
You can use it directly
logd(“hello”);
Create thread
pthread_t pthread;
//int pthread_create(pthread_t* __pthread_ptr, pthread_attr_t const* __attr, void* (*__start_routine)(void*), void*);
pthread_create(&pthread, nullptr,myThread, nullptr);
logd ("+++++++");
First there is a thread variable , Then create the thread .
Build a function above
void* myThread(void* a){
for (int i = 0; i <9; ++i) {
logd("myThread%d",i);
}
pthread_exit(0);
}
added pthread_join(pthread, nullptr)
Is to execute in the thread first , Then execute the following code .
Send a number into the thread
pthread_t pthread;
int num=4;
//int pthread_create(pthread_t* __pthread_ptr, pthread_attr_t const* __attr, void* (*__start_routine)(void*), void*);
pthread_create(&pthread, nullptr, myThread, &num);
pthread_join(pthread, nullptr);
logd ("===============");
Pass it in with a pointer
void* myThread(void* a){
int *num= static_cast<int *>(a);
for (int i = 0; i <*num; ++i) {
logd("myThread%d",i);
}
pthread_exit(0);
}
Pass a structure into the thread
Create a thread
struct moting{
std::string name;
int age;
bool sex;
};
moting aaa;
aaa.name="moting";
aaa.age=3;
aaa.sex= true;
pthread_t pthread;
//int pthread_create(pthread_t* __pthread_ptr, pthread_attr_t const* __attr, void* (*__start_routine)(void*), void*);
pthread_create(&pthread, nullptr, myThread, &aaa);
pthread_join(pthread, nullptr);
logd ("===============");
Then pass the pointer .
c Language return values are rarely used ,
so Execution timing of various functions in
init -》 init_array-》 jni_onload
jnionload Rewriting of
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved){
JNIEnv *env= nullptr;
if (vm->GetEnv((void **)&env,JNI_VERSION_1_6)!=JNI_OK){
logd("getenv filed");
}
logd("JNI_Onload%p",env);
return JNI_VERSION_1_6;
}
One so There can be no JNI_ONLOAD, For example, create a new thread . Must return JNI Version of
JNIENV and JNIVm The difference between
JNIENV and JNIVm, Defines a global variable JavaVM* globalVM;
How to use env call vm, The first argument to the function is env
JavaVM *vm1;
env->GetJavaVM(&vm1);
globalVm=vm1;
vm1 It's worth it
perhaps JNI_OnLoad The first parameter of
- vm Get env
In the main route
if (vm->GetEnv((void **)&env,JNI_VERSION_1_6)!=JNI_OK){
logd("getenv filed");
}
In the sub thread
边栏推荐
- 2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
- 2022-7-6 Leetcode 977.有序数组的平方
- 2022-7-6 Leetcode 977. Square of ordered array
- 最长上升子序列模型 AcWing 482. 合唱队形
- Excellent open source system recommendation of ThinkPHP framework
- LeetCode简单题分享(20)
- 2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
- ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
- FCOS3D label assignment
- MySQL "invalid use of null value" solution
猜你喜欢
docker部署oracle
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
【立体匹配论文阅读】【三】INTS
Help tenants
Redis 核心数据结构 & Redis 6 新特性详
2022-7-7 Leetcode 844.比较含退格的字符串
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
UML 状态图
随机推荐
请问,PTS对数据库压测有好方案么?
The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
Huawei image address
Xshell connection server changes key login to password login
Dry goods | summarize the linkage use of those vulnerability tools
Common response status codes
When FC connects to the database, do you have to use a custom domain name to access it outside?
Vmware共享主机的有线网络IP地址
[daily training -- Tencent select 50] 231 Power of 2
Cesium 已知一点经纬度和距离求另一个点的经纬度
参数关键字Final,Flags,Internal,映射关键字Internal
内存溢出和内存泄漏的区别
Es log error appreciation -limit of total fields
最长上升子序列模型 AcWing 482. 合唱队形
Environment configuration of lavarel env
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
FCOS3D label assignment
Leecode3. Longest substring without repeated characters
Excuse me, I have three partitions in Kafka, and the flinksql task has written the join operation. How can I give the join operation alone
libSGM的horizontal_path_aggregation程序解读