Firebase引用版本冲突解决:Android dependency 'com.google.android.gms:play-services-basement' has different

  • 前两天在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 回车之后等一会儿就可以查看到整个项目的依赖树结构了。

Firebase引用版本冲突解决:Android dependency 'com.google.android.gms:play-services-basement' has different_第1张图片
Firebase引用版本冲突解决:Android dependency 'com.google.android.gms:play-services-basement' has different_第2张图片

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

你可能感兴趣的:(React,Native,Android开发,Android,Studio)