iOS技术文章

Android Studio CMakeList链接自定义动态库

2016-12-16  本文已影响2040人  熊皮皮

Android Studio 2.2支持CMakeList编译脚本,这是个很不错的特性。本人并不熟悉CMakeList,只是不想再每次手动执行ndk-build、拷贝动态库到指定目录的shell脚本。
在向CMakeList指定链接自定义动态库时遇到了需要同时指定libs及src/main/libs才能编译成本的问题。


同时指定libs及src/main/libs

起初以为是gradle命令写错了。经测试,发现srcDir指定为libs时,app层级下的libs和src/main层级下的libs都能被拷贝到apk中。


疑似gradle引发的问题

如果删除src/main层级下的libs、只有app/libs时,则编译报错找不到src/main/libs中的动态库。


只有app/libs时链接失败

只用src/main层级下的libs,删除app/libs时,打包的apk找不到自定义的动态库。


只用src/main层级下的libs

为避免歧义,建议删除build.gradle中的sourceSets与拷贝预编译库相关的部分。

经检查,CMakeList指定的set_target_properties语句表明了动态库的路径,也是前面失败的根本原因。


set_target_properties语句导致的失败

修复后则顺利通过编译。

链接静态库只需修改add_library语句为STATIC IMPORTED。另外,指定链接库时容易出现yourLib-NOFOUND问题,原因是必须按语法要求使用前面add_library语句引入的变量名,而非直接填写。示例如下

add_library(libyuv SHARED IMPORTED)
set_target_properties( libyuv 
    PROPERTIESIMPORT_LOCATION
    ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libyuv.so )

target_link_libraries( ${libyuv})
# 下面是错误的写法
# target_link_libraries(libyuv)

完整示例如下所示。

# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)

####################################################################################################
set(ffmpeg-headers ${CMAKE_SOURCE_DIR}/src/main/cpp/ffmpeg-header)
include_directories(${ffmpeg-headers})

set(PNG-headers ${CMAKE_SOURCE_DIR}/src/main/cpp/PNG)
include_directories(${PNG-headers})

set(include-headers ${CMAKE_SOURCE_DIR}/src/main/cpp/include)
include_directories(${include-headers})

set(SDL-headers ${CMAKE_SOURCE_DIR}/src/main/SDL/include)
include_directories(${SDL-headers})

#---------------------------------------------------------------------------------------------------

add_library(libffmpeg SHARED IMPORTED)
set_target_properties( libffmpeg
                       PROPERTIES
                       IMPORT_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libffmpeg.so )

add_library(libyuv SHARED IMPORTED)
set_target_properties( libyuv
                       PROPERTIES
                       IMPORT_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libyuv.so )

add_library(libpng STATIC IMPORTED)
set_target_properties( libpng
                       PROPERTIES
                       IMPORT_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libpng.a )
####################################################################################################

# 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 it 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).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# 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}
                       ${libffmpeg}
                       ${libyuv}
                       ${libpng}
                       )
上一篇下一篇

猜你喜欢

热点阅读