主要参考以下两篇文章:
AndroidStudio中使用FFMPEG入门
Android使用FFmpeg(二)--Android Studio配置ffmpeg
遇到的问题:
1.build.gradle
NDK不能用Android Studio自动下载的,大于r17的版本,不再支持armeabi,所以需要把Android Sudio的NDK指定为小于r17的,我使用的是 android-ndk-r14b
build.gradle的完整文件如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mm.ffmpegdemo"
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// AMEYUME Add to configure jniLibs {@
sourceSets {
main {
jniLibs.srcDirs = ['src/main/libs']
}
}
// AMEYUME @}
externalNativeBuild {
cmake {
//cppFlags ""
cppFlags "-frtti -fexceptions" // AMEYUME Modify
// 生成.so库的目标平台
abiFilters 'armeabi' // AMEYUME Add
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
2.CMakeLists.txt
(1)DIR不能使用:
set(DIR ../../../../libs)
需要修改为以下方式:
set(DIR ${CMAKE_SOURCE_DIR}/src/main/libs)
(2)Build/Make Project时提示找不到头文件:
Build command failed.
Error while executing process /home/ame/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/ame/workspaces_test/FFmpegDemo/app/.externalNativeBuild/cmake/debug/armeabi --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
FAILED: /home/ame/bin/android-ndk-r14b/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv5te-none-linux-androideabi --gcc-toolchain=/home/ame/bin/android-ndk-r14b/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot=/home/ame/bin/android-ndk-r14b/platforms/android-14/arch-arm -Dnative_lib_EXPORTS -I../../../../libs/include -isystem /home/ame/bin/android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem /home/ame/bin/android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include -isystem /home/ame/bin/android-ndk-r14b/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-integrated-as -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -MF CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -c /home/ame/workspaces_test/FFmpegDemo/app/src/main/cpp/native-lib.cpp
/home/ame/workspaces_test/FFmpegDemo/app/src/main/cpp/native-lib.cpp:5:10: fatal error: 'libavcodec/avcodec.h' file not found
#include <libavcodec/avcodec.h>
^
1 error generated.
ninja: build stopped: subcommand failed.
修改方法:
include_directories(libs/include)
需要修改为
include_directories(src/main/libs/include)
(3)CMakeLists.txt完整内容如下,新增的内容增加了注释“# AMEYUME”
# 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)
# 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)
# AMEYUME Add {@
#include_directories(libs/include)
include_directories(src/main/libs/include)
set(DIR ${CMAKE_SOURCE_DIR}/src/main/libs)
add_library(avcodec-56
SHARED
IMPORTED)
set_target_properties(avcodec-56
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavcodec-56.so)
add_library(avdevice-56
SHARED
IMPORTED)
set_target_properties(avdevice-56
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavdevice-56.so)
add_library(avformat-56
SHARED
IMPORTED)
set_target_properties(avformat-56
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavformat-56.so)
add_library(avutil-54
SHARED
IMPORTED)
set_target_properties(avutil-54
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavutil-54.so)
add_library(postproc-53
SHARED
IMPORTED)
set_target_properties(postproc-53
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libpostproc-53.so)
add_library(swresample-1
SHARED
IMPORTED)
set_target_properties(swresample-1
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libswresample-1.so)
add_library(swscale-3
SHARED
IMPORTED)
set_target_properties(swscale-3
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libswscale-3.so)
add_library(avfilter-5
SHARED
IMPORTED)
set_target_properties(avfilter-5
PROPERTIES IMPORTED_LOCATION
${DIR}/armeabi/libavfilter-5.so)
# AMEYUME Add @}
# 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
# AMEYUME Add {@
avfilter-5
avcodec-56
avdevice-56
avformat-56
avutil-54
postproc-53
swresample-1
swscale-3
# AMEYUME Add @}
# Links the target library to the log library
# included in the NDK.
${log-lib})