深入理解Android(1)——理解Android中的JNI(上)

一、什么是JNI

JNI是Java Native Interface的缩写(Java本地调用),Java程序中的函数可以调用Native语言写的函数(一般指的是C/C++编写的函数),Native语言写的函数可以调用Java层的函数。

二、为什么要有JNI

Java语言的跨平台是因为在不同平台上可以运行Java虚拟机,而虚拟机是跑在具体平台上的,而本质上Java是通过JNI技术实现的跨平台,很多基层的模块在Java语言诞生之前已经有了比较优秀的实现,为了避免重复造轮子所以我们要使用JNI技术来使用已有的模块。


三、Mac OS上的环境搭建

在这里说明一下Max OS上的所需环境搭建,Windows和Linux的请搜索相关资料。

1、安装JDK(此处省略)。

2、安装ADT(Android Develop Tools),包括SDK和ADT插件,下载地址:http://pan.baidu.com/s/1o6OBIHG

3、安装Xcode可以去苹果商店下载安装(免费)。

4、安装Apache ANT(下载地址:http://ant.apache.org/bindownload.cgi)详细安装过程可以参考:http://blog.csdn.net/song_hui_xiang/article/details/14315529

5、安装GNU Make(默认已经安装,所以不用安装)可以使用 make -version命令验证是否安装。

6、安装NDK(下载地址:http://pan.baidu.com/s/1i3l5L8T),解压后在用户根目录下新建文件.bash_profile然后添加如下两行(配置环境变量,可以暂时不配置)。

  1. export ANDROID_NDK_HOME=/Users/lixiaoqiang/Documents/install_tools/ndk/android-ndk-r10c  
  2. export PATH=${PATH}:${ANDROID_NDK_HOME}  
注意:后面的地址是你解压后的目录

关于上面部分开发工具简要介绍:

1、Apache Ant,是一个将软件编译、测试、部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发。

2、NDK是Android原生开发工具包,可以支持C/C++等原生编程语言开发Android应用,它提供头文件、库和交叉编译工具链。

四、第一个示例程序

转载请说明出处:http://blog.csdn.net/dawanganban

1、为eclipse指定NDK路径


2、导入Android NDK中的示例代码(导入hello-jni工程),做过Android开发的朋友应该很熟悉,这里就不啰嗦了。


3、向项目中添加原生支持

项目——>右击——>Android Tools——>Add Native Support

该项目其实已经包含了一个原生项目,所以这一步可以跳过,我们直接Finish继续。

4、插上手机(模拟器太慢了,建议使用真机)运行项目。在C/C++界面视图我们可以看到如下信息

  1. **** Build of configuration Default for project HelloJni ****  
  2.   
  3. /Users/lixiaoqiang/Documents/install_tools/ndk/android-ndk-r10c/ndk-build all   
  4. Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 3 in ./AndroidManifest.xml      
  5. [arm64-v8a] Gdbserver      : [aarch64-linux-android-4.9] libs/arm64-v8a/gdbserver  
  6. [arm64-v8a] Gdbsetup       : libs/arm64-v8a/gdb.setup  
  7. [x86_64] Gdbserver      : [x86_64-4.9] libs/x86_64/gdbserver  
  8. [x86_64] Gdbsetup       : libs/x86_64/gdb.setup  
  9. [mips64] Gdbserver      : [mips64el-linux-android-4.9] libs/mips64/gdbserver  
  10. [mips64] Gdbsetup       : libs/mips64/gdb.setup  
  11. [armeabi-v7a] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi-v7a/gdbserver  
  12. [armeabi-v7a] Gdbsetup       : libs/armeabi-v7a/gdb.setup  
  13. [armeabi] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver  
  14. [armeabi] Gdbsetup       : libs/armeabi/gdb.setup  
  15. [x86] Gdbserver      : [x86-4.6] libs/x86/gdbserver  
  16. [x86] Gdbsetup       : libs/x86/gdb.setup  
  17. [mips] Gdbserver      : [mipsel-linux-android-4.6] libs/mips/gdbserver  
  18. [mips] Gdbsetup       : libs/mips/gdb.setup  
  19. [arm64-v8a] Compile        : hello-jni <= hello-jni.c  
  20. [arm64-v8a] SharedLibrary  : libhello-jni.so  
  21. [arm64-v8a] Install        : libhello-jni.so => libs/arm64-v8a/libhello-jni.so  
  22. [x86_64] Compile        : hello-jni <= hello-jni.c  
  23. [x86_64] SharedLibrary  : libhello-jni.so  
  24. [x86_64] Install        : libhello-jni.so => libs/x86_64/libhello-jni.so  
  25. [mips64] Compile        : hello-jni <= hello-jni.c  
  26. [mips64] SharedLibrary  : libhello-jni.so  
  27. [mips64] Install        : libhello-jni.so => libs/mips64/libhello-jni.so  
  28. [armeabi-v7a] Compile thumb  : hello-jni <= hello-jni.c  
  29. [armeabi-v7a] SharedLibrary  : libhello-jni.so  
  30. [armeabi-v7a] Install        : libhello-jni.so => libs/armeabi-v7a/libhello-jni.so  
  31. [armeabi] Compile thumb  : hello-jni <= hello-jni.c  
  32. [armeabi] SharedLibrary  : libhello-jni.so  
  33. [armeabi] Install        : libhello-jni.so => libs/armeabi/libhello-jni.so  
  34. [x86] Compile        : hello-jni <= hello-jni.c  
  35. [x86] SharedLibrary  : libhello-jni.so  
  36. [x86] Install        : libhello-jni.so => libs/x86/libhello-jni.so  
  37. [mips] Compile        : hello-jni <= hello-jni.c  
  38. [mips] SharedLibrary  : libhello-jni.so  
  39. [mips] Install        : libhello-jni.so => libs/mips/libhello-jni.so  
  40.   
  41. **** Build Finished ****  
这个过程其实就是在构建原生组件并和Java应用程序打包的过程。此时在我们手机上就可以看到一行文字


五、项目结构及主要目录介绍


1、jni目录:包含原生组件的源代码及描述原生组件构建方法的Make文件(Android.mk),该目录作为NDK构建项目时的构建目录。

2、libs目录:包含指定目标平台的独立子目录,在打包时该目录被包含在apk文件中。

3、obj目录:这是一个中间目录,编译源码后产生的目标文件都保存在该目录下,我们最好不用访问该目录。

六、 实例工程解析

Android.mk的内容如下

  1. # Copyright (C) 2009 The Android Open Source Project  
  2. #  
  3. # Licensed under the Apache License, Version 2.0 (the "License");  
  4. # you may not use this file except in compliance with the License.  
  5. # You may obtain a copy of the License at  
  6. #  
  7. #      http://www.apache.org/licenses/LICENSE-2.0  
  8. #  
  9. # Unless required by applicable law or agreed to in writing, software  
  10. # distributed under the License is distributed on an "AS IS" BASIS,  
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  12. # See the License for the specific language governing permissions and  
  13. # limitations under the License.  
  14. #  
  15. LOCAL_PATH := $(call my-dir)  
  16.   
  17. include $(CLEAR_VARS)  
  18.   
  19. LOCAL_MODULE    := hello-jni  
  20. LOCAL_SRC_FILES := hello-jni.c  
  21.   
  22. include $(BUILD_SHARED_LIBRARY)  
有关Makefile的知识请参考我的另一篇博文:http://blog.csdn.net/dawanganban/article/details/38750151

第一行:Android.mk文档必须以LOCAL_PATH变量的定义开头,my-dir是一个系统的宏定义,来定义源文件的目录位置。

第二行:Android构建系统将CLEAR_VARS变量设置为clear-vars.mk片段的位置,更多片段的makefile文件请看ndk\build\core目录,如下:


作用是清除除了LOCAL_PATH以外的LOCAL_<name>变量,这样可以避免冲突。

第三行:每一个原生组件被称为一个模块,LOCAL_MODULE变量用来给这些模块设定一个唯一的名称。

第四行:LOCAL_SRC_FILES变量定义用来建立和组装这个模块的源文件列表,用空格隔开。

第五行:指明了build-shared-library.mk文件的保存位置,该片段包含了将源文件构建成共享库的必要过程。

下面我们来看看HelloJni.java

  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.   
  18. import android.app.Activity;  
  19. import android.widget.TextView;  
  20. import android.os.Bundle;  
  21.   
  22.   
  23. public class HelloJni extends Activity  
  24. {  
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState)  
  28.     {  
  29.         super.onCreate(savedInstanceState);  
  30.   
  31.         /* Create a TextView and set its content. 
  32.          * the text is retrieved by calling a native 
  33.          * function. 
  34.          */  
  35.         TextView  tv = new TextView(this);  
  36.         tv.setText( stringFromJNI() );  
  37.         setContentView(tv);  
  38.     }  
  39.   
  40.     /* A native method that is implemented by the 
  41.      * 'hello-jni' native library, which is packaged 
  42.      * with this application. 
  43.      */  
  44.     public native String  stringFromJNI();  
  45.   
  46.     /* This is another native method declaration that is *not* 
  47.      * implemented by 'hello-jni'. This is simply to show that 
  48.      * you can declare as many native methods in your Java code 
  49.      * as you want, their implementation is searched in the 
  50.      * currently loaded native libraries only the first time 
  51.      * you call them. 
  52.      * 
  53.      * Trying to call this function will result in a 
  54.      * java.lang.UnsatisfiedLinkError exception ! 
  55.      */  
  56.     public native String  unimplementedStringFromJNI();  
  57.   
  58.     /* this is used to load the 'hello-jni' library on application 
  59.      * startup. The library has already been unpacked into 
  60.      * /data/data/com.example.hellojni/lib/libhello-jni.so at 
  61.      * installation time by the package manager. 
  62.      */  
  63.     static {  
  64.         System.loadLibrary("hello-jni");  
  65.     }  
  66. }  
从上面可以看到调用了原生的stringFromJNI()方法,使用关键字native来通知Java编译器,这个是用另一种语言实现的,再通过加装共享库(static语句块)hello-jni来告诉虚拟机原生方法的具体实现。java.lang.System类提供了两个静态方法,load和loadLibrary用来运行时加载共享库。下面我们来看看具体的实现。

  1. #include <string.h>  
  2. #include <jni.h>  
  3.   
  4. /* This is a trivial JNI example where we use a native method 
  5.  * to return a new VM String. See the corresponding Java source 
  6.  * file located at: 
  7.  * 
  8.  *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java 
  9.  */  
  10. jstring  
  11. Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,  
  12.                                                   jobject thiz )  
  13. {  
  14. #if defined(__arm__)  
  15.   #if defined(__ARM_ARCH_7A__)  
  16.     #if defined(__ARM_NEON__)  
  17.       #if defined(__ARM_PCS_VFP)  
  18.         #define ABI "armeabi-v7a/NEON (hard-float)"  
  19.       #else  
  20.         #define ABI "armeabi-v7a/NEON"  
  21.       #endif  
  22.     #else  
  23.       #if defined(__ARM_PCS_VFP)  
  24.         #define ABI "armeabi-v7a (hard-float)"  
  25.       #else  
  26.         #define ABI "armeabi-v7a"  
  27.       #endif  
  28.     #endif  
  29.   #else  
  30.    #define ABI "armeabi"  
  31.   #endif  
  32. #elif defined(__i386__)  
  33.    #define ABI "x86"  
  34. #elif defined(__x86_64__)  
  35.    #define ABI "x86_64"  
  36. #elif defined(__mips64)  /* mips64el-* toolchain defines __mips__ too */  
  37.    #define ABI "mips64"  
  38. #elif defined(__mips__)  
  39.    #define ABI "mips"  
  40. #elif defined(__aarch64__)  
  41.    #define ABI "arm64-v8a"  
  42. #else  
  43.    #define ABI "unknown"  
  44. #endif  
  45.   
  46.     return (*env)->NewStringUTF(env, "Hello from JNI !  Compiled with ABI " ABI ".");  
  47. }  
第一个参数JNIEnv是指向可用JNI函数表的接口指针,第二个参数jobject是HelloJni类实例的java对象。最后一句代码是用c字符串创建UTF-8的Java字符串。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值