Android-CMakeLists.txt 链接第三方库(动态或者静态库)到自己的生成库中

最近在做关于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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值