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

在Codemagic构建多Firebase项目的Flutter iOS应用失败求助

解决Codemagic上iOS Flavor构建时GoogleService-Info.plist找不到的问题

1. 修正预构建脚本的路径逻辑

本地Xcode脚本的相对路径在Codemagic环境中可能失效,因为Codemagic的工作目录是仓库根目录。调整脚本路径为基于仓库根的绝对相对路径:

# 替换原脚本中的路径逻辑,确保指向正确的文件位置
SRC="${CM_BUILD_DIR}/ios/config/GoogleService-Info-${FLAVOR}.plist"
DEST="${CM_BUILD_DIR}/ios/Runner/GoogleService-Info.plist"

cp "$SRC" "$DEST"

如果不确定FLAVOR变量是否正确传递,可以直接在脚本里打印验证:echo "Current flavor: $FLAVOR"

2. 在Codemagic中手动添加前置脚本

如果Xcode自带的预构建脚本在Codemagic中未触发,直接在Codemagic构建流程里添加Pre-build脚本:

  • 打开Codemagic构建配置,找到「Pre-build script」步骤
  • 粘贴以下脚本(根据你的Flavor名称调整文件名):
# 根据Flavor复制对应的plist文件
if [ "$FLAVOR" = "production" ]; then
  cp ios/config/GoogleService-Info-Production.plist ios/Runner/GoogleService-Info.plist
elif [ "$FLAVOR" = "development" ]; then
  cp ios/config/GoogleService-Info-Development.plist ios/Runner/GoogleService-Info.plist
else
  echo "Unknown flavor: $FLAVOR"
  exit 1
fi

# 验证文件是否复制成功
if [ -f ios/Runner/GoogleService-Info.plist ]; then
  echo "✅ GoogleService-Info.plist copied for flavor $FLAVOR"
else
  echo "❌ Failed to copy GoogleService-Info.plist"
  exit 1
fi
  • 在Codemagic的「Environment variables」中添加FLAVOR变量,值设为production或development,或者根据构建触发条件动态赋值

3. 检查Xcode项目的文件引用

确保Xcode中Runner目标的资源引用路径正确:

  • 打开Xcode,选中Runner目标 → 进入「Build Phases」→ 找到「Copy Bundle Resources」
  • 移除现有GoogleService-Info.plist引用,重新从ios/Runner/目录添加该文件,确保使用相对路径而非绝对路径

4. 调试文件存在性

在Pre-build脚本中添加调试命令,确认文件在Codemagic环境中存在:

# 列出config目录下的所有文件
ls -la ios/config/
# 列出Runner目录下的所有文件
ls -la ios/Runner/

构建后查看日志,确认目标plist文件存在,且复制后Runner目录中出现对应文件

5. 适配Flutter端Firebase初始化

确保Flutter代码根据Flavor加载对应配置,避免依赖单一plist:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  const flavor = String.fromEnvironment('FLAVOR');
  
  if (flavor == 'production') {
    await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  } else if (flavor == 'development') {
    await Firebase.initializeApp(options: DefaultFirebaseOptions.development);
  }
  
  runApp(const MyApp());
}

(注:需通过flutterfire configure命令生成对应Flavor的DefaultFirebaseOptions)


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 05:15:22