JNI の練習用にデモを作りたかったのですが、以前に作成したプロジェクトを使用していて、プロジェクト作成時に C++ が含まれていなかったため、 Android Notes の記事「Using CMake for JNI Development (Android Studio)」を見直しました。この記事の手順に従うと、以前は可能でしたが、コンパイル中に構成エラーが発生しました。最後に、Android Studio の同期プロジェクトが失敗したことがわかりました: 外部ネイティブ ビルドの問題: 構成エラー。これは、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 呼び出し
ここでは、C++ ファイル JniLib.cpp が src/main/cpp に記述されていると仮定します。
1. プロジェクト モジュールの下に新しい CMakeLists.txt を作成します。
ここでは、以下に示すように、アプリ モジュールを例として取り上げます
。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'
}
}
}
注: 3.2.0 より前の Gradle プラグインの場合、externalNativeBuild ノードが追加され、コンパイル中に構成エラーが報告されます。
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 type>が必要です。JNIEXPORT <JNI type> は関数の戻り値の型を制御するために使用されます。そうでない場合は、エラーが報告されます。
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();
}