JNI基础知识&开发环境配置

487 阅读5分钟

JNI&NDK定义

JNI全称:Java Native Interface。它是Java本身的一种特性,用来在Java里面调用C/C++代码的。

NDK全称:Native Development Kit。是Google提供的一套工具集,可以让你其他语言(C、C++或汇编)开发 Android的 JNI。

为什么用?

JNI可以简单理解为JAVA调用C/C++代码的桥梁。

用JNI做应用开发难度要比JAVA难很多,门槛也要高很多,如果你对C/C++把握的不好应用还会出现难以发现的Bug!所以通常在对性能要求比较高才会使用。

优点:C代码直接操作内存及硬件,效率高,常用来开发引擎以及需要大量算法的功能,如:

  1. 运算效率高-->opencv-->人脸识别(最近比较火,把opencv的算法,打成so包,提供jni给安卓用, 实现人脸检测及识别等)

  2. 渲染游戏-->opengl 3D渲染引擎

  3. 音频处理

  4. 例子:智能家电,基于安卓系统,C语言编写硬件驱动,提供jni,安卓系统做上层接口操作,实现 智能控制,还有车载电脑等等

    1. 另外就是如果你想把核心的一些算法或处理逻辑保护起来,选用JNI也是一个不错的方案。需要说明的是,睿屏的白板APP就用到了JNI调用。

Demo

通过Jni调用在界面上显示“hello Maybe”。 #方法步骤:

1.Android Studio 配置

下载好NDK,并设置路径

image.png

2.新建工程HelloNDK并新建jni文件夹

新建NdkJniUtils.java

package com.example.android.hellondk;
​
/**
 * Created by Maybe on 2017/12/7.
 */public class NdkJniUtils {
    static {
        //通过此方法加载库,库的名字(JniTest)由你自己命名,但是需要与后面CMakeLists.txt文件里一致
        System.loadLibrary("JniTest");
    }
       //native关键字  自定义调用函数sayHello() 
    public native String sayHello();
}

然后在main目录下新建jni文件夹 image.png

3.生成.h头文件

没有javah命令,则用以下方法 首先terminal进入项目app\src\main路径 然后执行以下命令 javac -h jni java\com\display\helloworld\LoadJni.java 注意,进入项目app\src\main路径执行命令就意味着在该目录下生成jni文件夹,并且java\com\display\helloworld\LoadJni.java即表示文件在该目录下app\src\main\java\com\display\helloworld\LoadJni.java

有两种方法,一种是在terminal中进入到相应的路径使用命令行代码生成; 命令行命令:javah com.kissdream.androidjnitest.myJNIUtils 另一种是在Android Studio 3.0中进行配置,具体步骤如下。

(1)执行操作 File-Setting-Tools-External Toolsimage.png(2)在工程里面找到你新建的NdkJniUtils.java,执行如下图中操作。

image.png

正常情况下会在新建的jni文件夹下面产生一个后缀名为.h的头文件,打开该头文件。

image.png

4.编辑.c文件

(3)在jni文件夹里新建.c文件,我这里为main.c。将头文件中全部内容复制到main.c里并实现sayHello方法如下:(里面逻辑属于c语言内容,给我们返回一个字符串Hello Maybe)

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_android_hellondk_NdkJniUtils */#ifndef _Included_com_example_android_hellondk_NdkJniUtils
#define _Included_com_example_android_hellondk_NdkJniUtils
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_example_android_hellondk_NdkJniUtils
 * Method:    sayHello
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_example_android_hellondk_NdkJniUtils_sayHello
  (JNIEnv *env, jobject jobj){
    return (*env)->NewStringUTF(env,"Hello Maybe");
  }
​
#ifdef __cplusplus
}
#endif
#endif

5.添加CMakeLists.txt文件

在app目录下添加CMakeLists.txt文件,如图所示。 image.png 该文件里面的内容如下(只需要注意汉字注释的位置,#为注释)

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html# Sets the minimum version of CMake required to build the native library.
#CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
​
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
​
add_library( # Sets the name of the library.
      # 设置so文件名称.与上面加载的一致
       JniTest
​
       # Sets the library as a shared library.
      # 设置这个so文件为共享.
       SHARED
​
       # Provides a relative path to your source file(s).
       # 提供你C文件的路径,这里我的main.c文件放在了刚才新建jni路径里.
       src/main/jni/main.c)#此处的括号不可删除,是add_library的括号
      
​
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
​
find_library( # Sets the name of the path variable.
       log-lib
​
       # Specifies the name of the NDK library that
       # you want CMake to locate.
       log )
​
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
​
target_link_libraries( # Specifies the target library.
            # 制定目标库,此处与你加载的库名称一致.
            JniTest
​
            # Links the target library to the log library
            # included in the NDK.
            ${log-lib} )

6.修改app下build.gradle

具体修改内容分为:

  1. cmake工具 在defaultConfig节点内添加
  2. 配置CMakeLists.txt路径 在andriod节点内添加
  3. 同步(Sync Now) 此处添加的内容可不用修改,复制到你的工程即可(注意是复制添加的内容,其他依赖之类的根据你的工程而定
apply plugin: 'com.android.application'
​
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.android.hellondk"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
​
        // 1.需要添加的位置 使用Cmake工具
        externalNativeBuild {
            cmake {
                cppFlags ""
                //生成多个版本的so文件
                abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'
            }
        }
    }
    //2.需要添加的位置 配置CMakeLists.txt路径
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"  // 设置所要编写的c源码位置,以及编译后so文件的名字
        }
    }
​
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
​
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

7.生成.so文件

执行操作:Rebuild Project,正常情况下在app/build/intermediates/cmake/debug/obj路径中会生成.so文件。

image.png

#####注意:如果你之有一个文件夹里生成了.so文件,那么你需要在jni文件夹下新建Application.mk文件,该文件里只有一句话APP_ABI := all,然后重新Rebuild Project。

8.调用

代码如图不多说了,自己体会吧。

package com.example.android.hellondk;
​
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
​
/**
 * Project Name:
 * Class desc. :
 *     date    : 2017/12/7
 *    author   : Maybe
 **/
public class MainActivity extends AppCompatActivity {
    private TextView textView;
​
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
​
        textView = (TextView)findViewById(R.id.text);
        NdkJniUtils jni = new NdkJniUtils();
        textView.setText(jni.sayHello());
    }
​
}

9.结语

效果如图。

image.png