AndroidStudio中CMakeLists配置

AndroidStudio自动构建的CMakeLists文件内容如下:

# 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)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/jniLibs/${ANDROID_ABI})

# 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.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        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.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

1.cmake_minimum_required(VERSION 3.4.1)
用来声明编译本地库文件时需要的Cmake最小版本,在AndroidStudio构建版本时会自动生成;
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY {PROJECT_SOURCE_DIR}/jniLibs/{ANDROID_ABI})
将libxx.so生成到jniLibs文件夹下

2.add_library()

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/cpp/native-lib.cpp)

native-lib : 编译本地本地lib生成的库文件的名称
SHARED : (生成的库种类)SHARED表示编译生成的是动态链接库
src/main/cpp/native-lib.cpp : 表示编译文件的相对路径,这里可以是一个文件的路径也可以是多个文件的路径

3.find_library()

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)

这个的作用是声明编译本地库的时候,需要用的到一些依赖库.
log-lib 是这个库的别名,方便我们以后引用
log 是我们调试的时候打印log的一个库

4.target_link_libraries()

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

目的是用来关联我们本地的库跟第三方的库.这里就是把native-lib库和log库关联起来.

推荐阅读更多精彩内容