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

添加Google Services插件后Flutter项目报Multiple dex定义冲突错误

解决Flutter Firebase添加google-services插件后的Dex冲突问题

这个问题我之前帮好几个开发者搞定过,本质是依赖版本不统一导致的重复Dex类定义,尤其是Google Play Services相关的库互相"打架"了。下面是一步步的解决办法:

  • 统一Google Play Services/Firebase版本
    打开项目根目录的build.gradle(不是app模块下的那个),在allprojects块里添加版本强制规则,确保所有相关库用同一个版本:

    allprojects {
        repositories {
            // 保留你原本的仓库配置
        }
        configurations.all {
            resolutionStrategy {
                // 替换成当前兼容的稳定版本号,可参考Flutter Firebase文档匹配
                force 'com.google.android.gms:play-services-base:17.6.0'
                force 'com.google.firebase:firebase-core:19.0.0'
                // 如果你用了其他Firebase服务(比如auth、firestore),也添加对应的force规则
            }
        }
    }
    
  • 开启Multidex(针对minSdkVersion<21的情况)
    如果你的app最低支持版本是20或更低,多个dex文件会触发这个问题,需要开启Multidex:

    1. 在app模块的build.gradle的defaultConfig里添加:
      defaultConfig {
          // 其他配置不变
          multiDexEnabled true
      }
      
    2. 添加Multidex依赖:
      dependencies {
          // 其他依赖不变
          implementation 'com.android.support:multidex:1.0.3'
      }
      
    3. 如果是自定义Application类,让它继承MultiDexApplication;如果没有自定义,就在AndroidManifest.xml的application标签里指定android:name="android.support.multidex.MultiDexApplication"。
  • 彻底清理缓存并重建
    旧缓存经常会搞事情,执行以下命令清干净:

    flutter clean
    cd android && ./gradlew clean
    cd .. && flutter pub get
    

    之后重新编译项目试试。

  • 更新Flutter Firebase插件
    确保pubspec.yaml里的所有Firebase插件都是最新稳定版,避免插件之间依赖的原生库版本不一致:

    dependencies:
      firebase_core: ^2.15.0
      firebase_auth: ^4.10.0
      # 其他Firebase插件也同步更新
    

    运行flutter pub upgrade把插件更到最新。

  • 排查并移除重复依赖
    用./gradlew app:dependencies命令查看app模块的依赖树,找到重复引入的Google Play Services库。如果是某个第三方插件带进来的,就用exclude移除重复部分:

    dependencies {
        implementation('有问题的第三方插件') {
            exclude group: 'com.google.android.gms'
        }
    }
    

按照这些步骤走下来,基本能解决这个Dex冲突问题。要是还不行,可以把依赖树的输出贴出来,我再帮你深挖一下。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 10:00:14