2

I have developed an application in Android Studio. I have integrated FFMPEG library in my application. This library contains a module and *.so files. Now I need to integrate another library having a jar file and native *.so files, Twilio for calling.
The issue I am facing is, whenever I try to build the application, *.so file of Twilio library automatically gets deleted. Then app gives error that, it can not find the native file.

Here is my build.gradle

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

repositories {
    jcenter()
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "my.package.name"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
    pickFirst 'META-INF/ASL2.0'
}

sourceSets.main {
    jniLibs.srcDir 'src/main/libs' //set .so files location to libs
    jni.srcDirs = [] //disable automatic ndk-build call
}

// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
    } else {
        def ndkDir = android.ndkDirectory.getAbsolutePath()
        commandLine ndkDir + '/ndk-build', '-C', file('src/main').absolutePath
    }
}
tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}


}

task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
    destinationDir file("$buildDir/native-libs")
    baseName 'native-libs'
    from fileTree(dir: 'libs', include: '**/*.so')
    into 'lib/'
    }

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    testCompile 'junit:junit:4.12'

    compile project(':ffmpegLibs')
    compile files('libs/commons-codec-1.9.jar')
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
    compile 'com.android.support:percent:23.1.1'
    compile 'com.android.support:palette-v7:23.1.1'
    compile 'com.fasterxml.jackson.core:jackson-core:2.7.0-rc1'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.0-rc1'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.7.0-rc1'
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
    compile 'com.google.code.gson:gson:2.5'
    compile 'com.android.support:recyclerview-v7:23.1.1'
    compile 'com.daimajia.swipelayout:library:1.2.0'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.github.bumptech.glide:glide:3.6.1'
    compile 'com.timehop.stickyheadersrecyclerview:library:0.4.3'
    compile files('libs/twilioclient-android.jar')
}
Priyank Gandhi
  • 1,257
  • 1
  • 16
  • 29
  • Do you use prebuilt libraries? Please post your **build.gradle** file – Alex Cohn Feb 06 '16 at 07:30
  • @AlexCohn i have edited my code and added build.gradle. Please check the same. – Priyank Gandhi Feb 08 '16 at 12:09
  • What does `:ffmpeglibs` project do? What libs are copied by `nativeLibsToJar` task and why? You should only make sure that all compiled or precompiled shared libraries end up in your **jniLibs.srcDir**. I understand that `twilioclient-android.jar` does not contain native lib, it's copied separately, isn't it? – Alex Cohn Feb 08 '16 at 14:23
  • `:ffmpeglibs` is a library for video cropping. I am not sure about `nativeLibsToJar`. I found that code from somewhere as a solution but it dint work. I have kept all my libraries in jniLibs folder. And yes, `twilioclient-android.jar` does't contain any native lib but to use this lib, `libtwilio-native.so` is required. – Priyank Gandhi Feb 09 '16 at 04:44
  • I know what ffmpeg libs do; the question was about the *project*. Does it build these libraries? Or only copies them to the right folder? I am afraid that you must learn how your build system works, not simply using gradle tasks from others. Or you maybe you need some professional help. – Alex Cohn Feb 09 '16 at 08:36
  • @AlexCohn, Thanks for your help. Would you please guide me how to build native libraries ? May be you can refer any tutorials or any blog link. It will be really helpful. – Priyank Gandhi Feb 09 '16 at 11:20

0 Answers0