Skip to content Skip to sidebar Skip to footer

Include Shared Library .so In Android Studio

I extracted ffmpeg in ndk's source folder then compiled it there only for that I followed this:http://www.roman10.net/2013/08/18/how-to-build-ffmpeg-with-ndk-r9/ and successfully g

Solution 1:

I took reference from third party google sample https://github.com/googlesamples/android-ndk/tree/master/hello-thirdparty which is basically for static lib(.a) and this is how I resolved my problem with gradle experimental plugin:

My build.gradle file:

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "com.example.spartan.hello"
            minSdkVersion.apiLevel = 18
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-android.txt'))
        }
    }

    repositories {
        libs(PrebuiltLibraries) {
            libavcodec {
                headers.srcDir "/home/spartan/AndroidStudioProjects/Hello/app/src/main/jni/include"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("/home/spartan/AndroidStudioProjects/Hello/app/src/main/jniLibs/${targetPlatform.getName()}/libavcodec-57.so")
                }
            }                

        }
    }

    android.ndk {
        moduleName = "tutorial01"

        def jniPath = "/home/spartan/AndroidStudioProjects/Hello/app/src/main/jniLibs"
        cppFlags.add("-I${file(jniPath)}".toString())
        file(jniPath).eachDirRecurse { dir ->
            cppFlags.add("-I${file(dir)}".toString())
        }
        ldLibs.addAll(["log", "android","jnigraphics"])
        stl = "gnustl_shared"
    }


    android.sources {
        main {
            jni {
                dependencies {
                    library "libavcodec" linkage "shared"
                }
            }
        }
    }

    android.productFlavors {
        create ("armv7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
    }

    android.sources {
        main {
            jni {
                source {
                    srcDirs = ['src/main/none']
                }

            }

        }
    }

}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
}

In my case I copied .so in jniLibs and include folder of .h files in jni folder and there is no need to add -l in android.ndk because I already added in headers.srcDir(I just added here to mention).

So here I competently replaced my Android.mk with repositories and its really simple.

Solution 2:

If you're using ndk-build yourself, you don't have to compile anything from gradle.

Set your jni sources to an non-existent folder or no folder:

android.sources{
   main.jni {
    source {
        srcDirs = [] // or ['src/main/none']
    }
  }
}

and remove this block:

// New code
android.ndk {
    moduleName = "tutorial01"
}

Post a Comment for "Include Shared Library .so In Android Studio"