1

How can I add a .so library in Android Studio and make calls to it through a .c file with native code?

I have googled for a week and I've tried every posible solution I've found but nothing.

The sites are this, this, this, this, and more...

I arrived to make calls to a native .c file but I cannot include the .so library because Android studio cannot find it.

I don't want to use the new experimental gradle yet.

The environment configuration is the next one:

  • Android Studio 1.5

  • Gradle 2.8

  • Android plugin version 1.3.0

  • JDK 1.8.0

This is part of my code and gradle configuration:

GRADLE FILE

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    ....

    ndk {
        moduleName "ffmpegkit"
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

PROJECT STRUCTURE

CAPTURE

INCLUDE STATEMENTS IN .C FILE

enter image description here

As you can see, "libavcodec.so" is not found but the project structure is the correct according to some solutions I've found.

Does anybody know how can I solve this problem and use these .so libraries with .c files?

Thank you very much!

Community
  • 1
  • 1
ezefire
  • 782
  • 5
  • 18

1 Answers1

1

I assume your .c file is somewhere under ffmpegkit directory in your project. You copied the ffmpeg binaries to your project's jniLibs, too. So far so good.

You need to provide the path to ffmpeg include files so that your .c files could compile. You also must list the libraries that the linker needs to find the external references of libffmpegkit.so that you are building:

defaultConfig {
    ....

    ndk {
        moduleName "ffmpegkit"
        abiFilter "armeabi"
        cFlags "-I/qqq/ffmpeg"
        ldLibs "-llog -L src/main/libs", "avformat", "avcodec", "avutil" 
    }
}

Make sure that the ffmpeg libraries are compiled with a compatible Android toolchain! Note that the order in which ffmpeg libraries are listed is important. Like, libavformat depends on libavcodec, therefore it should go before.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Thanks for your response, but it doesn't work.. I have changed de libavcodec library path to mine but it still not working – ezefire Nov 09 '15 at 15:35
  • did you build the ffmpeg libraries yourself, or you got precompiled binaries from somewhere? – Alex Cohn Nov 09 '15 at 16:04
  • I've built them myself with the NDK toolchain – ezefire Nov 09 '15 at 16:06
  • do you mind to continue this discussion in a [chat](http://chat.stackoverflow.com/rooms/94625/33610061-how-to-include-a-so-library-and-c-files-in-android-studio-1-5)? – Alex Cohn Nov 09 '15 at 16:52