Compiling JNI with cmake in Android NDK development

Although I have been working on the development of NDK for a long time, because the project has used makefile for bottom compilation for a long time, I don't know much about the cmake compilation method officially provided by Android studio. Now I'll learn this new method, which is not fashionable, and it's also keeping pace with the times.

First, let's talk about the relevant files to be involved, the necessary C source code at the bottom, the Java / kotlin file called at the top, the build.gradle file compiled by Android studio using gradle, and the cmakelists.txt configuration file required for cmake compilation.

There is no need to elaborate on C and java files. These are normal coding processes for NDK development. Let's talk about the configuration of cmake in build.gradle. In android.externalnativebuild.cmake{}, you can use path to configure the path of cmakelists.txt, as follows:

1 externalNativeBuild { 2 cmake { 3 path "CMakeLists.txt" 4 } 5 }

At the same time, use android.defaultconfig.ndk{} to configure ABI to generate so library versions under different platforms, as follows:

As long as it is associated with the cmakelists.txt configuration file used, other compilation problems can be handled by the configuration file. For cmake configuration learning, please refer to the official website documents: https://cmake.org/cmake-tutorial/

Here, we mainly specify the method of generating so library. If it is not specified, it is generated by default under app / build / intermediates / cmake / debug / obj / ${android_abi}. You can also use the command settings:

 #设置生成的so动态库最后输出的路径 3 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI}) 

Another useful thing is to specify the header file search path. If there are many header file paths, spaces are used to separate different paths:

Note that the above configuration must be defined before addlibrary(), otherwise it will not work.

Other precautions shall be added when encountered.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>