MacBook 2017使用Xcode 15.2构建Flutter iOS项目失败,降级Firebase SDK仍未解决
我之前在Intel款MacBook上用Xcode 15.2开发Flutter项目时,也遇到过几乎一模一样的Firebase编译错误,结合你的情况,给你几个具体的排查和解决方向:
1. 修复Podfile的配置冲突问题
从你的Podfile看,给每个Firebase、Google相关的Pod单独加:modular_headers => true,同时在target里又用了use_modular_headers!,这两种设置可能会冲突,导致Pods的编译环境异常。建议调整Podfile为以下结构:
# Uncomment this line to define a global platform for your project platform :ios, '15.0' # 固定Firebase SDK版本,选一个和Xcode15.2兼容的稳定版 $FirebaseSDKVersion = '10.17.0' # CocoaPods analytics sends network stats synchronously affecting flutter build latency. ENV['COCOAPODS_DISABLE_STATS'] = 'true' project 'Runner', { 'Debug' => :debug, 'Profile' => :release, 'Release' => :release, } def flutter_root generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) unless File.exist?(generated_xcode_build_settings_path) raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" end File.foreach(generated_xcode_build_settings_path) do |line| matches = line.match(/FLUTTER_ROOT\=(.*)/) return matches[1].strip if matches end raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" end require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) flutter_ios_podfile_setup target 'Runner' do use_frameworks! use_modular_headers! # 统一启用模块化头文件,无需单独给每个Pod设置 # 只声明核心依赖即可,不需要手动引入依赖链上的子Pod(比如FirebaseCore、GoogleUtilities等) pod 'Firebase/Analytics', :version => $FirebaseSDKVersion pod 'Firebase/Crashlytics', :version => $FirebaseSDKVersion pod 'Firebase/Messaging', :version => $FirebaseSDKVersion pod 'GoogleMaps', '~> 7.4.0' # 匹配Xcode15.2的GoogleMaps版本 flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) target 'RunnerTests' do inherit! :search_paths end end post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) # 强制给Firebase/Google相关Target设置正确的Swift版本 if target.name.start_with?('Firebase') || target.name.start_with?('Google') || target.name == 'nanopb' target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '5.9' # Xcode15.2默认支持的Swift版本 config.build_settings['CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES'] = 'YES' end end end end
2. 彻底清理缓存后重新构建
很多时候缓存会导致版本不匹配的问题,按以下步骤操作:
- 关闭Xcode
- 删除项目根目录的
Podfile.lock、Pods文件夹、Runner.xcworkspace文件 - 运行
flutter clean清理Flutter缓存 - 运行
flutter pub get重新生成Flutter配置 - 运行
pod install --repo-update更新Pod仓库并重新安装依赖
3. 检查Xcode项目的核心设置
打开Runner.xcworkspace后,确认以下设置:
- 选中
RunnerTarget,进入Build Settings,搜索Swift Language Version,设置为Swift 5.9 - 搜索
Deployment Target,确认iOS版本为15.0,和Podfile保持一致 - 搜索
Enable Modules (C and Objective-C),设置为YES
错误原因说明
你遇到的Cannot find 'nonisolated' in scope、Expressions are not allowed at the top level等错误,本质是Firebase SDK版本与Xcode 15.2的Swift版本不兼容:nonisolated、unsafe是Swift 5.9的新特性,但如果Pods中的Firebase编译时使用了错误的Swift版本,或者SDK版本本身引入了更高Swift版本的语法,就会出现编译器“不认识”这些关键字的情况。降级到10.17.0版本的Firebase SDK是因为这个版本是官方明确兼容Xcode 15早期版本的稳定版,不会出现这类语法冲突。
如果以上方法都不行,可以尝试使用Invertase提供的预编译Firebase框架,比如在Podfile中替换FirebaseFirestore的依赖(如果你用到了Firestore):
pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '10.17.0'
内容来源于stack exchange

