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

添加Firebase后Flutter聊天应用编译失败求助

解决Flutter集成Firebase后找不到firebase-annotations-16.1.0.jar的编译错误

问题根源

firebase-annotations:16.1.0是已过时的旧版本,大概率是项目中某个依赖间接引入的,与你当前使用的Firebase BOM(31.1.0)版本不兼容,同时Gradle插件、Kotlin版本与Firebase工具链的兼容性也可能加剧了这个问题。

具体修复步骤

1. 升级Firebase BOM版本

Firebase BOM会统一管理所有Firebase依赖的版本,使用最新兼容的BOM版本可以避免版本冲突。修改app/build.gradle中的BOM版本:

// 替换为较新的稳定版,示例使用32.7.0
implementation platform('com.google.firebase:firebase-bom:32.7.0')

2. 升级Google Services插件版本

旧版google-services插件可能无法正确处理新版Firebase依赖的版本协调,修改项目根目录的build.gradle:

// 替换原google-services版本
classpath 'com.google.gms:google-services:4.4.1'

3. 强制统一firebase-annotations版本

在app/build.gradle的dependencies块中添加强制依赖,覆盖间接引入的旧版本:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:32.7.0')
    implementation 'com.google.firebase:firebase-analytics'
    // 强制使用兼容的firebase-annotations版本
    implementation 'com.google.firebase:firebase-annotations:18.0.0'
}

4. 清理Gradle缓存并重新同步

  • 执行Android Studio菜单栏的File > Invalidate Caches... > Invalidate and Restart
  • 或在终端运行以下命令:
flutter clean
flutter pub get
cd android
./gradlew clean
./gradlew build --refresh-dependencies

5. 匹配Kotlin与Gradle插件版本

当前你使用的Gradle插件7.1.2兼容Kotlin 1.7.x版本,可修改根目录build.gradle的Kotlin版本:

ext.kotlin_version = '1.7.20'

额外提示

如果需要集成聊天相关的Firebase功能(如Firestore、Auth、Cloud Messaging),直接通过BOM引入对应依赖即可,无需指定版本:

implementation 'com.google.firebase:firebase-firestore'
implementation 'com.google.firebase:firebase-auth'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 19:05:24