satyres
satyres

Reputation: 51

Compile error for a simple library in Android using latest FFmpeg library with NDK

I'm trying to get FFmpeg working in Android , and after successfuly compiled it on Ubuntu 64bit , and created the Android.mk under source/ffmpeg-3.4.2/android/arm folder and also the Android.mk in my own Android project ! i'm unable to compile a simple program and create the .so file ! By the way i've searched over the net and in Stackoverflow for 2 days now without luck ! here is the code Android.mk

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := MyLibrary
LOCAL_SRC_FILES := MyNativeFunctions.c
LOCAL_LDLIBS := -lz -llog
LOCAL_STATIC_LIBRARIES := libavformat_static libavcodec_static libavutil_static
include $(BUILD_SHARED_LIBRARY)

$(call import-module,ffmpeg-3.2.4/android/arm)

The native functions

    #include <libavformat/avformat.h>
    #include <libavcodec/avcodec.h>
    #include <libavutil/avutil.h>

    #include <android/log.h>
    #define LOG_TAG "mylib"
    #define LOGI(...)  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
    #define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
    jint Java_com_example_home_hellondk_MyNativeFunctions_TestNdk(JNIEnv * env, jobject this, jstring filename)
    {
         av_register_all();

return 0;
}

when i use NDK-BUILD here is the error i got :

/home/home/Android/Sdk/ndk-bundle/sources/ffmpeg-3.2.4/android/arm/lib/libavformat.a: error adding symbols: File in wrong format
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/arm64-v8a/libMyLibrary.so] Error 1

[EDIT] Build_android.sh

#!/bin/bash
NDK=/home/home/Android/Sdk/ndk-bundle
SYSROOT=$NDK/platforms/android-24/arch-arm/
TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64
 function build_one
{
./configure \
--prefix=$PREFIX \
--disable-shared \
--enable-static \
--disable-doc \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-avdevice \
--disable-doc \
--disable-symver \
--cross-prefix=$TOOLCHAIN/bin/arm-linux-androideabi- \
--target-os=linux \
--arch=arm \
--enable-cross-compile \
--sysroot=$SYSROOT \
--extra-cflags="-Os -fpic $ADDI_CFLAGS" \
--extra-ldflags="$ADDI_LDFLAGS" \
$ADDITIONAL_CONFIGURE_FLAG
make clean
make
make install
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_one

[EDIT] The problem has been solved by adding the missing library in LOCAL_STATIC_LIBRARIES := libavformat_static libavcodec_static libswscale_static libavutil_static libswresample_static and for all users of Android Studio do not forget to add this line to your gradle otherwise it won't work.

sourceSets.main {
        jni.srcDirs = []
        jniLibs.srcDir 'src/main/libs'
    }

Best regards

Upvotes: 0

Views: 1102

Answers (1)

Dan Albert
Dan Albert

Reputation: 10499

You're only building ffmpeg for arm32 but you're building your app for all ABIs.

Here in your ffmpeg build script you're targeting arm specifically:

SYSROOT=$NDK/platforms/android-24/arch-arm/

And from the error output you can see it was building arm64:

make: *** [obj/local/arm64-v8a/libMyLibrary.so] Error 1

Either limit your ndk-build to building only arm32 (APP_ABI := armeabi-v7a in your Application.mk) or build ffmpeg for the other architectures as well.

By the way, you'll have an easier time building autoconf projects with the NDK if you use https://developer.android.com/ndk/guides/standalone_toolchain.html

Upvotes: 1

Related Questions