很久沒有寫jni相關的項目了碉渡,閑下來復習以下,把jni項目的構建流程記錄以下怖现,為以后使用方便查閱。
1. 環(huán)境配置
1. 下載NDK
方法一:在官網(wǎng)手動下載
選擇想要下載的對應系統(tǒng)NDK版本劝评,下載完成之后,解壓到你ndk存放目錄倦淀,這個目錄后續(xù)配置環(huán)境變量需要用到
方法二:使用Android studio SDK Manager下載(Android studio 4.x)
點擊Android studio 工具欄的
File | Settings | Appearance & Behavior | System Settings | Android SDK
或者Tool|SDK Manager
打開SDK Manager 界面選擇SDK Tools 條目蒋畜,勾選NDK和CMake后點擊apply按鈕等待下載完即刻
下載完成之后,ndk的位置在你的Android sdk目錄下方撞叽,有一個ndk的文件夾姻成。
2. 配置NDK
1.配置環(huán)境變量
打開高級環(huán)境變量控制
配置環(huán)境變量,在環(huán)境變量的Path中添加ndk的路徑
我的路徑是
D:\Android\SDK\ndk\21.3.6528147
在cmd中輸入ndk-build 出現(xiàn)以下結果證明配置成功
2.已有項目中集成jni
1. 在Androidstudio中配置
打開已有項目愿棋,通過File|Project Structure|SDK location
打開SDK和NDK配置界面科展,然后在ndk中配置你剛剛下載的NDK路徑
2. 在項目app目錄下的build.gradle文件進行配置
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "com.marlon.testjni"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// 增加cmake控制屬性
externalNativeBuild {
cmake {
// 指定編譯架構 可以省略
abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
// cpp 編譯時的額外選項 可以省略
cppFlags ""
}
}
}
// 在android節(jié)點下
// 指定CMakeLists.txt路徑
externalNativeBuild {
cmake {
// 在該文件種設置所要編寫的c源碼位置,以及編譯后so文件的名字
path "src/main/jni/CMakeLists.txt"
// cmake版本 可以省略
version "3.10.2"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
2. 新建JNI folder用來放置C/C++文件和CMakeLists文件
在app/src/main目錄下將會創(chuàng)建一個jni文件夾糠雨。
3. 新建HelloJNI文件
在src/main/java文件夾下面創(chuàng)建HelloJNI.java
文件
package com.marlon.testjni;
/**
* @author by marlon
* @date on 2020/9/13.
*/
public class HelloJNI {
static {
System.loadLibrary("native-lib");
}
public static native String helloJNI();
}
4.創(chuàng)建native-lib文件
在src/main/java
目錄下創(chuàng)建native-lib.cpp
//
// Created by marlon on 2020/9/13.
//
#include <jni.h>
extern "C"
JNIEXPORT jstring JNICALL
Java_com_marlon_testjni_HelloJNI_helloJNI(JNIEnv *env, jclass clazz) {
// TODO: implement helloJNI()
return env->NewStringUTF("Hello From JNI!");
}
5. 創(chuàng)建CMakeLists文件
在src/main/jni文件中創(chuàng)建
# 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.
#指定CMake構建本地庫時所需的最小版本
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.
#將資源文件生成動態(tài)鏈接庫(so文件)的庫名稱(文件名稱:“l(fā)ib" +設置的名稱)
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
# 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.
#將所有的add_library中的庫鏈接起來才睹,有多少個add_library成的庫就將其添加到這里
#這個和add_library中的指定的so庫名稱一致
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
3.使用
package com.marlon.testjni;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.text).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//在這里直接調用
String string = HelloJNI.helloJNI();
Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
}
});
}
}
整個就是這樣