• 前两天在RN项目中集成原生的firebase,之后报错插件版本冲突,报错信息如下:

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. 方法一:

  • 在项目的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. 方法三:

  • 各个module的dependencies里的compile改为implementation
    然而,并没有什么卵用

解决方法

  • 之后静下心来好好思考了下,依赖版本冲突,肯定是存在重复依赖的问题。从这个思路着手:

第一步:

  • 将firebase的依赖版本更新到最新版,这样能尽可能的降低版本冲突的概率;
    参考firebase插件的各个发布版本
    https://firebase.google.com/support/release-notes/android#20180523

第二步:

  • 去除依赖冲突 参考 https://blog.csdn.net/yyo201/article/details/80570741
    在Teminal 里面输入 gradlew app:dependencies 回车之后等一会儿就可以查看到整个项目的依赖树结构了。


  • 在依赖树结构里搜索出现版本冲突的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
1、Force

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
2、Exclude

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
3、单独使用group或module参数

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
4、不兼容

虽然可以通过force、exclude等方式避免依赖项版本冲突,使得grade同步成功,但是并不能代表编译时没有问题。由于不同版本可能不完全兼容,于是会出现各种奇怪的报错。已知的解决思路是更改包的版本、尝试强制使用不同版本的依赖项,找到可兼容的依赖组合。

参考:http://www.paincker.com/gradle-dependencies

更多相关文章

  1. 解决ionic在Android和iOS的一些样式上的冲突
  2. 5.4呈献:HP-Socket v5.3.1 支持 Android(安卓)NDK
  3. Android(安卓)7.0 以上安装Apk适配方案总结
  4. Android调试错误-No resource identifier found for attribute '
  5. Android(安卓)Studio创建项目Error:Server returned HTTP respon
  6. 高德Demo,网上找了很多资料都不适合,自己研究出一个Demo,非常适合入
  7. android Studio 引用fresco 出现的问题
  8. 创建maven-android工程
  9. 【android】Android(安卓)7.0适配步骤

随机推荐

  1. Android软键盘一些处理
  2. Launcher 安装APK快捷方式出现在末尾空白
  3. Android(安卓)- 判断当前网络环境、隐藏
  4. 移动架构39_RxAndroid二(变换调用:map、fla
  5. android wifi scan and auto re-connect
  6. Android(安卓)数据存储(二) 共享参数存储
  7. 这是我见过有关Android(安卓)RecyclerVie
  8. 我的Android进阶之旅------>android Toas
  9. android UVC h264 ffmpeg软解码
  10. Smali动态调试方法