深入理解Android(1)——理解Android中的JNI(上) - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

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

发表时间:2020-10-19

发布人:葵宇科技

浏览次数:32


我参加了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技巧来应用已有的模块。
[img]http://img.blog.csdn.net/20150104210435843?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

三、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-r10c
export PATH=${PATH}:${ANDROID_NDK_HOME}
留意:后面的地址是你解压后的目次
关于膳绫擎部分开辟对象扼要介绍:
1、Apache Ant,是一个将软件编译、测试、安排等步调联系袈溱一路加以主动化的一个对象,大年夜多用于Java情况中的软件开辟。
2、NDK是Android原生开辟对象包,可以支撑C/C++等原生编程说话开辟Android应用,它供给头文件、库和交叉编译对象链。

四、第一个示例法度榜样


转载请解释出处:http://blog.csdn.net/dawanganban
1、为eclipse指定NDK路径
[img]http://img.blog.csdn.net/20150104212549675?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
2、导入Android NDK中的示例代码(导入hello-jni工程),做过Android开辟的同伙应当很熟悉,这里就不烦琐了。
[img]http://img.blog.csdn.net/20150104212738236?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
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应用法度榜样打包的过程。此时在我们手机上就可以看到一行文字
[img]http://img.blog.csdn.net/20150104220026008?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

五、项目构造及重要目次介绍


[img]http://img.blog.csdn.net/20150104220319324?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
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-jni
LOCAL_SRC_FILES := hello-jni.c

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目次,如下:
[img]http://img.blog.csdn.net/20150104222306062?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF3YW5nYW5iYW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
感化是清除除了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
 */
jstring
Java_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字符串。

相关案例查看更多