android CMakeLists.txt文件详解

很多时候时候我们不得不感叹工具的强大是我们的生活和和工作变的越来越简单。在android 的ndk 开发中我们历经了有命令编译到eclipse 在studio 的工具的变迁,这一切都是朝着开发流程越来越简单,开发效率越来越高的方向发展。
今天学习了如何在Studio 使用CMakeLists.txt配置文件进行NDK 开发,感觉流程简单了很多,今天想详细介绍换一下CMakeLists.txt的基本使用。因为在实际的开发中对应JNI的使用不会简单的时候一个c/c++文件,有时候会用到多个c/c++ 文件,甚至会生成多个so文件。所以我要分析一下
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) 设置cmake最低版本

# 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文件名
             hello

             # Sets the library as a shared library. 设置库的类型,一种是静态的STATIC .a文件,一种是动态SHARED so文件
             SHARED

             # Provides a relative path to your source file(s). 要编译的C/C++文件,我这里编译的是两个文件
             src/main/cpp/native-lib.cpp
             src/main/cpp/hello.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. 指定连接的目标库
                       hello
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

上面对CMakeLists文件中的关键代码做了简单注释。
如何生成多个so文件
方法一:
直接在app/下的CMakeLists.text 的末尾添加要生成的so 文件名,资源路径,添加内容如下

add_library(test-lib SHARED src/main/cpp/test.cpp)

target_link_libraries(test-lib ${log-lib} )

方法二 创建一个新的CMakeLists.txt文件
文件内容如下 和上面的内容是一样的,然后在app下的CMakeLists.txt 末尾添加

ADD_SUBDIRECTORY(src/main/cpp/one) 括号内的内容是我们创建的CMakeLists.txt的路径

好了,今天先介绍到这里,关于更多android jni 开发的知识我还会继续探索。

猜你喜欢

转载自blog.csdn.net/songmingzhan/article/details/80720903
今日推荐