如何使用gradle从maven repo中正确删除传递依赖项

时间:2014-08-27 01:14:01

标签: android maven gradle android-studio dependency-management

构建项目时出现以下错误:

UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Lorg/slf4j/impl/StaticLoggerBinder;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
        at com.android.dx.command.dexer.Main.run(Main.java:230)
        at com.android.dx.command.dexer.Main.main(Main.java:199)
        at com.android.dx.command.Main.main(Main.java:103)

据我所知,这是因为我有多个依赖于slf4j的库。 build.gradle文件中的依赖项是:

dependencies {
    compile project(':libraries:some-sdk')
    compile files('libs/some-library.jar')
    compile 'com.some-module:some-artifact:X.X.X'
    compile 'com.github.tony19:logback-android-classic:1.1.1-3'
}

我知道logback-android-classic依赖是问题,因为它确实依赖于slf4j,如果我评论说我的项目构建正常。根据其他消息来源,我发现这通常是通过排除传递依赖性来解决的:

compile('com.github.tony19:logback-android-classic:1.1.1-3') {
    exclude group: 'org.slf4j'

然而,即使在我清理和重建之后,我仍然会收到错误。

更新: 运行dependencyInsight寻找slf4j作为dmahapatro建议没有返回结果:

$ gradle -q dependencyInsight --dependency slf4j --configuration compile
No dependencies matching given input were found in configuration ':main:compile'

我的完整依赖树是:

+--- project :stripe
+--- project :apptentive-android-sdk
+--- project :numberpicker
+--- project :switch-backport
+--- org.jsoup:jsoup:1.7.3
+--- org.ocpsoft.prettytime:prettytime:3.2.4.Final
+--- org.mockito:mockito-all:1.9.5
+--- com.google.dexmaker:dexmaker:1.0
+--- com.google.dexmaker:dexmaker-mockito:1.0
+--- com.pusher:pusher-java-client:0.3.1
\--- com.github.tony19:logback-android-classic:1.1.1-3
     +--- com.github.tony19:apktool-lib:1.4.4-3
     |    \--- com.google.android:android:2.1_r1
     |         +--- commons-logging:commons-logging:1.1.1
     |         +--- org.apache.httpcomponents:httpclient:4.0.1
     |         |    +--- org.apache.httpcomponents:httpcore:4.0.1
     |         |    +--- commons-logging:commons-logging:1.1.1
     |         |    \--- commons-codec:commons-codec:1.3
     |         +--- org.khronos:opengl-api:gl1.1-android-2.1_r1
     |         +--- xerces:xmlParserAPIs:2.6.2
     |         \--- xpp3:xpp3:1.1.4c
     \--- com.github.tony19:logback-android-core:1.1.1-3

更新2:

我已将冲突缩小到logback-android-classic库和本地jar(遗憾的是,由于许可问题,我无法直接编辑此jar)。排除其中任何一个解决冲突,以及只有这两个依赖项的空通用测试应用程序将产生冲突。我已经验证本地jar包含org.slf4j,但由于上述许可问题,我无法做到这一点。所以我唯一的选择就是从logback-android-classic中排除slf4j。鉴于依赖树实际上并没有将slf4j显示为logback-android-classic的依赖关系,并且考虑到添加

    compile('com.github.tony19:logback-android-classic:1.1.1-3') {
            exclude group: 'org.slf4j'
    }

还没有解决问题,如何从logback-android-classic中排除slf4j依赖?

最后更新: 最终通过与我正在使用的本地jar的所有者合作来解决这个问题。我怀疑这对未来的访问者来说是一个特别有用的答案,但是为了完成它想要包含它。

1 个答案:

答案 0 :(得分:0)

我在我的" build.gradle"中添加了以下内容:删除" slf4j"的传递依赖问题。这是排除依赖关系的方法,因此不会传递加载。并通过在"编译组"中提及它来明确加载冲突库。声明。认为它可以帮助你。

configurations {
    all*.exclude group: 'ch.qos.logback'
    all*.exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
    all*.exclude group: 'org.slf4j', module: 'jul-to-slf4j'
    all*.exclude group: 'org.slf4j', module: 'log4j-over-slf4j'
    all*.exclude group: 'org.slf4j', module: 'jcl-over-slf4j'
    all*.exclude group: 'org.slf4j', module: 'slf4j-log4j12'
    all*.exclude group: 'org.slf4j', module: 'slf4j-simple'
}

compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4j2Version
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4j2Version
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: slf4jToLog4jVersion