AndroidStudio使用预编译FFmpeg库结合ndk开发视频项目踩坑记录

更新时间:2018-08-07 09:58:00 点击次数:2165次

现状: 
1 已有预编译好的libffmpeg.so和ffmpeg头文件。 
2 libffmpeg.so对应armeabi的abi。 
3 已有对ffmpeg方法调用的C++代码。

需求 
在AndroidStudio上集成进上述代码实现jni功能。

项目结构: 
这里写图片描述 
C源码在cpp目录。

include下是ffmpeg的头文件,以及功能代码的头文件。

剩下的是实现功能的C++代码。

这里写图片描述 
jnilibs下是编译好的ffmpeg动态库,只有armeabi平台的。

这里写图片描述 
使用CMakeLists来配置jni。 
文件内容如下:

# 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/CLock.cpp ///隐去其他自己写的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.

add_library( ffmpeg
             SHARED
             IMPORTED )
set_target_properties( # Specifies the target library.
                       ffmpeg

                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION

                       # Provides the path to the library you want to import.
                       ${CMAKE_SOURCE_DIR}/src/main/jniLibs/armeabi/libffmpeg.so )


# Specifies a path to native header files.
include_directories(src/main/cpp/include/ src/main/cpp/include/ffmpeg)

target_link_libraries( # Specifies the target library.
                       native-lib
                       ffmpeg
                       # Links the target library to the log library # included in the NDK.
                       ${log-lib} )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

问题1: 
运行编译错误,出现类似“/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so’, missing and no known rule to make it”

原因:预编译库配置路径不合适

按照官网上的写法 
这里写图片描述 
预编译库似乎可以放到工程的任何目录。

然而最终发现需要放到/src/main/jniLibs下面。 
并且官网提供了${ANDROID_ABI}表达式指定在调试设备所需要的abi目录下查找这个动态库,如果当前只有一个对应armeabi的动态库,则需要写死这个表达式。如 
这里写图片描述 
对应的就是这个文件 
这里写图片描述

然后一顿clean ,sync,build 
这里写图片描述 
然后运行就可以解决问题1。

问题2: 
运行编译错误,出现各种类似“1 error generated…”,”FAILED: D:\NDK\android-ndk-r13\toolchains\llvm\prebuilt…” 
之类的错误信息。 
这里写图片描述

解决方法: 
在AndroidStudio 中选择File->Project Structure… 
在SDK Location选项下,选择别的版本的NDK。

这里写图片描述

系统默认的是AndroidSDK安装目录下的ndk-bundle目录。 
我还下载了几个版本的ndk,但在写这篇文章时,只有使用ndk-r16b这个版本可以编译成功。 
这里写图片描述

设置完后,同样一顿clean,refresh,build。

问题3: 
编译成功,安装成功,app在操作到需要到ffmpeg这块功能的时候闪退。 
Logcat出现类似:“java.lang.UnsatisfiedLinkError: dlopen failed: could not load library “libffmpeg.so” needed by “libnative-lib.so”; caused by library “libffmpeg.so” not found” 
的错误日志。

解决方法: 
在module的build.gradle配置abiFilter 
这里写图片描述

如果没有上述配置由于fffmpeg的预编译动态库手头只有支持armeabi的so文件,在abi为armeabi-v7a的手机上运行,so文件没有在apk的lib内生成。

通过AndroidStudio的Build->Analyze Apk… 
这里写图片描述

可以查看到生成的apk包内容 
这里写图片描述 
可以看到libffmpeg.so并没有在armeabi-v7a目录下。当armeabi-v7a机型的手机运行时就找不到动态库。

当设置了abiFilter后,查看生成的Apk包,结构如下: 
这里写图片描述 
lib下只有一个armeabi目录,里面有依赖的ffmpeg动态库和自己编辑的动态库。其他机型手机可以正常调用。

本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!