想弄個Demo進行JNI練習,由於使用的是之前創建的項目,且創建項目時,並沒有包含C++,然後就回顧了Android筆記之使用CMake進行JNI開發(Android Studio)這篇文章。按着這個文章步驟做,以前是可以的,但是在編譯時報了configrure Error,最後找到了Android studio 同步工程失敗:External Native Build Issues: Error configuring,說是gradle插件版本的問題,按着改了gradle插件版本就解決了問題。
這裏重新記錄下當前環境和實現步驟,以後好作個對比更新。
環境:
Android Studio 3.1.4
gradle插件是3.2.1 ,可在項目的根目錄的build.gradle中查看
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1' // gradle插件版本
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
添加步驟
1.添加CMakeLists.txt
2.模塊配置CMakeLists.txt
3.編寫C/CPP文件
4.java調用
這裏假設已經在src/main/cpp下編寫好了C++文件JniLib.cpp
1.在工程模塊下新建一個CMakeLists.txt
這裏以app模塊爲例,如下圖:
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( # Sets the name of the library.
#庫名,可修改
JniLib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# c++源代碼文件路徑
src/main/cpp/JniLib.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.
JniLib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
2.配置模塊支持jni調用
對項目模塊右鍵,選擇Link C++ Project with Gradle
在彈出的提示框中選擇剛新建配置的CMakeLists.txt文件:
等待編譯完成,會在模塊的android節點中生成如下內容
android {
...
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
}
注意: gradle插件3.2.0以下,添加添加了externalNativeBuild節點,編譯時會報configure error。
4.編寫C/CPP文件
JniLIb.cpp
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring // 指定方法的返回類型,不設置的話會報錯
JNICALL
Java_com_sharedream_demo_activity_JNIDemoActivity_getMessage(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
注意: 每個JNI方法,都需要 extern “C” JNIEXPORT <JNI類型> ,JNIEXPORT <JNI類型> 用於控制函數的返回類型,否則會報錯
5.java調用
public class JNIDemoActivity extends BaseActivity {
@BindView(R.id.tv_jni_message)
TextView tvJNIMessage;
static {
// 1.導入對應的so庫
System.loadLibrary("JniLib");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 3.調用
String message = getMessage();
Logger.d("message = " + message);
tvJNIMessage.setText(message);
}
@Override
protected int getLayoutId() {
return R.layout.activity_jni_demo;
}
// 2.聲明本地方法
public static native String getMessage();
}