Moodle移动Ionic应用集成FCM时遇googleServices扩展重复错误
解决Cordova构建Android时googleServices扩展重复注册问题
问题核心
执行cordova build android时触发构建失败,错误提示为:
Failed to apply plugin 'com.google.gms.google-services'. Cannot add extension with name 'googleServices', as there is an extension already registered with that name.
原因是多个Cordova插件(如FCM推送插件和其他Firebase相关插件)重复向Gradle配置中添加了google-services插件,导致冲突。
解决方案
1. 手动清理重复配置(临时解决)
- 打开
platforms/android/app/build.gradle文件 - 搜索所有包含
apply plugin: 'com.google.gms.google-services'的代码行,只保留最后一行,删除其余重复的引用 - 同时检查项目根目录的
build.gradle,确保dependencies区块中classpath 'com.google.gms:google-services:x.x.x'仅出现一次
2. 用Cordova钩子自动处理(持久化解决)
避免每次重新添加平台后手动修改,创建钩子脚本自动清理重复配置:
- 在项目根目录创建
hooks/after_prepare/01_remove_duplicate_google_services.js文件 - 写入以下代码:
const fs = require('fs'); const path = require('path'); module.exports = function(context) { const buildGradlePath = path.join(context.opts.projectRoot, 'platforms/android/app/build.gradle'); let buildGradleContent = fs.readFileSync(buildGradlePath, 'utf8'); // 移除重复的google-services插件引用,仅保留最后一个 const regex = /apply plugin: 'com.google.gms.google-services'\n/g; const matches = buildGradleContent.match(regex); if (matches && matches.length > 1) { buildGradleContent = buildGradleContent.replace(regex, ''); buildGradleContent += '\napply plugin: \'com.google.gms.google-services\'\n'; fs.writeFileSync(buildGradlePath, buildGradleContent); console.log('已移除重复的google-services插件配置'); } };
- 给脚本添加执行权限:
chmod +x hooks/after_prepare/01_remove_duplicate_google_services.js
3. 检查插件依赖兼容性
- 执行
cordova plugin list查看已安装插件,确认是否存在多个Firebase相关插件 - 确保所有Google服务相关插件使用的版本一致,避免因版本差异导致的重复配置冲突
4. 重新构建项目
cordova platform remove android cordova platform add android cordova build android
内容的提问来源于stack exchange,提问作者egp_s2s
相关产品推荐
相关产品推荐

