我在 Android Studio 2.2 Preview 1 中创建了一个新项目,其中包含 Android 应用程序和带有 Google 消息传递的后端模块。这是应用程序文件:
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 新手,无法找到这个错误。我如何解决它?
在您应用的 build.gradle
中添加以下内容:
android {
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
强制 Gradle 仅编译您为所有依赖项声明的版本号,无论依赖项声明了哪个版本号。
这是由于浓缩咖啡。您可以将以下内容添加到您的应用程序 build.grade
以缓解这种情况。
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
exclude group: 'com.google.code.findbugs'
}
exclude group: 'com.android.support', module: 'support-annotations'
方法 1:我删除了 espresso-core 行上的 androidTestCompile,它自动包含在新项目中。然后我的 Android Studio 编译干净。
androidTestCompile 在“build.gradle (Module: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'
})
...
}
方法3:强制使用特定版本编译:(在下面我强制它使用更高版本编译。)
dependencies {
...
androidTestCompile 'com.google.code.findbugs:jsr305:3.0.0'
...
}
运行检测测试时,主 APK 和测试 APK 共享相同的类路径。如果主 APK 和测试 APK 使用相同的库(例如 Guava)但版本不同,Gradle 构建将失败。如果 gradle 没有捕捉到这一点,您的应用在测试和正常运行期间的行为可能会有所不同(包括在其中一种情况下崩溃)。要使构建成功,只需确保两个 APK 使用相同的版本。如果错误是关于间接依赖(您在 build.gradle 中没有提到的库),只需将较新版本的依赖添加到配置中
将此行添加到您的 build.gradle 依赖项中,以便为两个 APK 使用更新版本:
compile('com.google.code.findbugs:jsr305:2.0.1')
为了将来参考,您可以检查您的 Gradle 控制台,它会在错误旁边提供一个有用的链接,以帮助解决任何 gradle 构建错误。
发生这种情况的原因是 diff 依赖使用相同的 diff 版本库。所以,有3个步骤或(1个步骤)来解决这个问题。
第一
添加
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
}
到您在 android {...}
中的 build.gradle
文件
第二
在 android studio
中打开终端运行 ./gradlew -q app:dependencies
命令。
第三
在 Build
列表中点击 android studio 菜单栏中的 Clean Project
。
它将重建项目,然后在第一步中的 remove
代码。
也许您只需要执行第二步。发生错误时我无法回滚。试试。
当我添加 module: 'jsr305'
作为附加的排除语句时,对我来说一切都很好。
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude module: 'jsr305'
})
如您的日志中所述,问题是 2 个依赖项尝试使用不同版本的第 3 个依赖项。将以下内容之一添加到 app-gradle 文件中:
androidTestCompile 'com.google.code.findbugs:jsr305:2.0.1'
androidTestCompile 'com.google.code.findbugs:jsr305:1.3.9'
接受的答案是解决问题的一种方法,因为它只会对有问题的依赖项(com.google.code.findbugs:jsr305)应用一些策略,并且会使用此依赖项的某个版本解决项目周围的问题。基本上它会在整个项目中对齐这个库的版本。 @Santhosh(和其他几个人)给出了一个答案,他建议排除对 espresso 的相同依赖项,这应该以相同的方式工作,但是如果项目有一些其他依赖项依赖于同一个库(com.google .code.findbugs:jsr305),我们也会遇到同样的问题。因此,为了使用这种方法,您需要从所有依赖于 com.google.code.findbugs:jsr305 的项目依赖项中排除同一组。我个人发现 Espresso Contrib 和 Espresso Intents 也使用 com.google.code.findbugs:jsr305。
我希望这个想法能帮助人们了解这里到底发生了什么以及事情是如何工作的(不仅仅是复制粘贴一些代码):)。
将此添加到依赖项以强制使用最新版本的 findbugs 库:
compile 'com.google.code.findbugs:jsr305:2.0.1'
删除 gradle 文件中的 espresso 依赖项对我有用。
删除 app gradle 文件中的这些行:
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
我试图使用 airbnb deeplink dispatch 并收到此错误。我还必须从 annotationProcessor 中排除 findbugs 组。
//airBnb
compile ('com.airbnb:deeplinkdispatch:3.1.1'){
exclude group:'com.google.code.findbugs'
}
annotationProcessor ('com.airbnb:deeplinkdispatch-processor:3.1.1'){
exclude group:'com.google.code.findbugs'
}
那些在 Android 3.0.1 中遇到同样错误的人,可以通过简单地将 compileSdkVersion 和 targetSdkVersion 的版本更新为 27 并在依赖项中实现 com.android.support:appcompat-v7:27.1.1' 来解决它。
在项目 ':app' 中,您可以将以下内容添加到您的 app/build.gradle 文件中:
android {
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
对于 react-native-firebase,将此添加到 app/build.gradle
依赖项部分使其对我有用:
implementation('com.squareup.okhttp3:okhttp:3.12.1') { force = true }
implementation('com.squareup.okio:okio:1.15.0') { force = true }
implementation('com.google.code.findbugs:jsr305:3.0.2') { force = true}
反应原生
如果您正在寻找 react native 解决方案,请将此代码段写入受影响的 node_modules gradle 构建文件中,例如我的情况下的 firebase。
android {
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.0'
}
}
2.0.1
也有效(无论如何,两个版本都需要强制相同)。