博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android jni的JNINativeMethod
阅读量:6553 次
发布时间:2019-06-24

本文共 4159 字,大约阅读时间需要 13 分钟。

hot3.png

<p>Andoird 中使用了一种不同传统Java JNI的方式来定义其native的函数。其中很重要的区别是Andorid使用了一种Java 和 C 函数的映射表数组,并在其中描述了函数的参数和返回值。这个数组的类型是JNINativeMethod,定义如下:</p> <p><wbr></p> <p>typedef struct {<br> <wbr><wbr><wbr>const char* name;<br> <wbr><wbr><wbr>const char* signature;<br> <wbr><wbr><wbr>void* <wbr><wbr><wbr><wbr><wbr><wbr>fnPtr;<br> } JNINativeMethod;</p> <p>第一个变量name是Java中函数的名字。</p> <p>第二个变量signature,用字符串是描述了函数的参数和返回值</p> <p>第三个变量fnPtr是函数指针,指向C函数。</p> <p><wbr></p> <p>其中比较难以理解的是第二个参数,例如</p> <p>"()V"</p> <p>"(II)V"</p> <p>"(Ljava/lang/String;Ljava/lang/String;)V"</p> <p><wbr></p> <p>实际上这些字符是与函数的参数类型一一对应的。</p> <p>"()" 中的字符表示参数,后面的则代表返回值。例如"()V" 就表示void Func();</p> <p>"(II)V" 表示 void Func(int, int);</p> <p><wbr></p> <p>具体的每一个字符的对应关系如下</p> <p><wbr></p> <p><strong>字符 <wbr><wbr><wbr><wbr>Java类型 <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>C类型</strong></p> <p>V&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>void&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>void<br> Z&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jboolean&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>boolean<br> I&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int<br> J&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<wbr><wbr><wbr><wbr><wbr><wbr><wbr>jlong&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long<br> D&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jdouble&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>double<br> F&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jfloat&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>float<br> B&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jbyte&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>byte<br> C&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jchar&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>char<br> S&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jshort&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>short</p> <p><wbr></p> <p>数组则以"["开始,用两个字符表示</p> <p><wbr></p> <p>[I&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jintArray&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int[]<br> [F&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jfloatArray&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr>float[]<br> [B&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jbyteArray&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>byte[]<br> [C&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jcharArray&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>char[]<br> [S&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jshortArray&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr>short[]<br> [D&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jdoubleArray <wbr><wbr><wbr><wbr>double[]<br> [J&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jlongArray&nbsp;&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>long[]<br> [Z&nbsp;&nbsp;&nbsp; <wbr><wbr><wbr><wbr><wbr><wbr>jbooleanArray <wbr><wbr><wbr>boolean[]</p> <p><wbr></p> <p>上面的都是基本类型。如果Java函数的参数是class,则以"L"开头,以";"结尾中间是用"/" 隔开的包及类名。而其对应的C函数名的参数则为jobject. 一个例外是String类,其对应的类为jstring</p> <p><wbr></p> <p>Ljava/lang/String; <wbr><wbr><wbr><wbr><wbr>String <wbr><wbr><wbr><wbr><wbr><wbr><wbr>jstring<br> Ljava/net/Socket; <wbr><wbr><wbr><wbr><wbr>Socket <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>jobject</p> <p><wbr></p> <p>如果JAVA函数位于一个嵌入类,则用$作为类名间的分隔符。</p> <p>例如 "(Ljava/lang/String;Landroid/os/FileUtils$FileStatus;)Z"</p> <p><br> </p>

转载于:https://my.oschina.net/u/247706/blog/86187

你可能感兴趣的文章
由ibatis向mybatis的转变
查看>>
Android Button事件
查看>>
IO流(PrintWriter) 很重要的一个类 核心
查看>>
python实现WordCount基础和拓展功能
查看>>
《开拓者研发团队》——基于弹幕评论的大数据分析平台项目的原型设计与开发...
查看>>
实践.Net Core在Linux环境下的第一个Hello World
查看>>
flask+redis实现抢购(秒杀)功能
查看>>
J2EE 十三个技术规范
查看>>
结对-象棋-测试过程
查看>>
SQLALchemy之介绍,基本使用
查看>>
堆——优先队列
查看>>
GDI+ Custom Controls with Visual C# 2005
查看>>
跨浏览器本地存储框架(store.js/USTORE.js/Box.js)
查看>>
css3关键帧动画实现轮播效果
查看>>
小白的进阶之路2
查看>>
通过python的import hooks实现像引用代码一样使用配置文件
查看>>
【Ecstore2.0】计划任务/队列/导入导出 的执行问题
查看>>
微信公众平台接入广告投放系统
查看>>
OpenCV学习(28) 轮廓
查看>>
js4数据类型、类型转换
查看>>