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关联对应的配置文件:
- 将prod的
GoogleService-Info.plist重命名为GoogleService-Info-prod.plist,dev的重命名为GoogleService-Info-dev.plist,放在ios根目录 - 在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"
- 点击
- 对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
相关产品推荐
相关产品推荐

