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

iOS生产环境Flutter应用Google登录崩溃问题排查

iOS Prod Flavor Google Sign-In Crash Troubleshooting

核心问题分析

你的dev flavor正常但prod flavor崩溃,大概率是GoogleSignIn的clientId与当前flavor的Firebase配置不匹配,或是Xcode环境变量未正确传递导致URL Scheme/配置文件出错。以下是针对性的排查和修复步骤:


1. 确认Firebase Options的Flavor区分逻辑

你当前使用DefaultFirebaseOptions.currentPlatform.iosClientId初始化GoogleSignIn,需确保这个配置能根据flavor动态切换到prod的clientId:

修复操作:

修改lib/firebase_options.dart(或你的配置文件),添加flavor判断逻辑:

import 'package:flutter/foundation.dart';

FirebaseOptions getFirebaseOptions() {
  const flavor = String.fromEnvironment('FLAVOR');
  
  // 替换为你实际的prod/dev Firebase配置
  if (flavor == 'prod') {
    return FirebaseOptions(
      iosClientId: 'com.googleusercontent.apps.xxxxxx-prod', // prod的CLIENT_ID(非REVERSED)
      apiKey: 'AIzaSyxxxxxx-prod',
      appId: '1:xxxxxx-prod:ios:xxxxxx',
      projectId: 'your-prod-project-id',
      messagingSenderId: 'xxxxxx-prod',
      storageBucket: 'your-prod-project.appspot.com',
    );
  } else {
    return FirebaseOptions(
      iosClientId: 'com.googleusercontent.apps.xxxxxx-dev', // dev的CLIENT_ID(非REVERSED)
      apiKey: 'AIzaSyxxxxxx-dev',
      appId: '1:xxxxxx-dev:ios:xxxxxx',
      projectId: 'your-dev-project-id',
      messagingSenderId: 'xxxxxx-dev',
      storageBucket: 'your-dev-project.appspot.com',
    );
  }
}

然后在main函数中初始化Firebase:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: getFirebaseOptions());
  runApp(const MyApp());
}

2. 验证Xcode User-Defined变量与Scheme的关联

检查prod scheme的Build Settings是否正确加载了prod的GOOGLE_SERVICE_REVERSED_CLIENT_ID:

  • 打开Xcode,切换到prod scheme
  • 点击Runner -> Build Settings -> 搜索User-Defined
  • 确认GOOGLE_SERVICE_REVERSED_CLIENT_ID对应的Debug-prod/Release-prod值是prod的REVERSED_CLIENT_ID(从prod的GoogleService-Info.plist中获取)
  • 同时删除ios/Flutter/Debug.xcconfig和Release.xcconfig中硬编码的GOOGLE_SERVICE_REVERSED_CLIENT_ID,避免覆盖User-Defined变量

3. 确保GoogleService-Info.plist与Flavor绑定

你当前ios/Runner目录下没有配置文件,需为每个flavor关联对应的配置文件:

  1. 将prod的GoogleService-Info.plist重命名为GoogleService-Info-prod.plist,dev的重命名为GoogleService-Info-dev.plist,放在ios根目录
  2. 在Xcode中,为prod scheme添加Run Script Build Phase:
    • 点击Runner -> Build Phases -> 点击+ -> 选择New Run Script Phase
    • 输入脚本:
      cp "${PROJECT_DIR}/GoogleService-Info-prod.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"
      
  3. 对dev scheme重复上述步骤,替换为GoogleService-Info-dev.plist

4. 调试崩溃日志定位根因

如果以上步骤仍未解决,通过Xcode查看崩溃详情:

  • 打开Xcode,切换到prod scheme,点击运行
  • 崩溃后,点击Window -> Devices and Simulators -> 选中你的设备 -> 点击View Device Logs
  • 查找最新的崩溃记录,重点看是否有Invalid client ID、URL scheme not found等关键词,直接定位问题点

关键注意点

  • GoogleSignIn的clientId参数需要的是GoogleService-Info.plist中的CLIENT_ID(非REVERSED_CLIENT_ID),而info.plist中的CFBundleURLSchemes需要的是REVERSED_CLIENT_ID,二者必须属于同一个Firebase项目
  • 编译时需确保flavor参数正确传递,比如运行prod的命令:flutter run --flavor prod --release

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 07:05:13