JNI:Java Native Interface(Java 本地编程接口),一套编程规范,它提供了若干的 API 实现了 Java 和其余语言的通讯(主要是 C/C++)。Java 能够经过 JNI 调用本地的 C/C++ 代码,本地的 C/C++ 代码也能够调用 java 代码。Java 经过 C/C++ 使用本地的代码的一个关键性缘由在于 C/C++ 代码的高效性。java
NDK:Native Development Kit(本地开发工具),一系列工具的集合,提供了一系列的工具,帮助开发者快速开发 C/C++,极大地减轻了开发人员的打包工做。android
Ps:CMake 是 AS 2.2 以后加入的一个跨平台的安装(编译)工具,能够用简单的语句来描述全部平台的安装(编译过程),简单来讲就是简化 JNI 开发的编译步骤
ndk.dir=D\:\\workTime\\android-studio-sdk-2.3\\android-studio-sdk-2.3\\ndk-bundle sdk.dir=D\:\\workTime\\android-studio-sdk-2.3\\android-studio-sdk-2.3
#gradle:3.0.1 studio3.0 以前用 android.useDeprecatedNdk=true #gradle:3.0.1 studio3.0 以后用 android.deprecatedNdkCompileLease=1511832698813
android { ......... externalNativeBuild { cmake { path "CMakeLists.txt" } } }
CMakeLists.txt所在目录和上面path "CMakeLists.txt"相关连git
CMakeLists.txt中内容以下:github
# CMake的编译脚本配置文件 # 1. 标注须要支持的CMake最小版本 cmake_minimum_required(VERSION 3.4.1) # 2. add_library 定义须要编译的代码库 名称, 类型, 包含的源码 add_library( # Sets the name of the library. JNIControl # Sets the library as a shared library. SHARED src/main/jni/JNIControl.cpp ) # 3. find_library 定义当前代码库须要依赖的系统或者第三方库文件(能够写多个) find_library( log_lib # 指定要查找的系统库, 给一个名字 log # 真正要查找的liblog.so或者liblog.a ) # 4. target_link_libraries设置最终编译的目标代码库 target_link_libraries( JNIControl # add_library 生成的 ${log_lib} # find_library 找到的系统库 ) }
/** * <pre> * author : Wp * e-mail : 18141924293@163.com * time : 2018/11/15 * desc : * version: 1.0 * </pre> */ public class JNIUtils { static { //JNIControl 后面新建的.c 或者.cpp 文件名 在这里能够先注释掉 System.loadLibrary("JNIControl"); } public static native String printStringByJni(); }
cd app/src/main/java
javah king.bird.ndkjnidemo.JNIUtils
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class king_bird_ndkjnidemo_JNIUtils */ #ifndef _Included_king_bird_ndkjnidemo_JNIUtils #define _Included_king_bird_ndkjnidemo_JNIUtils #ifdef __cplusplus extern "C" { #endif /* * Class: king_bird_ndkjnidemo_JNIUtils * Method: printStringByJni * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_king_bird_ndkjnidemo_JNIUtils_printStringByJni (JNIEnv *, jclass); #ifdef __cplusplus } #endif #endif
#include "king_bird_ndkjnidemo_JNIUtils.h" //king_bird_ndkjnidemo_JNIUtils_printStringByJni 包名+文件名+文件内方法名 JNIEXPORT jstring JNICALL Java_king_bird_ndkjnidemo_JNIUtils_printStringByJni (JNIEnv *env, jclass jclass){ //字符串返回 return env->NewStringUTF("没想到吧!我居然会JNI了!!!"); }
package king.bird.ndkjnidemo import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) mBtnLoadNative.setOnClickListener { val jniUtils = JNIUtils.printStringByJni() mTvText.text = jniUtils } } }
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/mTvText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/mBtnLoadNative" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="LoadNativeText" app:layout_constraintTop_toBottomOf="@+id/mTvText" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" tools:ignore="HardcodedText" /> </android.support.constraint.ConstraintLayout>