安装FCM插件时Cordova报错,Ionic构建遇Google服务版本冲突
解决Ionic构建时Google Play Services版本冲突问题
嘿,这个问题我之前帮不少开发者解决过,本质就是Google Services插件要求的版本和项目中引入的Play Services依赖版本不匹配,导致构建时抛出冲突错误。下面给你几个可行的解决方案:
方案1:统一所有Play Services依赖为插件要求的9.0.0版本
既然google-services插件明确要求9.0.0版本,那我们直接把所有冲突的依赖都改成这个版本就行:
- 找到项目中Android模块的
build.gradle文件(通常在platforms/android/app/目录下) - 找到所有以
com.google.android.gms开头的依赖项,把版本号从+或11.+改成9.0.0,修改后类似这样:implementation 'com.google.android.gms:play-services-auth:9.0.0' implementation 'com.google.android.gms:play-services-identity:9.0.0' implementation 'com.google.android.gms:play-services-location:9.0.0' - 注意:尽量避免使用
+这种动态版本号,它会自动拉取最新版本,很容易再次引发版本冲突。
方案2:升级Google Services插件到兼容更高Play Services版本的版本
如果你不想降级Play Services依赖,可以选择升级google-services插件,让它和你当前用的11.+版本兼容:
- 打开项目根目录下的
build.gradle文件 - 在
buildscript -> dependencies里找到google-services插件的依赖,把版本号升级到和Play Services 11.+匹配的版本(比如3.1.0及以上),修改后如下:
小提示:插件版本和Play Services版本有对应关系,只要保证插件版本不低于依赖的Play Services主版本对应的最低插件版本即可。buildscript { dependencies { // 替换成兼容Play Services 11.+的插件版本 classpath 'com.google.gms:google-services:3.1.0' } }
方案3:用Gradle强制统一依赖版本(适合Cordova/Ionic插件引入的依赖)
如果这些Play Services依赖是通过Ionic/Cordova插件自动引入的,你可以通过自定义Gradle配置来强制统一版本:
- 在项目根目录创建
build-extras.gradle文件,添加以下内容:configurations.all { resolutionStrategy { force 'com.google.android.gms:play-services-auth:9.0.0' force 'com.google.android.gms:play-services-identity:9.0.0' force 'com.google.android.gms:play-services-location:9.0.0' } } - 打开
config.xml,在<platform name="android">标签内添加引用这个Gradle文件的配置:<platform name="android"> <gradle-file src="build-extras.gradle" /> </platform>
这样构建时就会强制使用你指定的版本,覆盖插件自动引入的版本。
总结一下,核心原则就是让google-services插件版本和所有Play Services依赖版本保持兼容,选其中一个方案执行后,重新运行ionic cordova build android应该就能解决问题了。
内容的提问来源于stack exchange,提问作者Alonetech
相关产品推荐
相关产品推荐

