Build FFmpeg 4.0.2 with NDK r17c

  • macOS 10.12.6
  • Android Studio 3.5
  • Cmake 13.10.2
  • NDK r17c
  • FFmpeg 4.0.2

I follow the [Building FFmpeg 4.0 for Android with Clang](https://medium.com/@ilja.kosynkin/building-ffmpeg-4-0-for-android-with-clang-642e4911c31e\ blog to build the library.

Download NDK r17c

  • to NDK Archives of Android Developer NDK Downloads page download the r17c.

NDK Downloads page

Download FFmpeg source

I can use Git to checkout n4.0.3 from FFmpeg repository, but I want to clone source from FFmpeg Development Kit GitHub directly to avoid problems.

I put the FFmpeg source to sources folder of NDK.

/Users/yen/Downloads/android-ndk-r17c/sources/ffmpeg4

FFmpeg Development Kit @GitHub

Build share library of FFmpeg for Android

$pwd
/Users/yen/Downloads/android-ndk-r17c/sources/ffmpeg4
$ ./build.sh
IS_CLANG 1
DIR is /Users/yen/Downloads/android-ndk-r17c/sources/ffmpeg4
PARENT is /Users/yen/Downloads/android-ndk-r17c/sources
NDK is /Users/yen/Downloads/android-ndk-r17c
SYSROOT is /Users/yen/Downloads/android-ndk-r17c/sysroot
HOST is darwin-x86_64
LLVM_TOOLCHAIN is /Users/yen/Downloads/android-ndk-r17c/toolchains/llvm/prebuilt/darwin-x86_64/bin
ARCH is x86
LEVEL is 14
TARGET is i686-linux-android
PLATFORM_ARCH is x86
TOOLCHAIN_FOLDER is x86
TOOLCHAIN is /Users/yen/Downloads/android-ndk-r17c/toolchains/x86-4.9/prebuilt/darwin-x86_64/bin
CC is /Users/yen/Downloads/android-ndk-r17c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
CXX is /Users/yen/Downloads/android-ndk-r17c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++
AS is /Users/yen/Downloads/android-ndk-r17c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
AR is /Users/yen/Downloads/android-ndk-r17c/toolchains/x86-4.9/prebuilt/darwin-x86_64/bin/i686-linux-android-ar
LD is /Users/yen/Downloads/android-ndk-r17c/toolchains/x86-4.9/prebuilt/darwin-x86_64/bin/i686-linux-android-ld
STRIP is /Users/yen/Downloads/android-ndk-r17c/toolchains/x86-4.9/prebuilt/darwin-x86_64/bin/i686-linux-android-strip
-L/Users/yen/Downloads/android-ndk-r17c/toolchains/x86-4.9/prebuilt/darwin-x86_64/lib/gcc/i686-linux-android/4.9.x -L/Users/yen/Downloads/android-ndk-r17c/platforms/android-14/arch-x86/usr/lib -lc

The it will auto build .so. I can find all library and header in android folder.

$ pwd
/Users/yen/Downloads/android-ndk-r17c/sources/ffmpeg4/android
$ tree -P "*.so"
.
├── armeabi-v7a
│   ├── include
│   │   ├── libavcodec
│   │   ├── libavdevice
│   │   ├── libavfilter
│   │   ├── libavformat
│   │   ├── libavutil
│   │   ├── libswresample
│   │   └── libswscale
│   ├── lib
│   │   ├── libavcodec.so
│   │   ├── libavdevice.so
│   │   ├── libavfilter.so
│   │   ├── libavformat.so
│   │   ├── libavutil.so
│   │   ├── libswresample.so
│   │   ├── libswscale.so
│   │   └── pkgconfig
│   └── share
└── x86
    ├── include
    │   ├── libavcodec
    │   ├── libavdevice
    │   ├── libavfilter
    │   ├── libavformat
    │   ├── libavutil
    │   ├── libswresample
    │   └── libswscale
    ├── lib
    │   ├── libavcodec.so
    │   ├── libavdevice.so
    │   ├── libavfilter.so
    │   ├── libavformat.so
    │   ├── libavutil.so
    │   ├── libswresample.so
    │   ├── libswscale.so
    │   └── pkgconfig
    └── share

Using FFmpeg library in Android project

  • create empty native Android Studio project.
  • clone .so to projectRoot/app/src/main/jniLibs/${ABI}/.
  • clone include folder from /Users/yen/Downloads/android-ndk-r17c/sources/ffmpeg4/android/x86/ to projectRoot/app/src/main/cpp/. Just needs one for all ABIs.
  • CMake
cmake_minimum_required(VERSION 3.10.2)

add_library(native-lib SHARED native-lib.cpp)

find_library(log-lib log)

set(JNI_LIB_DIR ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
include_directories(${CMAKE_SOURCE_DIR}/include)

add_library( avcodec
        SHARED
        IMPORTED)
set_target_properties( avcodec
        PROPERTIES IMPORTED_LOCATION
        ${JNI_LIB_DIR}/libavcodec.so)

add_library( avdevice
        SHARED
        IMPORTED)
set_target_properties( avdevice
        PROPERTIES IMPORTED_LOCATION
        ${JNI_LIB_DIR}/libavdevice.so)

add_library( avfilter
        SHARED
        IMPORTED)
set_target_properties( avfilter
        PROPERTIES IMPORTED_LOCATION
        ${JNI_LIB_DIR}/libavfilter.so)

add_library( avformat
        SHARED
        IMPORTED)
set_target_properties( avformat
        PROPERTIES IMPORTED_LOCATION
        ${JNI_LIB_DIR}/libavformat.so)

add_library( avutil
        SHARED
        IMPORTED)
set_target_properties( avutil
        PROPERTIES IMPORTED_LOCATION
        ${JNI_LIB_DIR}/libavutil.so)

add_library( swresample
        SHARED
        IMPORTED)
set_target_properties( swresample
        PROPERTIES IMPORTED_LOCATION
        ${JNI_LIB_DIR}/libswresample.so)

add_library( swscale
        SHARED
        IMPORTED)
set_target_properties( swscale
        PROPERTIES IMPORTED_LOCATION
        ${JNI_LIB_DIR}/libswscale.so)

target_link_libraries(native-lib
        avcodec
        avdevice
        avfilter
        avformat
        avutil
        swresample
        swscale
        ${log-lib})

target_compile_options(native-lib PUBLIC -fsanitize=address -fno-omit-frame-pointer)
set_target_properties(native-lib PROPERTIES LINK_FLAGS -fsanitize=address)

Android-通过cmake集成ffmpeg https://juejin.im/post/5bdb1c36e51d455a385e56de

  • module build gradle
def SupportedABIs = ['arm64-v8a', 'armeabi-v7a', 'x86_64', 'x86']

android {
    defaultConfig {
        externalNativeBuild {
            cmake {
                cppFlags ""
                arguments "-DANDROID_STL=c++_shared"
                abiFilters.addAll(SupportedABIs)
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
}
  • native-lib.cpp
#include <jni.h>
#include <android/log.h>
#include <string>

extern "C" {
#include "libavcodec/avcodec.h"
}

constexpr auto TAG = "nativeCode";

extern "C" JNIEXPORT void JNICALL Java_com_practice_Lib_callNative(JNIEnv *env, jobject) {
  __android_log_print(ANDROID_LOG_INFO, TAG, "start");
  __android_log_print(ANDROID_LOG_ERROR, TAG, "avcodec: %s", avcodec_configuration());
}
  • LogCat
I/nativeCode: start
E/nativeCode: avcodec: --prefix=android/x86 --disable-asm --enable-cross-compile --disable-static --disable-programs --disable-doc --enable-shared --enable-protocol=file --enable-pic --enable-small --ar=/Users/yen/Downloads/android-ndk-r17c/toolchains/x86-4.9/prebuilt/darwin-x86_64/bin/i686-linux-android-ar --strip=/Users/yen/Downloads/android-ndk-r17c/toolchains/x86-4.9/prebuilt/darwin-x86_64/bin/i686-linux-android-strip --ld=/Users/yen/Downloads/android-ndk-r17c/toolchains/x86-4.9/prebuilt/darwin-x86_64/bin/i686-linux-android-ld --cc=/Users/yen/Downloads/android-ndk-r17c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --cxx=/Users/yen/Downloads/android-ndk-r17c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --as=/Users/yen/Downloads/android-ndk-r17c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --target-os=android --extra-cflags='-target i686-none-linux-androideabi -mtune=intel -mssse3 -mfpmath=sse -m32 -I/Users/yen/Downloads/android-ndk-r17c/sysroot/usr/include/i686-linux-android -O3 -fPIC' --extra-ldflags='-

results matching ""

    No results matching ""