本文參考博客:https://blog.csdn.net/leixiaohua1020/article/details/47008825
在上面的博客中芭概,雷神使用的Eclipse孕蝉,也就是需要自己手動(dòng)創(chuàng)建Android.mk文件和ndk命令來編譯so妻熊,本文將基于AndroidStudio3.1.4以及CMake來進(jìn)行NDK開發(fā)。
Android中調(diào)用FFmpeg類庫主要分為下面幾步乘陪。
- 編譯FFmpeg獲取so文件
- 編寫java端代碼
- 編寫C代碼哮内,生成so
- 通過JNI調(diào)用C方法
開發(fā)環(huán)境
FFmpeg源碼地址:https://github.com/FFmpeg/FFmpeg
編譯環(huán)境:
- 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
關(guān)于編譯FFmpeg生成so,在之前的文章中介紹過了掂为,參考http://www.reibang.com/p/637c7813f69c
編寫Java端代碼
準(zhǔn)備工作:
需要先下載NDK開發(fā)相關(guān)工具,主要是NDK和CMake员串。
使用AndroidStudio創(chuàng)建一個(gè)新項(xiàng)目勇哗,記得把Include C++ support勾選上,然后一路Next寸齐,就可以創(chuàng)建一個(gè)可以直接進(jìn)行NDK開發(fā)的項(xiàng)目了欲诺。
從上面可以看出來,AndroidStudio已經(jīng)幫我們創(chuàng)建了一個(gè)demo渺鹦,java直接調(diào)用C++中的方法瞧栗,可以直接運(yùn)行,會(huì)在屏幕上顯示“Hello from C++”海铆。
我們需要調(diào)用的是FFmpeg中的方法,那么就需要加載FFmpeg的so文件挣惰,我們修改下MainActivity.java中的static代碼塊卧斟。
//注意不要把so的前綴lib給復(fù)制上來了
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端代碼
在上面新建的項(xiàng)目中,AndroidStudio已經(jīng)幫我們創(chuàng)建了一個(gè)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());
}
上面的方法名是有規(guī)定的憎茂,不是隨便取得珍语,遵循如下規(guī)則:
Java_包名_類名 _方法名。
這里插播一下竖幔,AndroidStudio主要是通過CMake來編譯cpp文件產(chǎn)生so的(生成的目錄是app/build/intermediates/cmake/)板乙,而通過CMake編譯是根據(jù)CMakeLists.txt文件的規(guī)則來編譯的。
關(guān)于Make和CMake的區(qū)別拳氢,可以看下https://blog.csdn.net/weixin_42491857/article/details/80741060
繼續(xù)募逞,我們想要調(diào)用的是FFmpeg提供的方法,我們就以獲取FFmpeg配置信息為例馋评。
先放一張目錄結(jié)構(gòu)圖供參考
Step1.把FFmpeg編譯后的so拷貝到cpp目錄下放接,并創(chuàng)建armeabi-v7a目錄
注意:這里的目錄名字不能瞎寫,說多了都是淚
Step2.FFMpeg編譯后的頭文件拷貝到cpp目錄下留特,并創(chuàng)建一個(gè)目錄纠脾,這個(gè)目錄名隨意玛瘸。
Step3.修改build.gradle,注意不要把a(bǔ)rmeabi-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 需要注意目錄
#設(shè)置頭文件目錄
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一定要放在最前面糊渊,否則就會(huì)報(bào)錯(cuò)誤,說多了都是淚
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"來包一下渺绒,否則又會(huì)出現(xiàn)下面這種問題,說多了都是淚
/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應(yīng)用程序使用FFmpeg類庫的流程圖如下所示
一定要注意我上面寫的注意磷瘤,之前沒有做過NDK開發(fā)芒篷,遇到了不少坑。
雷神NB
參考博客:
http://www.reibang.com/p/850e47c28a6b
https://blog.csdn.net/leixiaohua1020/article/details/47008825