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>
複製代碼