你可以在android gradle中用packagingOptions排除类吗?

时间:2016-11-12 15:01:22

标签: android gradle android-gradle

我试图弄清楚是否可以排除类文件或对jar文件依赖项中的类使用pickfirst。我们似乎总是遇到问题,其中第三方库在aar文件中打包jar而不是使用gradle依赖项,因此导致重复文件zip异常:

像这样:

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

我们到目前为止找到的唯一解决方案是解压缩aar,删除有问题的jar,然后将其重新压缩。有没有办法从gradle中排除jar或类?

看看来源,我似乎应该能够使用包装选项。所以我尝试了先选择排除的各种组合,但没有运气:

packagingOptions {
    pickFirst '**/StringEncoderComparator.class'
    pickFirst 'org/apache/commons/codec/StringEncoderComparator.class'
    pickFirst 'org/apache/commons/codec/*'

}

1 个答案:

答案 0 :(得分:0)

使用resolutionStrategy。

https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html

apply plugin: 'java' //so that there are some configurations

configurations.all {
  resolutionStrategy {
    // fail eagerly on version conflict (includes transitive dependencies)
    // e.g. multiple different versions of the same dependency (group and name are equal)
    failOnVersionConflict()

    // prefer modules that are part of this build (multi-project or composite build) over external modules
    preferProjectModules()

    // force certain versions of dependencies (including transitive)
    //  *append new forced modules:
    force 'asm:asm-all:3.3.1', 'commons-io:commons-io:1.4'
    //  *replace existing forced modules with new ones:
    forcedModules = ['asm:asm-all:3.3.1']

    // add dependency substitution rules
    dependencySubstitution {
      substitute module('org.gradle:api') with project(':api')
      substitute project(':util') with module('org.gradle:util:3.0')
    }

    // cache dynamic versions for 10 minutes
    cacheDynamicVersionsFor 10*60, 'seconds'
    // don't cache changing modules at all
    cacheChangingModulesFor 0, 'seconds'
  }
}