What went wrong:
Execution failed for task ‘:app:preDebugBuild’.
Android dependency ‘com.google.android.gms:play-services-basement’ has different version for the compile (16.0.1) and runtime (16.1.0) classpath. You should manually set the same version via DependencyResolution
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
###尝试
1. 方法一:
allprojects {
subprojects {
project.configurations.all {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'com.android.support'
&& !details.requested.name.contains('multidex') ) {
details.useVersion "16.1.0"
}
}
}
}
}
2. 方法二:
classpath 'com.google.gms:google-services:4.0.2' // Just updated the version here.
3. 方法三:
第一步:
第二步:
在依赖树结构里搜索出现版本冲突的module,即play-services-basement,
上面应该可以看出来 271行到273行firebase-core引用的play-services-basement版本是16.0.1,
432行-436行看到react-native-device-info引用的play-services-basement版本变成了16.1.0,
才导致了play-services-basement引用版本冲突。
那我们去除重复引用play-services-basement不就可以了么。
在引用firebase-core的moudle的build.gradle文件中exclude 掉play-services-basement,
implementation ('com.google.firebase:firebase-core:16.0.6') { // 所加的第三方框架
exclude group: 'com.google.android.gms',module: 'play-services-basement' // 加载时排除框架中的design包
}
在同一个配置下(例如androidTestCompile),某个模块的不同版本同时被依赖时,默认使用最新版,gradle同步时不会报错。例如下面的hamcrest-core和runner。
dependencies {
androidTestCompile('com.android.support.test:runner:0.4')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
+--- com.android.support.test:runner:0.4
| +--- com.android.support:support-annotations:23.0.1
| +--- junit:junit:4.12
| | \--- org.hamcrest:hamcrest-core:1.3
| \--- com.android.support.test:exposed-instrumentation-api-publish:0.4
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 -> 0.4 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
+--- com.android.support.test:rules:0.2 (*)
+--- com.squareup:javawriter:2.1.1
+--- org.hamcrest:hamcrest-integration:1.1
| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- org.hamcrest:hamcrest-library:1.1
| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- com.android.support.test:runner:0.2 -> 0.4 (*)
+--- javax.annotation:javax.annotation-api:1.2
\--- org.hamcrest:hamcrest-core:1.1 -> 1.3
force强制设置某个模块的版本。
configurations.all {
resolutionStrategy {
force 'org.hamcrest:hamcrest-core:1.3'
}
}
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
可以看到,原本对hamcrest-core 1.1的依赖,全部变成了1.3。
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
+--- com.android.support.test:rules:0.2 (*)
+--- com.squareup:javawriter:2.1.1
+--- org.hamcrest:hamcrest-integration:1.1
| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- org.hamcrest:hamcrest-library:1.1
| \--- org.hamcrest:hamcrest-core:1.1 -> 1.3
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- com.android.support.test:runner:0.2 (*)
+--- javax.annotation:javax.annotation-api:1.2
\--- org.hamcrest:hamcrest-core:1.1 -> 1.3
Exclude可以设置不编译指定的模块
configurations {
all*.exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
+--- com.android.support.test:rules:0.2 (*)
+--- com.squareup:javawriter:2.1.1
+--- org.hamcrest:hamcrest-integration:1.1
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- org.hamcrest:hamcrest-library:1.1
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- com.android.support.test:runner:0.2 (*)
\--- javax.annotation:javax.annotation-api:1.2
exclude后的参数有group和module,可以分别单独使用,会排除所有匹配项。例如下面的脚本匹配了所有的group为’com.android.support.test’的模块。
configurations {
all*.exclude group: 'com.android.support.test'
}
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
-- com.android.support.test.espresso:espresso-core:2.1
+--- com.squareup:javawriter:2.1.1
+--- org.hamcrest:hamcrest-integration:1.1
| \--- org.hamcrest:hamcrest-core:1.1
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- org.hamcrest:hamcrest-library:1.1
| \--- org.hamcrest:hamcrest-core:1.1
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- javax.annotation:javax.annotation-api:1.2
\--- org.hamcrest:hamcrest-core:1.1
单独给某个模块指定exclude
dependencies {
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
exclude group: 'org.hamcrest'
}
}
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | \--- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| \--- com.android.support:support-annotations:22.0.0
+--- com.android.support.test:rules:0.2
| \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.espresso:espresso-core:2.1
+--- com.android.support.test:rules:0.2 (*)
+--- com.squareup:javawriter:2.1.1
+--- com.android.support.test.espresso:espresso-idling-resource:2.1
+--- javax.inject:javax.inject:1
+--- com.google.code.findbugs:jsr305:2.0.1
+--- com.android.support.test:runner:0.2 (*)
\--- javax.annotation:javax.annotation-api:1.2
不同配置下的版本冲突
同样的配置下的版本冲突,会自动使用最新版;而不同配置下的版本冲突,gradle同步时会直接报错。可使用exclude、force解决冲突。
例如compile ‘com.android.support:appcompat-v7:23.1.1’,和androidTestCompile ‘com.android.support.test.espresso:espresso-core:2.1’,所依赖的com.android.support:support-annotations版本不同,就会导致冲突。
dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile('com.android.support.test:runner:0.2')
androidTestCompile('com.android.support.test:rules:0.2')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.1')
}
gradle同步时会提示
Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.
执行dependencies会提示
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':app'.
> Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
虽然可以通过force、exclude等方式避免依赖项版本冲突,使得grade同步成功,但是并不能代表编译时没有问题。由于不同版本可能不完全兼容,于是会出现各种奇怪的报错。已知的解决思路是更改包的版本、尝试强制使用不同版本的依赖项,找到可兼容的依赖组合。
参考:http://www.paincker.com/gradle-dependencies