简单介绍一下JNI:

JNI是Java Native Interface的缩写,中文为JAVA本地调用。从Java1.1开始,Java Native Interface(JNI)标准成为java平台的一部分,它允许Java代码和其他语言写的代码进行交互。JNI一开始是为了本地已编译语言,尤其是C和C++而设计的,但是它并不妨碍你使用其他语言,只要调用约定受支持就可以了。

android  jni 开发 使用NDK 开发工具。

参考:

http://blog.csdn.net/acanoe/article/details/8613714

转载请标明出处:

http://blog.csdn.net/ACanoe

这篇博文简单分析一下jni 开发所依赖的文件和程序简单说明:

下面是 hello-jni 目录结构

|-- AndroidManifest.xml
|-- default.properties
|-- jni
|   |-- Android.mk # Android.mk  类似于Linux 下的Makefile
|   `-- hello-jni.c
|-- libs
|   `-- armeabi
|       |-- gdb.setup
|       |-- gdbserver
|       `-- libhello-jni.so
|-- obj
|   `-- local
|       `-- armeabi
|           |-- libhello-jni.so # 这个是编译后得到的.so 文件
|           `-- objs-debug
|               `-- hello-jni
|                   |-- hello-jni.o
|                   `-- hello-jni.o.d
|-- res
|   `-- values
|       `-- strings.xml
|-- src
|   `-- com
|       `-- example
|           `-- hellojni
|               `-- HelloJni.java
`-- tests
    |-- AndroidManifest.xml
    |-- default.properties
    `-- src
        `-- com
            `-- example
                `-- HelloJni
                    `-- HelloJniTest.java

主要代码为:jni/hello-jni.c   和 src/com/example/hellojni/HelloJni.java

HelloJni.java 代码:

