gradle 中未排除 Apache 公共依赖项 | Android

Apache commons dependency not excluded in gradle | Android

我有一个使用 org.apache.http.legacy 库的库项目 (Janrain:Jump)。当我尝试构建我的项目时,出现如下重复错误:

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

所以我认为 org.apache.commons 是重复的条目,因为 Janrain 使用它并且它也包含在 Android 24(作为外部库)中。所以我试图从 :Jump gradle 中删除公共资源,例如:

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

现在,我希望这会删除 org.apache.commons,但我仍然收到重复条目 gradle 错误。

这是 :Jump gradle 文件

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: 
    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"
        }
    }
}

为什么 org.apache.commons 即使我在配置中包含它也没有被排除?

只需将此任务添加到您的 build.gradle,然后 运行 gradle findDuplicates 即可找到罐子

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"
                }
            }
        }
    }
}