(二)NDK 開發之 CMakeLists.text 使用總結

在我們創建項目的時候,如果勾選 Include C++ Support ,就會在 main 的同級目錄下生成一個CMakeLists.text

下面來一一介紹

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

我們可以直接翻譯來看:

# 有關使用Android工作室使用CMAGE的更多信息,
# 閱讀文檔: https://d.android.com/studio/projects/add-native-code.html

# 設置創建本地庫所需的最小CMake版本

cmake_minimum_required(VERSION 3.4.1)

# 創建和命名庫, 將其設置爲STATIC 或者SHARED, 
# 並提供其源代碼的相對路徑。
# 您可以定義多個庫,CMake爲您構建它們。
# 用Apk自動打包共享庫.

add_library( # 設置庫的名稱.
             native-lib

             # 將庫設置爲共享庫.
             SHARED

             # 爲源文件提供相對路徑.
             src/main/cpp/native-lib.cpp
             )

# 搜索指定的預構建庫並將路徑存儲爲變量。
# 因爲CMake默認包含搜索路徑中的系統庫
# 您只需指定要添加的公共NDK庫的名稱即可.
# CMake 在完成構建之前驗證該庫是否存在

find_library( # 設置路徑變量的名稱.
              log-lib

              # 指定希望CMake定位的NDK庫的名稱.
              log )

# 指定庫CMake 應該鏈接到目標庫.
# 你可以鏈接多個庫 ,作爲在這個構建腳本中定義的庫,
# 預構建的第三方庫或系統庫.

target_link_libraries( # 指定目標庫.
                       native-lib

                       # 將目標庫鏈接到日誌庫
                       # 包含在NDK中.
                       ${log-lib} )

解析:

1、制定CMake 所需的最小版本,有系統自動生成,無需更改
    cmake_minimum_required(VERSION 3.4.1)

 

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 :

生成動態庫的名稱

SHARE:

設置鏈接庫的類型, 分爲 :SHARE 動態鏈接庫,SATTIC 靜態鏈接庫

src/main/cpp/native-lib.cpp:

你寫的c/c++ 代碼的相對路徑。目前我的cpp 目錄中有個natice-lib.cpp文件。如果,我又添加了一個 hello.cpp,則直接在下面添加、

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

             SHARED

             src/main/cpp/native-lib.cpp
             src/main/cpp/hello.cpp
             )

 

3、find_library():用來讓我們加一些編譯本地NDK庫的時候所用的到一些依賴庫.

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:

我們自己的庫,和add_library 中的名稱一致

${log-lib}:

需要關聯的庫的名稱。

二、使用cmake  生成so庫

CMakeLists.text 中添加 如下代碼: 

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

編譯運行後,會在目錄中直接生成 .so 

還可以 在app 下的  build.gradle中指定abi abiFilters

    defaultConfig {

       ......
       ......
       ......

        externalNativeBuild {
            cmake {
                cppFlags ""
                abiFilters 'x86','armeabi-v7a';
            }
        }
    }

如果不指定,會生成所有當前支持的 abi庫

指定之後,只會生成響應版本的abi

 

注: 參考借鑑:https://blog.csdn.net/hktkfly6/article/details/79593127

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章