You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级cordova-android到10.1.1后安卓发布构建失败如何解决

解决方案

  • 第一步:移除重复的Google Services插件引入配置
    先删除config.xml中你新增的以下两行配置:

    <preference name="GradlePluginGoogleServicesEnabled" value="true" />
    <preference name="GradlePluginGoogleServicesVersion" value="4.3.8" />
    

    对应的重复注册报错是因为Cordova自带的Google Services插件引入逻辑,和其他第三方Cordova插件内部自带的引入逻辑冲突,导致同一插件被重复注册。

  • 第二步:统一google-services gradle插件版本
    本次初始报错的根源是低于4.3.8版本的google-services gradle插件不兼容cordova-android 10.1.1内置的Gradle 7+版本。你可以通过添加Cordova构建钩子的方式强制统一版本:

    1. 在项目根目录下的hooks文件夹新建before_build/010_fix_google_services_version.js文件
    2. 写入以下逻辑:
    const fs = require('fs');
    const path = require('path');
    
    module.exports = function(ctx) {
      const buildGradlePath = path.join(ctx.opts.projectRoot, 'platforms/android/build.gradle');
      let buildGradleContent = fs.readFileSync(buildGradlePath, 'utf8');
      // 替换google-services版本为兼容Gradle7的版本
      buildGradleContent = buildGradleContent.replace(
        /com\.google\.gms:google-services:\d+\.\d+\.\d+/g,
        'com.google.gms:google-services:4.3.14'
      );
      fs.writeFileSync(buildGradlePath, buildGradleContent, 'utf8');
    }
    
    1. 非Windows环境需要给该文件添加可执行权限。
  • 第三步:清理缓存并重新构建
    执行以下命令清理旧缓存,避免残留配置影响:

    cordova platform rm android
    rm -rf node_modules package-lock.json
    npm install
    cordova platform add android@10.1.1
    cordova build android --release --buildconfig=build.json
    

如果上述操作后仍报错,逐个检查你安装的第三方Cordova插件的plugin.xml以及插件内的gradle配置文件,删除所有自定义的apply plugin: 'com.google.gms.google-services'逻辑,统一通过上述钩子管理版本即可。

内容的提问来源于stack exchange,提问作者Matheus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 03:24:00