Android-CMakeLists.txt 連結第三方庫(動態或者靜態庫)到自己的生成庫中
阿新 • • 發佈:2019-01-31
最近在做關於NDK開發的專案,編譯方式通過cmake。其中一個就是要將第三方動態庫連結到自己的動態庫最終生成一個動態庫供他人呼叫,這個折騰了好久,終於搞好記錄下筆記,免得以後再踩坑,有同樣需求的童鞋可以參考,有錯誤請指出。
多的不說,上程式碼.
1.首先看目錄結構:第三方庫放在jniLibs下,並建立對應的CUP平臺目錄。標頭檔案隨便放:
2.上CMakeLists.txt內容,有兩個方式,這裡不會一一解釋,有註釋,看不懂cmake常用名詞請自覺Google或者百度。
第一種方式:
# 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) message(STATUS "******************************************************************") message(STATUS "CMAKE_SOURCE_DIR->" ${CMAKE_SOURCE_DIR}) message(STATUS "PROJECT_SOURCE_DIR->)" ${PROJECT_SOURCE_DIR}) message(STATUS "******************************************************************") #1.搜尋原始檔並賦值給變數名 aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app CLIENT_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/user/source USER_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/user USER_OP_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/device/source DEVICE_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/device DEVICE_OP_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/event/source EVENT_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/event EVENT_OP_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/tools TOOLS_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/helper HELPER_SRC) #2.設定原始檔到統一的變數 set(DIR_SRCS ${CLIENT_SRC} ${USER_SRC} ${USER_OP_SRC} ${DEVICE_SRC} ${DEVICE_OP_SRC} ${EVENT_SRC} ${EVENT_OP_SRC} ${TOOLS_SRC} ${HELPER_SRC}) #3.設定第三方庫標頭檔案所在位置 include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/thirdInclude/) #4.設定第三方庫頭庫所在位置 link_directories(${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/) #5.對應的庫 link_libraries(SKYhttpClient.so SKYmxml.so) # 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. #6.建立動態庫 add_library( # Sets the name of the library. skyqcloud-sdk # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). ${DIR_SRCS} ${CMAKE_SOURCE_DIR}/src/main/cpp/skyqc_sdk_native.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. #7.連結需要的第三方動態庫 target_link_libraries( # Specifies the target library. skyqcloud-sdk #the third library SKYhttpClient SKYmxml # Links the target library to the log library # included in the NDK. ${log-lib} )
第二種方式連結第三方動態庫:
# 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) message(STATUS "******************************************************************") message(STATUS "CMAKE_SOURCE_DIR->" ${CMAKE_SOURCE_DIR}) message(STATUS "PROJECT_SOURCE_DIR->)" ${PROJECT_SOURCE_DIR}) message(STATUS "******************************************************************") #1.搜尋原始檔並賦值給變數名 aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app CLIENT_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/user/source USER_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/user USER_OP_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/device/source DEVICE_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/device DEVICE_OP_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/event/source EVENT_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/event EVENT_OP_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/tools TOOLS_SRC) aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/helper HELPER_SRC) #2.設定原始檔到統一的變數 set(DIR_SRCS ${CLIENT_SRC} ${USER_SRC} ${USER_OP_SRC} ${DEVICE_SRC} ${DEVICE_OP_SRC} ${EVENT_SRC} ${EVENT_OP_SRC} ${TOOLS_SRC} ${HELPER_SRC}) #3.設定第三方庫標頭檔案所在位置 include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/thirdInclude/) #4.匯入第三方動態庫 # 新增第三方動態庫 add_library(SKYhttpClient SHARED IMPORTED) # 設定第三方動態庫屬性(儲存位置) armeabi-v7a set_target_properties(SKYhttpClient PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libSKYhttpClient.so) # 新增第三方動態庫 add_library(SKYmxml SHARED IMPORTED) # 設定第三方動態庫屬性(儲存位置) set_target_properties(SKYmxml PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libSKYmxml.so) # 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. #6.建立動態庫(自己將要生成的動態庫) add_library( # Sets the name of the library. skyqcloud-sdk # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). ${DIR_SRCS} ${CMAKE_SOURCE_DIR}/src/main/cpp/skyqc_sdk_native.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. #6.連結需要的第三方動態庫 target_link_libraries( # Specifies the target library. skyqcloud-sdk #the third library SKYhttpClient SKYmxml # Links the target library to the log library # included in the NDK. ${log-lib} )
3.注意事項
(1)第三方動態庫存放工程目錄時,要將對應平臺CUP庫檔案分開對應存放,我現在只有個armeabi-v7a,如果有armeabi或者
x86,就要建立對應的目錄存放,否則編譯時會報錯生成對應動態庫有問題。
(2)build.gradle 配置:
over