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

如何在Flutter应用中同时使用谷歌云项目与不同Firebase项目?

同时使用Google People API与Firebase Analytics的冲突解决

需求

在公司项目中,需通过OAuth从谷歌云访问People API,同时集成Firebase Analytics功能。

问题

通过OAuth访问People API时出现以下错误:

google_sign_in plugin:PlatformException(sign_in_failed,com.google.android.gms.common.api.ApiException: 10: , null)
该问题仅在应用级和项目级gradle文件中添加Google Play Services依赖时触发(按Firebase Android SDK指引配置后出现)。

已尝试方案

  • 在谷歌云凭据页面添加了应用所有可能的SHA密钥
  • 配置OAuth支持邮箱登录
  • 移除gradle中的Google Play Services依赖后,google_sign_in问题解决,但Firebase Analytics停止工作,报错:

Missing google_app_id. Firebase Analytics disabled. See shortened_link_that_I_cant_post

当前状态

只能二选一使用服务,已暂时移除Firebase并在控制台删除应用。

涉及依赖包

  • google_sign_in: ^5.4.3
  • extension_google_sign_in_as_googleapis_auth: ^2.0.7
  • googleapis: ^10.0.0
  • firebase_core、firebase_analytics

解决方案

1. 统一依赖版本,避免冲突

项目级build.gradle中指定统一的Google Play Services和Firebase BOM版本:

buildscript {
    ext {
        googlePlayServicesVersion = "20.1.0"
        firebaseBomVersion = "32.3.1"
    }
    // 其他原有配置...
}

应用级build.gradle通过Firebase BOM引入Analytics,避免手动指定版本导致的冲突:

dependencies {
    implementation platform('com.google.firebase:firebase-bom:$firebaseBomVersion')
    implementation 'com.google.firebase:firebase-analytics'
    // 其他原有依赖...
}

2. 同步OAuth凭据与Firebase配置

  • 重新在Firebase控制台添加应用,下载最新的google-services.json替换到android/app目录
  • 确认谷歌云控制台的OAuth 2.0客户端ID中,包含Firebase自动生成的SHA-1和SHA-256密钥(可在Firebase项目设置→常规→应用签名中查看)

3. 正确配置Google Sign-In作用域

初始化GoogleSignIn时明确指定People API所需的作用域:

final GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: [
    'email',
    'https://www.googleapis.com/auth/contacts.readonly', // 根据实际需求调整作用域
  ],
);

4. 清理重建项目

  • 执行flutter clean后重新运行flutter pub get
  • 在Android Studio中选择File → Invalidate Caches / Restart,清除缓存后重新构建项目

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 12:05:21