[java] view plaincopyprint?
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.hellojni;
  17. import android.app.Activity;
  18. import android.widget.TextView;
  19. import android.os.Bundle;
  20. public class HelloJni extends Activity
  21. {
  22. /** Called when the activity is first created. */
  23. @Override
  24. public void onCreate(Bundle savedInstanceState)
  25. {
  26. super.onCreate(savedInstanceState);
  27. /* Create a TextView and set its content.
  28. * the text is retrieved by calling a native
  29. * function.
  30. */
  31. TextView  tv = new TextView(this);
  32. tv.setText( stringFromJNI() );
  33. setContentView(tv);
  34. }
  35. /* A native method that is implemented by the
  36. * 'hello-jni' native library, which is packaged
  37. * with this application.
  38. */
  39. public native String  stringFromJNI();       // 函数 <span style="font-family: Arial, Helvetica, sans-serif;">stringFromJNI() 调用 lib 中 hello-jni.c 中的 </span>
  40. <span style="white-space: pre;">                        </span> // Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,jobject thiz )
  41. // 函数名解析为: java 为语言 ,<span style="font-family: Arial, Helvetica, sans-serif;">com_example_hellojni 为包目录,</span><pre class="java" name="code"><span style="font-family: Arial, Helvetica, sans-serif;">HelloJni</span><span style="font-family: Arial, Helvetica, sans-serif;">也就是类名 , stringFormJNI,是你在 .java 中调用的真正函数名。</span></pre> /* This is another native method declaration that is *not* * implemented by 'hello-jni'. This is simply to show that * you can declare as many native methods in your Java code * as you want, their implementation is searched in the * currently
  42. loaded native libraries only the first time * you call them. * * Trying to call this function will result in a * java.lang.UnsatisfiedLinkError exception ! */ public native String unimplementedStringFromJNI(); /* this is used to load the 'hello-jni' library
  43. on application * startup. The library has already been unpacked into * /data/data/com.example.HelloJni/lib/libhello-jni.so at * installation time by the package manager. */ static { System.loadLibrary("hello-jni"); // 声明使用lib 的文件名, 去掉 libhello-jni.so 中的lib
  44. 和 .so 字符后就是程序所要声明的lib 名称 }}
  45. <pre></pre>
  46. <p></p>
  47. <p><br>
  48. </p>
  49. <p><br>
  50. </p>
  51. hello-jni.c  代码为:
  52. <p></p>
  53. <pre class="cpp" name="code">/*
  54. * Copyright (C) 2009 The Android Open Source Project
  55. *
  56. * Licensed under the Apache License, Version 2.0 (the "License");
  57. * you may not use this file except in compliance with the License.
  58. * You may obtain a copy of the License at
  59. *
  60. *      http://www.apache.org/licenses/LICENSE-2.0
  61. *
  62. * Unless required by applicable law or agreed to in writing, software
  63. * distributed under the License is distributed on an "AS IS" BASIS,
  64. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  65. * See the License for the specific language governing permissions and
  66. * limitations under the License.
  67. *
  68. */
  69. #include <string.h>
  70. #include <jni.h>
  71. /* This is a trivial JNI example where we use a native method
  72. * to return a new VM String. See the corresponding Java source
  73. * file located at:
  74. *
  75. *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
  76. */
  77. jstring
  78. Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
  79. jobject thiz )
  80. {
  81. return (*env)->NewStringUTF(env, "Hello from JNI !");
  82. }
  83. </pre>
  84. <p></p>
  85. <p><br>
  86. </p>
  87. jni/Android.mk 代码为:
  88. <p></p>
  89. <pre class="cpp" name="code"># Copyright (C) 2009 The Android Open Source Project
  90. #
  91. # Licensed under the Apache License, Version 2.0 (the "License");
  92. # you may not use this file except in compliance with the License.
  93. # You may obtain a copy of the License at
  94. #
  95. #      http://www.apache.org/licenses/LICENSE-2.0
  96. #
  97. # Unless required by applicable law or agreed to in writing, software
  98. # distributed under the License is distributed on an "AS IS" BASIS,
  99. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  100. # See the License for the specific language governing permissions and
  101. # limitations under the License.
  102. #
  103. LOCAL_PATH := $(call my-dir)
  104. include $(CLEAR_VARS)
  105. LOCAL_MODULE    := hello-jni<span style="white-space: pre;">    </span>#编译后的Module
  106. LOCAL_SRC_FILES := hello-jni.c  #要编译的源码文件
  107. include $(BUILD_SHARED_LIBRARY)</pre><br>
  108. <br>
  109. <p>你可以尝试修改函数名,以及替换字符串。来验证.so 调用正确与否。</p>
  110. <p>为了验证 hello-jni.c 中大函数是引用包目录,现在我新建一个android test 项目使用MainActivity默认的类名称,使用button 来获得libhello-jni.so 中的字符串。</p>
  111. <p> 新包目录结构为 button  com.example.button  </p>
  112. <p>其中MainActivity.java 代码为:</p>
  113. <p></p>
  114. <pre class="cpp" name="code">package com.example.button;
  115. import android.os.Bundle;
  116. import android.app.Activity;
  117. import android.graphics.Color;
  118. import android.view.View;
  119. import android.widget.TextView;
  120. public class MainActivity extends Activity {
  121. TextView mtxtPeri;
  122. @Override
  123. protected void onCreate(Bundle savedInstanceState) {
  124. super.onCreate(savedInstanceState);
  125. setContentView(R.layout.activity_main);
  126. mtxtPeri = (TextView)findViewById(R.id.txtPeri);
  127. //下面的代码用于为按钮注册一个监听
  128. findViewById(R.id.button).setOnClickListener(new OnClickListener() {
  129. //下面的代码用于处理按钮点击后的事件
  130. public void onClick(View v) {
  131. //下面的代码用于使背景变色
  132. mtxtPeri.setText( stringFromJNI() );
  133. //   setContentView(mtxtPeri);
  134. findViewById(R.id.button).setBackgroundColor(Color.BLUE);</pre><pre class="cpp" name="code"><span style="white-space: pre;">        </span>Log.v("Test_fclose", stringFromJNI());      // Log cat 打印操作
  135. }
  136. });
  137. }
  138. public native String  stringFromJNI();
  139. /* This is another native method declaration that is *not*
  140. * implemented by 'hello-jni'. This is simply to show that
  141. * you can declare as many native methods in your Java code
  142. * as you want, their implementation is searched in the
  143. * currently loaded native libraries only the first time
  144. * you call them.
  145. *
  146. * Trying to call this function will result in a
  147. * java.lang.UnsatisfiedLinkError exception !
  148. */
  149. public native String  unimplementedStringFromJNI();
  150. /* this is used to load the 'hello-jni' library on application
  151. * startup. The library has already been unpacked into
  152. * /data/data/com.example.HelloJni/lib/libhello-jni.so at
  153. * installation time by the package manager.
  154. */
  155. static {
  156. System.loadLibrary("hello-jni");
  157. }
  158. }
  159. </pre>
  160. <p></p>
  161. <p><br>
  162. </p>
  163. XML 文件修改<br>
  164. res/layout/activity_main.xml 代码为:
  165. <p></p>
  166. <pre class="html" name="code"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  167. xmlns:tools="http://schemas.android.com/tools"
  168. android:layout_width="match_parent"
  169. android:layout_height="match_parent"
  170. tools:context=".MainActivity" >
  171. <TextView
  172. android:id="@+id/txtPeri"
  173. android:layout_width="wrap_content"
  174. android:layout_height="wrap_content"
  175. android:layout_centerHorizontal="true"
  176. android:layout_centerVertical="true"
  177. />
  178. <Button android:id="@+id/button"
  179. android:layout_width="wrap_content"
  180. android:layout_height="wrap_content"
  181. />
  182. </RelativeLayout></pre>这时编译运行的时候就会报错,应为对应的libs 对应的 包目录不对。 所以需要重新编译修改.so 文件。
  183. <p></p>
  184. <p>只需要将原有的hello-jni.c 的函数名修改为:</p>
  185. <p>Java_com_example_button_MainActivity_stringFromJNI()<br>
  186. </p>
  187. <p>就像我前面所说的: Java 为语言, com.example.button 为包目录, MainActivity 为class 名,stringFromJNI 为class 类中实际调用的接口。(这一部分纯属我个人理解)</p>
  188. <p>1.然后重新编译,将生成的.so 文件替换原有的就行。</p>
  189. <p>2. run</p>
  190. <p>点击按钮能获得我们改写的字符串就代表验证成功。</p>
  191. <p>代码分析中若有出错,还望多多指正。</p>
  192. <p><br>
  193. </p>

