官方文档:https://developer.android.com/ndk/guides/cmake
1、创建项目,在首界面勾选上Include C++ support,然后点击next,直至创建完成
2、项目会默认生成jni事例代码,观察项目结构,在app/src/main下有个cpp文件夹,我们要写或者要添加的c/c++文件就写在该文件夹下。
3、打开MainActivity.java,在里面可以看到这段代码
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
其中native-lib就是我们要生成的so文件的名称,在后续配置文件中还会用到,stringFromJNI就是调用c/c++的方法,我们打开app/src/main/cpp/native-lib.cpp文件,就会在其中发现有个Java_com_lianluo_jnitest_MainActivity_stringFromJNI方法,就是与之对应的。现在我们自己写个native方法,public native String helloWorld();此时我们发现helloWorld方法名是红色的,这是把鼠标指针移上去,然后点击alt+enter键,选择create function Java_com_lianluo_jnitest_MainActivity_helloWorld,这时会在native-lib.cpp文件中自动生成对应的c/c++方法。
4、打开app下的CMakeLists.txt文件,这个是进行配置cmake的,详细的配置请看官网,这里简单的写一下要注意的地方
# 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.
//这里要写上我们要生成的so文件名称,要与MainActivity.java里的名称对应
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
//这里要写上我们放在项目里的c/c++文件
src/main/cpp/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.
//这里要写上我们要生成的so文件名称,要与上面的名称对应
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
4、打开app下build.gradle,将Cmake配置到gradle中
要在
android {
compileSdkVersion 27
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
...
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
5、编译so文件,当写好代码后,我们clean项目,然后在工具栏build里面选择Make Project,这是在app的build/intermediates/cmake/debug(release)/obj文件夹下生成对应的so文件
注意:使用cmake编译so文件时,绝对不能有中文路径,否则会报这个错误
Build command failed.
Error while executing process /Users/krubo/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/krubo/Documents/工作/项目代码/AndroidStudioProjects/JniTest/app/.externalNativeBuild/cmake/debug/x86_64 --target native-lib}
ninja: warning: premature end of file; recovering
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
ninja: build stopped: Error writing to deps log: No such file or directory.