目標(biāo)
用CMake方法實(shí)現(xiàn)在java中調(diào)用本地C/C++文件中的方法痹愚,并生成相應(yīng)so庫(kù)導(dǎo)出腻异。
實(shí)現(xiàn)步驟梳理
- 在需要調(diào)用本地方法的java文件中加載so庫(kù)窍株,并聲明本地函數(shù)硬纤。
- 新建與java同級(jí)的cpp文件夾膳灶,在其中新建c/c++文件咱士,實(shí)現(xiàn)本地方法。
- 新建CMakeList.txt文件轧钓,在其中作相應(yīng)so庫(kù)名稱序厉、C文件路徑等相關(guān)配置。
- 在build.gradle中配置CMakeList.txt文件路徑毕箍、ABI類(lèi)別等弛房。
- 編譯運(yùn)行,在app/build/intermediates/cmake/debug/obj路徑下即可找到生成的so庫(kù)而柑。
具體實(shí)現(xiàn)
1.在需要調(diào)用本地方法的java文件中加載so庫(kù)文捶,并聲明本地函數(shù)。
package com.example.taoying.testndkapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");//加載so庫(kù)牺堰,待加載的so庫(kù)的名稱為libnative-lib.so拄轻。
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNIwithp("11"));
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();//本地方法用native修飾
public native String stringFromJNIwithp(String a);
}
注意兩點(diǎn):
- System.loadLibrary("native-lib");加載so庫(kù),so庫(kù)的名稱為libnative-lib.so伟葫。
- 本地函數(shù)用native修飾恨搓。
2.新建與java同級(jí)的cpp文件夾,在其中新建c/c++文件筏养,實(shí)現(xiàn)本地方法斧抱。
native-lib.cpp
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_taoying_testndkapp_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
extern "C" JNIEXPORT jstring JNICALL Java_com_example_taoying_testndkapp_MainActivity_stringFromJNIwithp
(JNIEnv * env, jobject /* this */, jstring jstring1){
std::string hello = "stringFromJNIwithp";
return env->NewStringUTF(hello.c_str());
}
- 本地函數(shù)名的格式需要遵循如下規(guī)則:Java_包名類(lèi)名方法名,否則該方法將無(wú)法被找到渐溶。
- JNIEnv* 表示一個(gè)指向JNI環(huán)境的指針辉浦,可以通過(guò)他來(lái)訪問(wèn)JNI提供的接口方法。
- jobject表示Java對(duì)象中的this(如果該方法是非靜態(tài)方法)或者Class對(duì)象(如果該方法是static方法)茎辐。
- JNIEXPORT和JNICALL是JNI中定義的宏宪郊,可以在jni.h這個(gè)頭文件中找到。JNIEXPORT確保函數(shù)在符號(hào)表中可見(jiàn)拖陆, JNICALL確保函數(shù)使用正確的調(diào)用約定弛槐。
3.新建CMakeList.txt文件,在其中作相應(yīng)so庫(kù)名稱依啰、C文件路徑等相關(guān)配置乎串。
CMakeLists.txt
# 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_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表示生成靜態(tài)鏈接庫(kù)libnative-lib.so
//native-lib 即為生成庫(kù)的名稱
//SHARED 表示生成動(dòng)態(tài)庫(kù)libnative-lib.so, STATIC表示生成靜態(tài)庫(kù)libnative-lib.a速警。
//native-lib.cpp 表示c/c++源文件的路徑叹誉。
//${CMAKE_SOURCE_DIR}:表示CMakeLists.txt的當(dāng)前文件夾路徑鸯两。
add_library( # Sets the name of the library.
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.
//需要加載的so庫(kù),需要加載多個(gè)時(shí)用空格" "連接
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
這里有更多關(guān)于CMakeList.txt的編寫(xiě)規(guī)則的介紹。
4.在build.gradle中配置CMakeList.txt文件路徑长豁、ABI類(lèi)別等钧唐。
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.taoying.testndkapp"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
//代表編譯C++代碼時(shí)用的編譯選項(xiàng)
//如cppFlags "-frtti -fexceptions" 配置支持RTTI和C++異常處理(這個(gè)非必須 也可以不配)
cppFlags ""
}
}
ndk {
//配置ABI類(lèi)別,注意這里配置的Cpu類(lèi)型在編譯后會(huì)生成相應(yīng)的so動(dòng)態(tài)庫(kù)蕉斜,不配置默認(rèn)生成所有
//生成的so庫(kù)在C:\Users\taoying\Desktop\TestNdkApp\app\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so路徑下
abiFilters "arm64-v8a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
// 指定CMake編譯配置文件路徑
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
//可選配置
/*sourceSets.main {
//默認(rèn)so庫(kù)放在src/main/jniLibs目錄下逾柿,此時(shí)可以制定新的存放so庫(kù)的目錄
jniLibs.srcDirs = ['libs']
}*/
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
5. 編譯運(yùn)行,在app/build/intermediates/cmake/debug/obj路徑下即可找到生成的so庫(kù)宅此。
更多JNI&NDK系列文章机错,參見(jiàn):
JNI&NDK開(kāi)發(fā)最佳實(shí)踐(一):開(kāi)篇
JNI&NDK開(kāi)發(fā)最佳實(shí)踐(二):CMake實(shí)現(xiàn)調(diào)用已有C/C++文件中的本地方法
JNI&NDK開(kāi)發(fā)最佳實(shí)踐(三):CMake實(shí)現(xiàn)調(diào)用已有so庫(kù)中的本地方法
JNI&NDK開(kāi)發(fā)最佳實(shí)踐(四):JNI數(shù)據(jù)類(lèi)型及與Java數(shù)據(jù)類(lèi)型的映射關(guān)系
JNI&NDK開(kāi)發(fā)最佳實(shí)踐(五):本地方法的靜態(tài)注冊(cè)與動(dòng)態(tài)注冊(cè)
JNI&NDK開(kāi)發(fā)最佳實(shí)踐(六):JNI實(shí)現(xiàn)本地方法時(shí)的數(shù)據(jù)類(lèi)型轉(zhuǎn)換
JNI&NDK開(kāi)發(fā)最佳實(shí)踐(七):JNI之本地方法與java互調(diào)
JNI&NDK開(kāi)發(fā)最佳實(shí)踐(八):JNI局部引用、全局引用和弱全局引用
JNI&NDK開(kāi)發(fā)最佳實(shí)踐(九):調(diào)試篇