当前位置: 代码迷 >> Android >> 深入懂得Android(1)——理解Android中的JNI
  详细解决方案

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

热度:45   发布时间:2016-04-28 03:01:17.0
深入理解Android(1)——理解Android中的JNI

我参加了CSDN博客之星评选,如果在过去的一段时间里阳光小强的博客对你有所帮助,在这里希望能投上您宝贵的一票,每天都可以投一次:http://vote.blog.csdn.net/blogstar2014/details?username=lxq_xsyu#content

一、什么是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然后添加如下两行(配置环境变量,可以暂时不配置)。

export ANDROID_NDK_HOME=/Users/lixiaoqiang/Documents/install_tools/ndk/android-ndk-r10cexport 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++界面视图我们可以看到如下信息

**** Build of configuration Default for project HelloJni ****/Users/lixiaoqiang/Documents/install_tools/ndk/android-ndk-r10c/ndk-build all Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 3 in ./AndroidManifest.xml    [arm64-v8a] Gdbserver      : [aarch64-linux-android-4.9] libs/arm64-v8a/gdbserver[arm64-v8a] Gdbsetup       : libs/arm64-v8a/gdb.setup[x86_64] Gdbserver      : [x86_64-4.9] libs/x86_64/gdbserver[x86_64] Gdbsetup       : libs/x86_64/gdb.setup[mips64] Gdbserver      : [mips64el-linux-android-4.9] libs/mips64/gdbserver[mips64] Gdbsetup       : libs/mips64/gdb.setup[armeabi-v7a] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi-v7a/gdbserver[armeabi-v7a] Gdbsetup       : libs/armeabi-v7a/gdb.setup[armeabi] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver[armeabi] Gdbsetup       : libs/armeabi/gdb.setup[x86] Gdbserver      : [x86-4.6] libs/x86/gdbserver[x86] Gdbsetup       : libs/x86/gdb.setup[mips] Gdbserver      : [mipsel-linux-android-4.6] libs/mips/gdbserver[mips] Gdbsetup       : libs/mips/gdb.setup[arm64-v8a] Compile        : hello-jni <= hello-jni.c[arm64-v8a] SharedLibrary  : libhello-jni.so[arm64-v8a] Install        : libhello-jni.so => libs/arm64-v8a/libhello-jni.so[x86_64] Compile        : hello-jni <= hello-jni.c[x86_64] SharedLibrary  : libhello-jni.so[x86_64] Install        : libhello-jni.so => libs/x86_64/libhello-jni.so[mips64] Compile        : hello-jni <= hello-jni.c[mips64] SharedLibrary  : libhello-jni.so[mips64] Install        : libhello-jni.so => libs/mips64/libhello-jni.so[armeabi-v7a] Compile thumb  : hello-jni <= hello-jni.c[armeabi-v7a] SharedLibrary  : libhello-jni.so[armeabi-v7a] Install        : libhello-jni.so => libs/armeabi-v7a/libhello-jni.so[armeabi] Compile thumb  : hello-jni <= hello-jni.c[armeabi] SharedLibrary  : libhello-jni.so[armeabi] Install        : libhello-jni.so => libs/armeabi/libhello-jni.so[x86] Compile        : hello-jni <= hello-jni.c[x86] SharedLibrary  : libhello-jni.so[x86] Install        : libhello-jni.so => libs/x86/libhello-jni.so[mips] Compile        : hello-jni <= hello-jni.c[mips] SharedLibrary  : libhello-jni.so[mips] Install        : libhello-jni.so => libs/mips/libhello-jni.so**** Build Finished ****
这个过程其实就是在构建原生组件并和Java应用程序打包的过程。此时在我们手机上就可以看到一行文字


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


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

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

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

六、 实例工程解析

Android.mk的内容如下

# 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-jniLOCAL_SRC_FILES := hello-jni.cinclude $(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

/* * 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();    /* 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");    }}
从上面可以看到调用了原生的stringFromJNI()方法,使用关键字native来通知Java编译器,这个是用另一种语言实现的,再通过加装共享库(static语句块)hello-jni来告诉虚拟机原生方法的具体实现。java.lang.System类提供了两个静态方法,load和loadLibrary用来运行时加载共享库。下面我们来看看具体的实现。

#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 */jstringJava_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,                                                  jobject thiz ){#if defined(__arm__)  #if defined(__ARM_ARCH_7A__)    #if defined(__ARM_NEON__)      #if defined(__ARM_PCS_VFP)        #define ABI "armeabi-v7a/NEON (hard-float)"      #else        #define ABI "armeabi-v7a/NEON"      #endif    #else      #if defined(__ARM_PCS_VFP)        #define ABI "armeabi-v7a (hard-float)"      #else        #define ABI "armeabi-v7a"      #endif    #endif  #else   #define ABI "armeabi"  #endif#elif defined(__i386__)   #define ABI "x86"#elif defined(__x86_64__)   #define ABI "x86_64"#elif defined(__mips64)  /* mips64el-* toolchain defines __mips__ too */   #define ABI "mips64"#elif defined(__mips__)   #define ABI "mips"#elif defined(__aarch64__)   #define ABI "arm64-v8a"#else   #define ABI "unknown"#endif    return (*env)->NewStringUTF(env, "Hello from JNI !  Compiled with ABI " ABI ".");}
第一个参数JNIEnv是指向可用JNI函数表的接口指针,第二个参数jobject是HelloJni类实例的java对象。最后一句代码是用c字符串创建UTF-8的Java字符串。

1楼pistolove1小时前
学习了,写的不错!谢谢分享!
  相关解决方案