android关联so cmake,android 引入so文件CmakeList 配置

做NDK开发的时候,很大程度的是引用成熟类库,我们做逻辑,功能,那么怎么把so文件引入到我们的android工程里面的?

目前AndroidStudio2.2之后官方推荐的是Cmake,之前是Android.mk构建文件,大概类似,我们在只讲解一下Cmake,知识总是要及时更新才是。

通常我们默认编写的时候他会自动书写Cmake

#将指定的源文件生成链接文件,然后添加到工程中去

add_library(

#设置lib的名字

native-lib

#设置lib的类型 共享型 还有STATIC、和MODULE

SHARED

#源码的相对路径

src/main/cpp/native-lib.cpp

)

#这个地方是 系统已经集成了很多lib,然后需要find以下 作为声明,表示你要用这些lib

find_library(

#自己起一个 lib的名称

log-lib

#lib的文件名

log )

#将目标文件与库文件进行链接

target_link_libraries( # Specifies the target library.

native-lib

# Links the target library to the log library

# included in the NDK.

${log-lib} )

这是系统根据我们写的C/C++文件生成的,但是我们要加入第三方so怎么配置呢?我就用FFmpeg和Libyuv.so来演示一下。

首先最大的不同是:

需要声明一下文件的基地址,这个后面地址的时候需要用到

set(distribution_DIR ../../../../libs)

然后

#添加lib,SHARED类型,是IMPORTED 引入的库

add_library( libyuv

SHARED

IMPORTED)

#设置 库的属性 里面是名称 ,属性:引入地址把我们的真实地址填写进去

set_target_properties( libyuv

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/armeabi-v7a/libyuv.so)

最后

target_link_libraries(

native-lib

#指定链接库

libyuv

# Links the target library to the log library

# included in the NDK.

${log-lib} )

这是ffmpeg和libyuv的配置

# 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

src/main/cpp/mylo.h)

# 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.

set(distribution_DIR ../../../../libs)

add_library( libyuv

SHARED

IMPORTED)

set_target_properties( libyuv

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/armeabi-v7a/libyuv.so)

add_library( avcodec

SHARED

IMPORTED)

set_target_properties( avcodec

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/armeabi-v7a/libavcodec.so)

add_library( avfilter

SHARED

IMPORTED)

set_target_properties( avfilter

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/armeabi-v7a/libavfilter.so)

add_library( avformat

SHARED

IMPORTED)

set_target_properties( avformat

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/armeabi-v7a/libavformat.so)

add_library( avutil

SHARED

IMPORTED)

set_target_properties( avutil

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/armeabi-v7a/libavutil.so)

add_library( swresample

SHARED

IMPORTED)

set_target_properties( swresample

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/armeabi-v7a/libswresample.so)

add_library( swscale

SHARED

IMPORTED)

set_target_properties( swscale

PROPERTIES IMPORTED_LOCATION

${distribution_DIR}/armeabi-v7a/libswscale.so)

include_directories(libs/include)

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 )

find_library( # Sets the name of the path variable.

android-lib

# Specifies the name of the NDK library that

# you want CMake to locate.

android )

# 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

libyuv

avcodec

avfilter

avformat

avutil

swresample

swscale

# Links the target library to the log library

# included in the NDK.

${log-lib}

${android-lib})

最后的最后不要忘记在build.gradle(app)中配置

externalNativeBuild {

cmake {

cppFlags "-frtti -fexceptions"

}

}

ndk {

//选择要添加的对应cpu类型的.so库。

abiFilters 'armeabi-v7a'

// 还可以添加 'x86', 'x86_64', 'mips', 'mips64'

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值