Android中CMakeList的编写方法

CMake是一个跨平台的安装编译工具,可以用简短的语句来描述各个平台的安装编译过程。

在CMakeList开头应设置好工程的jni文件目录,比如:

set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLibs)

如果有用到第三方库,这里还需要引入第三方头文件:

include_directories(src/main/jniLibs/include/ffmpeg)

如果要引用第三方so库,并且有多个平台的so文件,引用方法如下:

add_library(
    avcodec-lib
    STATIC
    IMPORTED)
set_target_properties( avcodec-lib
                       PROPERTIES IMPORTED_LOCATION
                       ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavcodec-56.so)

add_library是添加库的函数,里面有三个参数,第一个是引入的库别名;第二个是库的类型,即静态库还是动态库,第二个参数一般都是"SHARED;第三个是通过什么方式引入,一般都是"IMPORTED"。
add_library后,要设置so库的详细路径,通过set_target_properties()函数来设置,第一个参数也是引入库的别名;第二个参数是固定的,都是"PROPERTIES IMPORTED_LOCATION"表示库的引入方式,都是通过本地引入;第三个参数是库的具体路径。

如果我们项目中自己的c/c++文件编译,也是通过add_library函数添加,但是写法不同:

add_library( # Sets the name of the library.
             humusic

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/humusic.c)

如果项目中有用到系统的.so库就不能用add_library了,需要用到"find_library()"函数,例如添加系统日志库:

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 )

target_link_libraries()这个函数是将所有的库链接起来。写法如下:

target_link_libraries( # Specifies the target library.
                       humusic
                       avcodec-lib
                       avdevice-lib
                       avfilter-lib
                       avformat-lib
                       avutil-lib
                       postproc-lib
                       swresample-lib
                       swscale-lib
                       yuv-lib
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

把要链接的库别名都写到这里面就可以了,如果是系统的库,要用${库的名字}的方式。

整个CMakeList如下:

# 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.
set(distribution_DIR ${CMAKE_SOURCE_DIR}/src/main/jniLib)
include_directories(src/main/cpp/include/ffmpeg)
include_directories(src/main/cpp/include/libyuv)
add_library( # Sets the name of the library.
        HmusicPlayer

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/cpp/HmusicPlayer.c)


add_library(
        avcodec
        STATIC
        IMPORTED
)
set_target_properties(
        avcodec
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavcodec-56.so
)


add_library(
        avdevice
        STATIC
        IMPORTED
)
set_target_properties(
        avdevice
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavdevice-56.so
)

add_library(
        avfilter
        STATIC
        IMPORTED
)
set_target_properties(
        avfilter
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavfilter-5.so
)

add_library(
        avformat
        STATIC
        IMPORTED
)
set_target_properties(
        avformat
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavformat-56.so
)

add_library(
        avutil
        STATIC
        IMPORTED
)
set_target_properties(
        avutil
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libavutil-54.so
)

add_library(
        postproc
        STATIC
        IMPORTED
)
set_target_properties(
        postproc
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libpostproc-53.so
)

add_library(
        swresample
        STATIC
        IMPORTED
)
set_target_properties(
        swresample
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswresample-1.so
)

add_library(
        swscale
        STATIC
        IMPORTED
)
set_target_properties(
        swscale
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libswscale-3.so
)

add_library(
        yuv
        STATIC
        IMPORTED
)
set_target_properties(
        yuv
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libyuv.so
)
# 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.
        HmusicPlayer
        avcodec
        avdevice
        avfilter
        avformat
        avutil
        postproc
        swresample
        swscale
        yuv
        -landroid
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值