安卓中间件 hello dema解析相关推荐

  1. 安卓系统文件夹及其文件解析

    安卓系统文件夹及其文件解析 打开Android文件管理器,会发现里面数十个英文名称命名的文件夹罗列其中,很多功能我们可以从其名字上略有所知,内部大批量的文件却让我们有些一头雾水.这些文件是什么?有什么 ...

  2. [转] Nodejs 进阶:Express 常用中间件 body-parser 实现解析

    写在前面 body-parser是非常常用的一个express中间件,作用是对post请求的请求体进行解析.使用非常简单,以下两行代码已经覆盖了大部分的使用场景. app.use(bodyParser ...

  3. 安卓接受后台数据转换解析出错_安卓手机内存越大,速度就会越快?

    与非网 2 月 18 日讯,现在安卓手机的内存在产品规划上越来越大,2020 年发布的旗舰手机运行内存已经拓展到惊人的 10GB 以上,而这个运存大小已经超过笔记本主流的 8GB 内存,手机到底需要这 ...

  4. Express 常用中间件 body-parser 实现解析

    写在前面 body-parser是非常常用的一个express中间件作用是对post请求的请求体进行解析.使用非常简单以下两行代码已经覆盖了大部分的使用场景. app.use(bodyParser.j ...

  5. 安卓OTA升级系统解析上

    在MTK安卓环境中只需要在alps 目录执行./mk otapackage即可打包ota升级包,下面我们来分析下这个过程中.  alps/mk代码片段 sub chkDep {   my $modul ...

  6. multiparty 中间件源码解析

    写这篇文章的起因是我在尝试使用 Node 的原生模块去接收表单上传的数据并分割过滤表单文件与数据时,由于一开始是对可写流拼接 buffer 转的字符串进行轮询,导致只能接收文件格式为 txt 的文本文 ...

  7. android string json,安卓之String json解析

    //String的jsonobject解析 String jsonstr = source.toString(); JSONObject jsonobj = new JSONObject(jsonst ...

  8. AndroidStudio_安卓原生开发_Json解析报错_要注意这点---Android原生开发工作笔记141

    人脸识别的一个pad程序,平时运行没发现问题,但是后来报错了...不知道怎么回事,但是 找到了错误出现的地方: 是因为json字符串解析的时候,报错的: try {jsonObj=new JSONOb ...

  9. android 监听fling,[安卓]Android Recycler Fling解析

    问题描述 最近在做appbarlayout和recyclerView配合使用的时候,发现recyclerView和appbarlayout配合过程偶尔会非常的诡异,特别是快速滑动的时候会导致appba ...

最新文章

  1. 解决Eclipse添加新server时无法选择Tomcat7的问题
  2. python使用numpy的np.power函数计算numpy数组中每个数值的指定幂次(例如平方、立方)、np.power函数默认返回整数格式、np.float_power函数默认返回浮点数
  3. 宇通客车java_6米采血车
  4. 窗口分析函数_15_找出第一个元素
  5. 云上高并发系统改造最佳实践
  6. 网络上总结python中的面试题
  7. mysql字段作用_mysql用户表host字段作用
  8. Linux C入门之路,Linux C++学习之路
  9. 数据流中的中位数 c语言,41 数据流中的中位数(时间效率)
  10. python清理日志脚本_Python日志:如果在程序运行时删除了日志文件,则创建新的日志文件(RotatingFileHandler)...
  11. 5双机配置_CentOS 7 高可用双机热备实现
  12. VMware GSX Server 3.2.1 Build 19281免费下载
  13. Niubility!华为天才少年自制机械臂能给葡萄缝针
  14. 程序员讨厌没有价值的任务
  15. POJ2248 Addition Chains(迭代加深搜索)
  16. StringBuffer的常用方法
  17. 360手机:360N5S Twrp、Root、Magisk教程
  18. krc 编辑 linux,KRC 文件扩展名: 它是什么以及如何打开它?
  19. RISC-V详细介绍
  20. Unity Shader Graph 制作Rim Light边缘光效果

热门文章

  1. 文件服务器挂载命令,使用mount命令进行目录挂载
  2. 黑马4天从浅入深精通SpringCloud 微服务架构(完整资料)
  3. 趣味Python | 223 段代码助你从入门到大师
  4. ruby 中叹号问号的作用
  5. oracle 开区间,OOAP0019 开区间错误
  6. zookeeper 使用场景
  7. oracle 本地数据库卸载,Oracle数据库卸载
  8. 深入解读 Elasticsearch 热点线程 hot_threads
  9. 淘宝自动发货助手插旗API接口,实现订单插旗颜色标注,自动发货接口
  10. 合作师专计算机培训,2016年西北师范大学校企合作计算机联合办学建设方案.pdf...