(二)NDK 開發之 CMakeLists.text 使用總結
阿新 • • 發佈:2018-11-06
在我們建立專案的時候,如果勾選 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