当前位置:网站首页>ndk初学习(一)
ndk初学习(一)
2022-07-07 12:10:00 【v_3483608762】
NDK的知识
1. 静态注册 。
为什么要有静态注册呢,是因为java层和so层的函数需要一一对应,不会找错了。比如下面的。
public native String stringFromJNI();
那是怎么样对应起来的呢。
是通过跳转的函数名字,非常的有规律
Java_com_example_demonndk_MainActivity_stringFromJNI
是java,然后包名,然后类名,然后才是名字。这就是静态注册。
JNIEnv env:像是jni的环境 ,可以用它来调用c层的java。或者是java的数据类型到c的数据类型,c数据到java数据类型的数据转换。
第一个参数,jobject:像是this指针,知道是那个地方调用了so层的这个函数
第二个参数,jclass和jobject的区分:当所声明Native方法是静态方法时,对应参数jclass
,因为静态方法不依赖对象实例,而依赖于类,所以参数中传递的是一个jclass
类型。相反,如果声明的Native方法时非静态方法时,那么对应参数是jobject
。
extern "C" JNIEXPORT jstring JNICALL
告诉是以c的方式进行
静态注册函数必须是导出函数 JNIEXPORT
LOGD输出 头部引入#include <android/log.h>
就可以使用__android_log_print(3,“moting”,__VA_ARGS__)
了;
简化的可以为logd
就可以直接用了
logd(“hello”);
创建线程
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 ("+++++++");
先有一个线程变量,然后创建线程。
在上方建一个函数
void* myThread(void* a){
for (int i = 0; i <9; ++i) {
logd("myThread%d",i);
}
pthread_exit(0);
}
加了pthread_join(pthread, nullptr)
就是先执行线程里的,然后执行下面的代码。
传个数字进线程
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 ("===============");
用指针传进去
void* myThread(void* a){
int *num= static_cast<int *>(a);
for (int i = 0; i <*num; ++i) {
logd("myThread%d",i);
}
pthread_exit(0);
}
传个结构体进入线程
创建一个线程
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 ("===============");
然后接着传指针。
c语言返回值很少用,
so中各种函数的执行时机
init -》 init_array-》 jni_onload
jnionload的重写
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;
}
一个so中可以没有JNI_ONLOAD,比如创建一个新线程。必须返回JNI的版本
JNIENV和JNIVm 的区别
JNIENV和JNIVm, 定义了一个全局变量JavaVM* globalVM;
如何使用env调用vm,函数第一个参数是env
JavaVM *vm1;
env->GetJavaVM(&vm1);
globalVm=vm1;
vm1就有值了
或者JNI_OnLoad的第一个参数
- vm拿到env
主线程里
if (vm->GetEnv((void **)&env,JNI_VERSION_1_6)!=JNI_OK){
logd("getenv filed");
}
子线程里
边栏推荐
- Wired network IP address of VMware shared host
- Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败
- AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
- The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
- 3D Detection: 3D Box和点云 快速可视化
- 請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
- DID登陆-MetaMask
- 请问,PTS对数据库压测有好方案么?
- [untitled]
- 参数关键字Final,Flags,Internal,映射关键字Internal
猜你喜欢
Battle Atlas: 12 scenarios detailing the requirements for container safety construction
室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
高等數學---第八章多元函數微分學1
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
Thread pool reject policy best practices
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)
. Net core about redis pipeline and transactions
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
随机推荐
Indoor ROS robot navigation commissioning record (experience in selecting expansion radius)
[AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
Help tenants
LeetCode简单题分享(20)
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
Laravel Form-builder使用
3D detection: fast visualization of 3D box and point cloud
Use day JS let time (displayed as minutes, hours, days, months, and so on)
Dry goods | summarize the linkage use of those vulnerability tools
[untitled]
Common response status codes
Cesium knows the longitude and latitude of one point and the distance to find the longitude and latitude of another point
ES日志报错赏析-Limit of total fields
The meaning of variables starting with underscores in PHP
Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
Huawei image address
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
AI talent cultivation new ideas, this live broadcast has what you care about