本文参考博客:https://blog.csdn.net/leixiaohua1020/article/details/47008825
在上面的博客中,雷神使用的Eclipse,也就是需要自己手动创建Android.mk文件和ndk命令来编译so,本文将基于AndroidStudio3.1.4以及CMake来进行NDK开发。
Android中调用FFmpeg类库主要分为下面几步。
- 编译FFmpeg获取so文件
- 编写java端代码
- 编写C代码,生成so
- 通过JNI调用C方法
开发环境
FFmpeg源码地址:https://github.com/FFmpeg/FFmpeg
编译环境:
- macOS Mojava(10.14.2)
- FFmpeg(tag:n3.4)
- ndk-r10e
- AndroidStudio(3.1.4)
NDK配置
export ANDROID_NDK=/Users/zhouxiang/Library/Android/sdk/android-ndk-r10e
export PATH=\$ANDROID_NDK:$PATH
编译FFmpeg
关于编译FFmpeg生成so,在之前的文章中介绍过了,参考https://www.jianshu.com/p/637c7813f69c
编写Java端代码
准备工作:
需要先下载NDK开发相关工具,主要是NDK和CMake。
使用AndroidStudio创建一个新项目,记得把Include C++ support勾选上,然后一路Next,就可以创建一个可以直接进行NDK开发的项目了。
从上面可以看出来,AndroidStudio已经帮我们创建了一个demo,java直接调用C++中的方法,可以直接运行,会在屏幕上显示“Hello from C++”。
我们需要调用的是FFmpeg中的方法,那么就需要加载FFmpeg的so文件,我们修改下MainActivity.java中的static代码块。
//注意不要把so的前缀lib给复制上来了
static {
System.loadLibrary("native-lib");
System.loadLibrary("avutil-55");
System.loadLibrary("avcodec-57");
System.loadLibrary("avformat-57");
System.loadLibrary("avdevice-57");
System.loadLibrary("swresample-2");
System.loadLibrary("swscale-4");
System.loadLibrary("postproc-54");
System.loadLibrary("avfilter-6");
}
编写C端代码
在上面新建的项目中,AndroidStudio已经帮我们创建了一个cpp文件
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring
JNICALL
Java_com_example_zhouxiang_hellojni_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++?";
return env->NewStringUTF(hello.c_str());
}
上面的方法名是有规定的,不是随便取得,遵循如下规则:
Java_包名_类名 _方法名。
这里插播一下,AndroidStudio主要是通过CMake来编译cpp文件产生so的(生成的目录是app/build/intermediates/cmake/),而通过CMake编译是根据CMakeLists.txt文件的规则来编译的。
关于Make和CMake的区别,可以看下https://blog.csdn.net/weixin_42491857/article/details/80741060
继续,我们想要调用的是FFmpeg提供的方法,我们就以获取FFmpeg配置信息为例。
先放一张目录结构图供参考
Step1.把FFmpeg编译后的so拷贝到cpp目录下,并创建armeabi-v7a目录
注意:这里的目录名字不能瞎写,说多了都是泪
Step2.FFMpeg编译后的头文件拷贝到cpp目录下,并创建一个目录,这个目录名随意。
Step3.修改build.gradle,注意不要把armeabi-v7a文件夹写到了jniLibs.srcDirs中,说多了都是泪
Step4.配置CMakeLists.txt
# 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)
#important 需要注意目录
#设置头文件目录
include_directories(src/main/cpp/include)
# 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 )
add_library(
avcodec
SHARED
IMPORTED
)
#指定库的位置
set_target_properties(
avcodec
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavcodec-57.so
)
add_library(
avutil
SHARED
IMPORTED
)
set_target_properties(
avutil
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavutil-55.so
)
add_library(
avdevice
SHARED
IMPORTED
)
set_target_properties(
avdevice
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavdevice-57.so
)
add_library(
avfilter
SHARED
IMPORTED
)
set_target_properties(
avfilter
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavfilter-6.so
)
add_library(
avformat
SHARED
IMPORTED
)
set_target_properties(
avformat
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libavformat-57.so
)
add_library(
postproc
SHARED
IMPORTED
)
set_target_properties(
postproc
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libpostproc-54.so
)
add_library(
swresample
SHARED
IMPORTED
)
set_target_properties(
swresample
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libswresample-2.so
)
add_library(
swscale
SHARED
IMPORTED
)
set_target_properties(
swscale
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/cpp/armeabi-v7a/libswscale-4.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.
native-lib
avcodec
avutil
avdevice
avfilter
avformat
postproc
swscale
swresample
# Links the target library to the log library
# included in the NDK.
${log-lib} )
这里要特别注意:native-lib一定要放在最前面,否则就会报错误,说多了都是泪
Step5.重写native-lib.cpp中的Java_com_example_zhouxiang_hellojni_MainActivity_stringFromJNI**方法
#include <jni.h>
extern "C"
{
#include "include/libavcodec/avcodec.h"
}
extern "C" JNIEXPORT jstring
JNICALL
Java_com_example_zhouxiang_hellojni_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
return env->NewStringUTF(avcodec_configuration());
}
注意上面的引入头文件,需要extern "C"来包一下,否则又会出现下面这种问题,说多了都是泪
/code/HelloJNI/app/src/main/cpp/native-lib.cpp:13: error: undefined reference to 'avcodec_configuration()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
大功告成:
这里我引用一张雷神博客中的图片
Android应用程序使用FFmpeg类库的流程图如下所示
一定要注意我上面写的注意,之前没有做过NDK开发,遇到了不少坑。
雷神NB
参考博客:
https://www.jianshu.com/p/850e47c28a6b
https://blog.csdn.net/leixiaohua1020/article/details/47008825