安卓中间件 hello dema解析
简单介绍一下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 代码:
- /*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.example.hellojni;
- import android.app.Activity;
- import android.widget.TextView;
- import android.os.Bundle;
- public class HelloJni extends Activity
- {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- /* Create a TextView and set its content.
- * the text is retrieved by calling a native
- * function.
- */
- TextView tv = new TextView(this);
- tv.setText( stringFromJNI() );
- setContentView(tv);
- }
- /* A native method that is implemented by the
- * 'hello-jni' native library, which is packaged
- * with this application.
- */
- public native String stringFromJNI(); // 函数 <span style="font-family: Arial, Helvetica, sans-serif;">stringFromJNI() 调用 lib 中 hello-jni.c 中的 </span>
- <span style="white-space: pre;"> </span> // Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,jobject thiz )
- // 函数名解析为: 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
- 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
- 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
- 和 .so 字符后就是程序所要声明的lib 名称 }}
- <pre></pre>
- <p></p>
- <p><br>
- </p>
- <p><br>
- </p>
- hello-jni.c 代码为:
- <p></p>
- <pre class="cpp" name="code">/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- #include <string.h>
- #include <jni.h>
- /* This is a trivial JNI example where we use a native method
- * to return a new VM String. See the corresponding Java source
- * file located at:
- *
- * apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
- */
- jstring
- Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
- jobject thiz )
- {
- return (*env)->NewStringUTF(env, "Hello from JNI !");
- }
- </pre>
- <p></p>
- <p><br>
- </p>
- jni/Android.mk 代码为:
- <p></p>
- <pre class="cpp" name="code"># Copyright (C) 2009 The Android Open Source Project
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- #
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE := hello-jni<span style="white-space: pre;"> </span>#编译后的Module
- LOCAL_SRC_FILES := hello-jni.c #要编译的源码文件
- include $(BUILD_SHARED_LIBRARY)</pre><br>
- <br>
- <p>你可以尝试修改函数名,以及替换字符串。来验证.so 调用正确与否。</p>
- <p>为了验证 hello-jni.c 中大函数是引用包目录,现在我新建一个android test 项目使用MainActivity默认的类名称,使用button 来获得libhello-jni.so 中的字符串。</p>
- <p> 新包目录结构为 button com.example.button </p>
- <p>其中MainActivity.java 代码为:</p>
- <p></p>
- <pre class="cpp" name="code">package com.example.button;
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.Color;
- import android.view.View;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- TextView mtxtPeri;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mtxtPeri = (TextView)findViewById(R.id.txtPeri);
- //下面的代码用于为按钮注册一个监听
- findViewById(R.id.button).setOnClickListener(new OnClickListener() {
- //下面的代码用于处理按钮点击后的事件
- public void onClick(View v) {
- //下面的代码用于使背景变色
- mtxtPeri.setText( stringFromJNI() );
- // setContentView(mtxtPeri);
- 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 打印操作
- }
- });
- }
- public native String stringFromJNI();
- /* 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 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 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");
- }
- }
- </pre>
- <p></p>
- <p><br>
- </p>
- XML 文件修改<br>
- res/layout/activity_main.xml 代码为:
- <p></p>
- <pre class="html" name="code"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/txtPeri"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- />
- <Button android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </RelativeLayout></pre>这时编译运行的时候就会报错,应为对应的libs 对应的 包目录不对。 所以需要重新编译修改.so 文件。
- <p></p>
- <p>只需要将原有的hello-jni.c 的函数名修改为:</p>
- <p>Java_com_example_button_MainActivity_stringFromJNI()<br>
- </p>
- <p>就像我前面所说的: Java 为语言, com.example.button 为包目录, MainActivity 为class 名,stringFromJNI 为class 类中实际调用的接口。(这一部分纯属我个人理解)</p>
- <p>1.然后重新编译,将生成的.so 文件替换原有的就行。</p>
- <p>2. run</p>
- <p>点击按钮能获得我们改写的字符串就代表验证成功。</p>
- <p>代码分析中若有出错,还望多多指正。</p>
- <p><br>
- </p>
安卓中间件 hello dema解析相关推荐
- 安卓系统文件夹及其文件解析
安卓系统文件夹及其文件解析 打开Android文件管理器,会发现里面数十个英文名称命名的文件夹罗列其中,很多功能我们可以从其名字上略有所知,内部大批量的文件却让我们有些一头雾水.这些文件是什么?有什么 ...
- [转] Nodejs 进阶:Express 常用中间件 body-parser 实现解析
写在前面 body-parser是非常常用的一个express中间件,作用是对post请求的请求体进行解析.使用非常简单,以下两行代码已经覆盖了大部分的使用场景. app.use(bodyParser ...
- 安卓接受后台数据转换解析出错_安卓手机内存越大,速度就会越快?
与非网 2 月 18 日讯,现在安卓手机的内存在产品规划上越来越大,2020 年发布的旗舰手机运行内存已经拓展到惊人的 10GB 以上,而这个运存大小已经超过笔记本主流的 8GB 内存,手机到底需要这 ...
- Express 常用中间件 body-parser 实现解析
写在前面 body-parser是非常常用的一个express中间件作用是对post请求的请求体进行解析.使用非常简单以下两行代码已经覆盖了大部分的使用场景. app.use(bodyParser.j ...
- 安卓OTA升级系统解析上
在MTK安卓环境中只需要在alps 目录执行./mk otapackage即可打包ota升级包,下面我们来分析下这个过程中. alps/mk代码片段 sub chkDep { my $modul ...
- multiparty 中间件源码解析
写这篇文章的起因是我在尝试使用 Node 的原生模块去接收表单上传的数据并分割过滤表单文件与数据时,由于一开始是对可写流拼接 buffer 转的字符串进行轮询,导致只能接收文件格式为 txt 的文本文 ...
- android string json,安卓之String json解析
//String的jsonobject解析 String jsonstr = source.toString(); JSONObject jsonobj = new JSONObject(jsonst ...
- AndroidStudio_安卓原生开发_Json解析报错_要注意这点---Android原生开发工作笔记141
人脸识别的一个pad程序,平时运行没发现问题,但是后来报错了...不知道怎么回事,但是 找到了错误出现的地方: 是因为json字符串解析的时候,报错的: try {jsonObj=new JSONOb ...
- android 监听fling,[安卓]Android Recycler Fling解析
问题描述 最近在做appbarlayout和recyclerView配合使用的时候,发现recyclerView和appbarlayout配合过程偶尔会非常的诡异,特别是快速滑动的时候会导致appba ...
最新文章
- 解决Eclipse添加新server时无法选择Tomcat7的问题
- python使用numpy的np.power函数计算numpy数组中每个数值的指定幂次(例如平方、立方)、np.power函数默认返回整数格式、np.float_power函数默认返回浮点数
- 宇通客车java_6米采血车
- 窗口分析函数_15_找出第一个元素
- 云上高并发系统改造最佳实践
- 网络上总结python中的面试题
- mysql字段作用_mysql用户表host字段作用
- Linux C入门之路,Linux C++学习之路
- 数据流中的中位数 c语言,41 数据流中的中位数(时间效率)
- python清理日志脚本_Python日志:如果在程序运行时删除了日志文件,则创建新的日志文件(RotatingFileHandler)...
- 5双机配置_CentOS 7 高可用双机热备实现
- VMware GSX Server 3.2.1 Build 19281免费下载
- Niubility!华为天才少年自制机械臂能给葡萄缝针
- 程序员讨厌没有价值的任务
- POJ2248 Addition Chains(迭代加深搜索)
- StringBuffer的常用方法
- 360手机:360N5S Twrp、Root、Magisk教程
- krc 编辑 linux,KRC 文件扩展名: 它是什么以及如何打开它?
- RISC-V详细介绍
- Unity Shader Graph 制作Rim Light边缘光效果
热门文章
- 文件服务器挂载命令,使用mount命令进行目录挂载
- 黑马4天从浅入深精通SpringCloud 微服务架构(完整资料)
- 趣味Python | 223 段代码助你从入门到大师
- ruby 中叹号问号的作用
- oracle 开区间,OOAP0019 开区间错误
- zookeeper 使用场景
- oracle 本地数据库卸载,Oracle数据库卸载
- 深入解读 Elasticsearch 热点线程 hot_threads
- 淘宝自动发货助手插旗API接口,实现订单插旗颜色标注,自动发货接口
- 合作师专计算机培训,2016年西北师范大学校企合作计算机联合办学建设方案.pdf...