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

Flutter集成Firebase编译报错:无法解析grpc-stub:1.50.2依赖

问题描述

离开Flutter和Firebase开发一段时间后,回归完成项目时,按照以往操作及官方文档集成两者,项目无法编译,报错信息如下:

Could not resolve io.grpc:grpc-stub:1.50.2.
Required by:
    project :app > project :cloud_firestore > com.google.firebase:firebase-firestore:24.4.2
 > Could not resolve io.grpc:grpc-stub:1.50.2.
    > Could not parse POM https://repo.maven.apache.org/maven2/io/grpc/grpc-stub/1.50.2/grpc-stub-1.50.2.pom
       > Content is not allowed in prolog.

我的app/build.gradle配置如下:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:31.2.0')
    implementation 'com.google.firebase:firebase-analytics-ktx'
}

移除Firebase相关依赖后项目可正常运行,尝试搜索但无头绪,附上pubspec.yaml配置寻求帮助:

version: 1.0.0+1

environment:
  sdk: '>=2.18.2 <3.0.0'

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
  flutter:
    sdk: flutter


  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^1.0.2
  firebase_core: ^2.4.1
  firebase_auth: ^4.2.5
  cloud_firestore: ^4.3.1

dev_dependencies:
  flutter_test:
    sdk: flutter

  # The "flutter_lints" package below contains a set of recommended lints to
  # encourage good coding practices. The lint set provided by the package is
  # activated in the `analysis_options.yaml` file located at the root of your
  # package. See that file for information about deactivating specific lint
  # rules and activating additional ones.
  flutter_lints: ^2.0.0
解决方案

这个错误源于Maven仓库的POM文件解析失败,大概率是网络问题导致文件下载不完整、本地缓存损坏,或是依赖版本兼容问题,按以下步骤处理:

  1. 清理Gradle缓存并刷新依赖
    执行命令:

    ./gradlew clean build --refresh-dependencies
    

    也可以手动删除本地Gradle缓存目录:

    • Windows:C:\Users\<你的用户名>\.gradle\caches
    • macOS/Linux:~/.gradle/caches
  2. 升级Firebase依赖版本
    当前使用的Firebase BOM和Flutter端Firebase包版本较旧,可能存在依赖链兼容问题:

    • 修改pubspec.yaml中的Firebase依赖为最新稳定版(可通过flutter pub outdated查看更新),示例:
      firebase_core: ^2.24.2
      firebase_auth: ^4.16.0
      cloud_firestore: ^4.13.6
      
    • 同步更新app/build.gradle中的Firebase BOM版本,示例:
      implementation platform('com.google.firebase:firebase-bom:32.7.0')
      
  3. 配置国内Maven镜像(若网络受限)
    如果无法正常访问Maven中央仓库,在项目根目录的build.gradle中添加阿里云镜像:

    buildscript {
        repositories {
            maven { url 'https://maven.aliyun.com/repository/public' }
            google()
            mavenCentral()
        }
        // 保留原有其他配置
    }
    
    allprojects {
        repositories {
            maven { url 'https://maven.aliyun.com/repository/public' }
            google()
            mavenCentral()
        }
    }
    
  4. 验证POM文件可访问性
    手动打开报错中的POM链接,确认能正常加载并下载文件。如果无法访问,说明网络存在拦截,需调整网络环境。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 07:15:32