androidgradlejanrain

Apache commons dependency not excluded in gradle | Android


I have a library project (Janrain:Jump) that uses the org.apache.http.legacy library. When I try to build my project I get the duplicate error like following:

com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/apache/commons/codec/StringEncoderComparator.class

So I figured that the org.apache.commons are duplicate entry because Janrain uses it and it's also included in the Android 24 (as an external library). So I tried to remove the commons from the :Jump gradle like:

configurations {
    all*.exclude group: 'org.apache.commons'
}

Now, I'd expect that this removes org.apache.commons but I still get the duplicate entry gradle error.

Here is the :Jump gradle file

apply plugin: 'com.android.library'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    //If building with strict Android 6.0 the following will need to be uncommented
    //See: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
    //And: http://stackoverflow.com/questions/31653002/how-to-use-the-legacy-apache-http-client-on-android-m
    useLibrary "org.apache.http.legacy"

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 24
        // replace the below string with your own Google client ID. Make sure this is consistent
        // with the values used in openid_appauth_idp_configs.xml
        manifestPlaceholders = [
                <my values>]
    }

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

    }
}

configurations {
    all*.exclude group: 'org.apache.commons'
}

dependencies {
    compile 'com.android.support:support-v4:24.2.0'
    compile files('libs/org.apache.http.legacy.jar')
    compile 'com.squareup.okhttp:okhttp:2.5.0'
    compile 'com.squareup.okhttp:okhttp-apache:2.5.0'
    compile 'com.squareup.okio:okio:1.6.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.squareup.retrofit:retrofit:1.8.0'
    compile 'net.openid:appauth:0.4.1'
}

allprojects {
    repositories {
        jcenter()
    }
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}

Why is the org.apache.commons not excluded even when I include it in configurations?


Solution

  • Just add this task to your build.gradle then run gradle findDuplicates to find the jars

    task findDuplicates {
        doLast {
            def findMe = 'org/apache/commons/codec/StringEncoderComparator.class'
            configurations.runtime.asFileTree.matching {
                include '**/*.jar'
            }.files.each { File jarFile ->
                zipTree(jarFile).visit { FileVisitDetails fvd ->
                    if (fvd.path == findMe) {
                        println "Found $findMe in $jarFile.name"
                    }
                }
            }
        }
    }