现状:
1 已有预编译好的libffmpeg.so和ffmpeg头文件。
2 libffmpeg.so对应armeabi的abi。
3 已有对ffmpeg方法调用的C++代码。
需求
在AndroidStudio上集成进上述代码实现jni功能。
include下是ffmpeg的头文件,以及功能代码的头文件。
剩下的是实现功能的C++代码。
jnilibs下是编译好的ffmpeg动态库,只有armeabi平台的。
# 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:
运行编译错误,出现类似“/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…”
之类的错误信息。
或者:“Build command failed.
Error while executing process D:\”…
解决方法:
在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动态库和自己编辑的动态库。其他机型手机可以正常调用。