I wanted to make a Demo for JNI practice. Since I was using a previously created project and C++ was not included when creating the project, I reviewed the article "Using CMake for JNI Development (Android Studio)" in Android Notes . Following the steps in this article, it was possible before, but a configure Error occurred during compilation. Finally, I found the Android studio synchronization project failed: External Native Build Issues: Error configuring , which said it was a problem with the gradle plug-in version, so I changed the gradle The plug-in version solves the problem.
The current environment and implementation steps are re-recorded here for comparison and update in the future.
environment:
Android Studio 3.1.4
gradle plugin is 3.2.1 , which can be viewed in build.gradle in the root directory of the project
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
}
Add steps
1. Add CMakeLists.txt
2. Module configuration CMakeLists.txt
3. Write C/CPP file
4.java call
It is assumed here that the C++ file JniLib.cpp has been written under src/main/cpp
1. Create a new CMakeLists.txt under the project module.
Here we take the app module as an example, as shown below:
The content of CMakeLists.txt is as follows:
# 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. The configuration module supports jni calling.
Right-click on the project module, select Link C++ Project with Gradle, and
select the newly configured CMakeLists.txt file in the pop-up prompt box:
Wait for the compilation to complete, and the following content will be generated in the android node of the module.
android {
...
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
}
Note: For gradle plugins below 3.2.0, the externalNativeBuild node is added, and a configure error will be reported during compilation.
4. Write the C/CPP file
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());
}
Note: Each JNI method requires extern "C" JNIEXPORT <JNI type> , JNIEXPORT <JNI type> is used to control the return type of the function, otherwise an error will be reported
5.java call
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();
}
reference
Android Notes: Using CMake for JNI Development (Android Studio)