- 前兩天在RN項(xiàng)目中集成原生的firebase,之后報(bào)錯(cuò)插件版本沖突悍手,報(bào)錯(cuò)信息如下:
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
嘗試
- 這個(gè)問題可是折磨了我大半天逢慌,按照網(wǎng)上搜索的解決方式各種嘗試
1. 方法一:
- 在項(xiàng)目的build.gradle中加入
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. 方法三:
- 各個(gè)module的dependencies里的compile改為implementation
然而悠轩,并沒有什么卵用
解決方法
- 之后靜下心來好好思考了下,依賴版本沖突攻泼,肯定是存在重復(fù)依賴的問題火架。從這個(gè)思路著手:
第一步:
- 將firebase的依賴版本更新到最新版,這樣能盡可能的降低版本沖突的概率忙菠;
參考firebase插件的各個(gè)發(fā)布版本
https://firebase.google.com/support/release-notes/android#20180523
第二步:
-
去除依賴沖突 參考https://blog.csdn.net/yyo201/article/details/80570741
在Teminal 里面輸入 gradlew app:dependencies 回車之后等一會(huì)兒就可以查看到整個(gè)項(xiàng)目的依賴樹結(jié)構(gòu)了何鸡。
image.png
image.png 在依賴樹結(jié)構(gòu)里搜索出現(xiàn)版本沖突的module,即play-services-basement只搁,
上面應(yīng)該可以看出來 271行到273行firebase-core引用的play-services-basement版本是16.0.1音比,
432行-436行看到react-native-device-info引用的play-services-basement版本變成了16.1.0,
才導(dǎo)致了play-services-basement引用版本沖突氢惋。那我們?nèi)コ貜?fù)引用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' // 加載時(shí)排除框架中的design包
}
- 現(xiàn)在編譯焰望,就能成功通過了骚亿。
其他可行方法
版本沖突
在同一個(gè)配置下(例如androidTestCompile),某個(gè)模塊的不同版本同時(shí)被依賴時(shí)熊赖,默認(rèn)使用最新版来屠,gradle同步時(shí)不會(huì)報(bào)錯(cuò)。例如下面的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
1俱笛、Force
force強(qiáng)制設(shè)置某個(gè)模塊的版本。
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')
}
可以看到传趾,原本對(duì)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
2浆兰、Exclude
Exclude可以設(shè)置不編譯指定的模塊
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
3磕仅、單獨(dú)使用group或module參數(shù)
exclude后的參數(shù)有g(shù)roup和module,可以分別單獨(dú)使用簸呈,會(huì)排除所有匹配項(xiàng)榕订。例如下面的腳本匹配了所有的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
單獨(dú)給某個(gè)模塊指定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
不同配置下的版本沖突
同樣的配置下的版本沖突蜕便,會(huì)自動(dòng)使用最新版劫恒;而不同配置下的版本沖突,gradle同步時(shí)會(huì)直接報(bào)錯(cuò)玩裙〖婷常可使用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版本不同,就會(huì)導(dǎo)致沖突螺垢。
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同步時(shí)會(huì)提示
Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app and test app differ.
執(zhí)行dependencies會(huì)提示
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
4喧务、不兼容
雖然可以通過force、exclude等方式避免依賴項(xiàng)版本沖突枉圃,使得grade同步成功功茴,但是并不能代表編譯時(shí)沒有問題。由于不同版本可能不完全兼容孽亲,于是會(huì)出現(xiàn)各種奇怪的報(bào)錯(cuò)坎穿。已知的解決思路是更改包的版本、嘗試強(qiáng)制使用不同版本的依賴項(xiàng)返劲,找到可兼容的依賴組合玲昧。