为什么我得到重复条目:com/google/common/base/FinalizableReference.class?

Why do I get duplicate entry: com/google/common/base/FinalizableReference.class?

当我将 Microsoft Azure 移动服务 SDK 添加到我的项目时:

compile 'com.microsoft.azure:azure-mobile-services-android-sdk:2.0.2'

我收到这个错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: com/google/common/base/FinalizableReference.class

可能是什么原因,我该如何解决?
我想我可以为 Gradle 制定一些排除规则,但那会是什么样子?

运行任务dependencies,我们可以看到azure-mobile-services-android-sdk包含两个依赖。

\--- com.microsoft.azure:azure-mobile-services-android-sdk:2.0.2
     +--- com.google.code.gson:gson:2.3
     \--- com.google.guava:guava:18.0

报告为重复的 class 来自 Guava。如果您不直接使用它,则可能是另一个依赖项正在使用它(可能与另一个版本一起使用)。一个可能的解决方法是从依赖项中排除 Guava。

compile('com.microsoft.azure:azure-mobile-services-android-sdk:2.0.2') {
    exclude module: 'guava'
}

编辑:

回答您的评论,运行 您项目中的 dependencies 任务,并尝试找到其他使用 guava 作为依赖项的库(可能是旧版本的 guava)。

(in your project root folder)
$ cd app
(if you are running on OSX or Linux)
$ ../gradlew dependencies
(if you are running on Windows)
$ ../gradlew.bat dependencies

当您确定哪些依赖项具有 guava 时,以与之前对 azure 相同的方式排除它。

是因为您有两个包含相同 java class 的库。命令 gradle dependencies 应该告诉你它是什么。

关于如何排除依赖项,您有几种选择:

dependencies {

 compile('com.microsoft.azure:azure-mobile-services-android-sdk:2.0.2') {


   exclude module: 'cglib' //by artifact name

   exclude group: 'org.jmock' //by group

   exclude group: 'com.unwanted', module: 'someLib' //by both name and group

 }

}