错误:与依赖项'com.google.code.findbugs:jsr305'冲突。

246

我在 Android Studio 2.2 Preview 1 中创建了一个新项目,包含 Android 应用程序和后端模块,并使用了 Google Messaging。这是应用程序文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.xxx.xxx"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1'
    compile 'com.google.android.gms:play-services-gcm:9.0.0'
    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support:support-annotations:23.4.0'
    compile project(path: ':backend', configuration: 'android-endpoints')
}

但是它显示:

错误:与依赖项 'com.google.code.findbugs: jsr305' 冲突。 应用程序(1.3.9)和测试应用程序(2.0.1)的已解析版本不同。请参见http://g.co/androidstudio/app-test-app-conflict以获取详细信息。

我是Android开发新手,不知道这个错误是什么。我该如何解决?

15个回答

651

在你的应用程序的build.gradle中添加以下内容:

android {
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
    }
}

强制 Gradle 仅编译您声明的版本号,而不管依赖项声明的版本号如何。

58
解释此片段可能会很有用。 - Andrew Gallasch
18
@Andy,这明确告诉Gradle应该编译哪个版本(不管依赖声明的版本号是什么)...强制使用v 2.0.1也可以(无论如何,两个版本都需要被强制保持一致)。 - Martin Zeitler
2
@MartinZeitler 但它并没有真正解释这个问题的根源。 我今天在testImplementation 'junit:junit:4.12',androidTestImplementation 'com.android.support.test:runner:1.0.1'和androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'方面遇到了这个问题。 - fralbo
1
非常感谢。即使过了三年半,这仍然非常有帮助。;) - Kartik
1
@Kartik 哈哈,那是很久以前的事了!别担心,谢谢你,希望有所帮助!我已经停止了 Android 开发,但我相信总有一天我会再次开始。 - gbhall
显示剩余2条评论

170

这是由于浓缩咖啡引起的。您可以将以下内容添加到您的应用程序的build.grade中以减轻此问题。

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
  exclude group: 'com.google.code.findbugs'
}

1
不起作用。被设置为答案的问题是可以正常工作的。 - Warpzit
2
@Warpzit - 这对我有用(被接受的答案也是)。 - Ted Hopp
@Santhosh.. 你能否解释一下,你的代码实际上会做什么? - kumar
2
这很可能是每个人都在寻找的答案。在大多数情况下,Android Studio默认包含Espresso在依赖列表中,您需要排除一些内容才能与其他库一起使用它。除了@Santhosh所说的之外,我还要exclude group: 'com.android.support',module:'support-annotations' - milosmns
1
我刚刚删除了androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') { exclude group: 'com.google.code.findbugs' },它起作用了:D,因为你的修复主要问题是有些人已经在他们的gradle文件中添加了像这样的 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }),再添加一个exclude会抛出异常,向gradle脚本致敬,仍然给你点赞 :) - ShayHaned
显示剩余3条评论

31

方法1: 我删除了新项目中自动包含的espresso-core行中的androidTestCompile。然后我的Android Studio编译干净了。

androidTestCompile在“build.gradle(模块:app)”中:

dependencies {
    ...
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    ...
}

我不知道这个删除操作将来是否会有问题,但现在它肯定适用于我的当前项目。

方法2:在findbugs上添加一个排除也可以起到作用:

dependencies {
    ...
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.google.code.findbugs'
    })
    ...
}

方法三:强制编译使用指定的版本:

(以下我强制使用较高版本进行编译。)

dependencies {
    ...
    androidTestCompile 'com.google.code.findbugs:jsr305:3.0.0'
    ...
}

有一个版本更新:androidTestCompile 'com.google.code.findbugs:jsr305:3.0.1' - Insoft

18

来自Gradle插件用户指南

运行仪器测试时,主APK和测试APK共享相同的类路径。如果主APK和测试APK使用不同版本的同一库(例如Guava),则Gradle构建将失败。如果Gradle未能捕获到这个问题,则您的应用程序可能会在测试和正常运行期间表现不同(包括在某些情况下崩溃)。

要使构建成功,只需确保两个APK使用相同的版本即可。如果错误是关于间接依赖项(在build.gradle中未提及的库),只需添加对新版本的依赖项即可解决问题。

将此行添加到您的build.gradle依赖项中,以同时为两个APK使用新版本:

compile('com.google.code.findbugs:jsr305:2.0.1')

供参考,您可以检查您的Gradle控制台,它将为任何Gradle构建错误提供有用的链接。


8
这种情况发生的原因是不同的依赖项使用了不同版本的库。
因此,解决这个问题有三个步骤或一个步骤。

第一步

添加

configurations.all {
    resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
}

将以下内容添加到位于android {...}build.gradle文件中:

第二步

在Android Studio中打开终端,运行./gradlew -q app:dependencies命令。

第三步

从菜单栏中的Build列表中选择Clean Project。 它将重新构建项目,然后删除第一步中的代码。
如果发生错误,则可能只需要执行第二步。 我不能回滚。 试试看。

7
当我将module: 'jsr305'作为额外的排除语句添加时,一切都很好。
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude module: 'jsr305'

})


6
根据您的日志,问题在于有两个依赖项试图使用不同版本的第三方依赖项。请在app-gradle文件中添加以下内容之一:
androidTestCompile 'com.google.code.findbugs:jsr305:2.0.1'
androidTestCompile 'com.google.code.findbugs:jsr305:1.3.9'

4
  1. 接受的答案是解决问题的一种方法,因为它将为出现问题的依赖项(com.google.code.findbugs:jsr305)应用一些策略,并且使用此依赖项的某个版本来解决整个项目中的问题。基本上,它将对整个项目内部的库的版本进行对齐。

  2. @Santhosh(以及其他几个人)提供了一个建议,即从Espresso中排除相同的依赖项,这应该是通过相同方式工作的,但如果项目有其他依赖项依赖于相同的库(com.google.code.findbugs:jsr305),我们将再次遇到同样的问题。因此,为了使用此方法,您需要从所有项目依赖项中排除相同的组,这些依赖项依赖于com.google.code.findbugs:jsr305。我个人发现Espresso Contrib和Espresso Intents也使用com.google.code.findbugs:jsr305。

我希望这些想法能够帮助某些人认识到这里发生了什么以及事情是如何运作的(不只是复制粘贴一些代码):).


3
将以下内容添加到依赖项中,以强制使用最新版本的findbugs库:
compile 'com.google.code.findbugs:jsr305:2.0.1'

2

在我的情况下,删除gradle文件中的espresso依赖项是有效的。

删除应用程序gradle文件中的这些行:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

3
如果你只是删除了espresso的依赖项,那么你将无法再测试你的应用程序,因此这种解决方案显然无效。 - Jan Heinrich Reimer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接