当前位置:网站首页>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");
}
子线程里
边栏推荐
- Help tenants
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
- 属性关键字Aliases,Calculated,Cardinality,ClientName
- Realization of search box effect [daily question]
- PostgreSQL array type, each splice
- 2022-7-7 Leetcode 844. Compare strings with backspace
- Leecode3. Longest substring without repeated characters
- Laravel Form-builder使用
- [daily training] 648 Word replacement
- 118. Yanghui triangle
猜你喜欢

Transferring files between VMware and host

566. Reshaping the matrix

Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
![SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1

室內ROS機器人導航調試記錄(膨脹半徑的選取經驗)

Flask session forged hctf admin

flask session伪造之hctf admin

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)

Use day JS let time (displayed as minutes, hours, days, months, and so on)

Parsing of XML files
随机推荐
Learning breakout 2 - about effective learning methods
postgresql array类型,每一项拼接
Realize the IP address home display function and number home query
Regular expression integer positive integer some basic expressions
[high frequency interview questions] difficulty 2.5/5, simple combination of DFS trie template level application questions
带你掌握三层架构(建议收藏)
【日常训练--腾讯精选50】231. 2 的幂
How does MySQL control the number of replace?
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
《厌女:日本的女性嫌恶》摘录
Mysql怎样控制replace替换的次数?
MySQL "invalid use of null value" solution
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
LeetCode简单题分享(20)
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
PC端页面如何调用QQ进行在线聊天?
最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
2022-7-6 beginner redis (I) download, install and run redis under Linux
call undefined function openssl_cipher_iv_length
When FC connects to the database, do you have to use a custom domain name to access it outside?