构建Flutter macOS应用时如何修改MACOSX_DEPLOYMENT_TARGET
macOS端Flutter release构建时MACOSX_DEPLOYMENT_TARGET版本警告修复
问题环境
- 硬件:M1芯片Mac设备
- 系统版本:macOS v12.4
- Flutter版本:v3.0.4(FVM管理)
- 构建命令:
~/fvm/versions/3.0.4/bin/flutter build macos --release
问题现象
构建过程中输出如下警告:
💪 Building with sound null safety 💪 Running pod install... 668ms --- xcodebuild: WARNING: Using the first of multiple matching destinations: { platform:macOS, arch:arm64, id:00006000-001248980AE2801E } { platform:macOS, arch:x86_64, id:00006000-001248980AE2801E } /Users/xxx/project/macos/Pods/Pods.xcodeproj: warning: The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 10.6, but the range of supported deployment target versions is 10.9 to 12.3.99. (in target 'FMDB' from project 'Pods') Building macOS application...
警告核心为:Pods项目下FMDB目标的MACOSX_DEPLOYMENT_TARGET被设置为10.6,超出当前Xcode支持的10.9~12.3.99部署目标版本范围。
已尝试操作
- 手动将Pods和Runner的部署目标修改为10.11
- 在Xcode中完成Pods的macOS部署配置
- Podfile中已配置
platform :osx, '10.11',完整Podfile配置如下:
platform :osx, '10.11' # 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', 'ephemeral', '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 Flutter-Generated.xcconfig, then run \"flutter pub get\"" end require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) flutter_macos_podfile_setup target 'Runner' do use_frameworks! use_modular_headers! flutter_install_all_macos_pods File.dirname(File.realpath(__FILE__)) end post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_macos_build_settings(target) end end
- 清理构建缓存后重新构建,警告仍然存在
问题原因
手动修改Xcode内Pods配置、仅在Podfile头部声明platform版本都不会永久生效。每次执行pod install时,CocoaPods会根据各个依赖库自身podspec中声明的最低部署版本重新生成Pods项目配置,部分老旧依赖(如本次的FMDB)自身声明的最低部署版本为10.6,会直接覆盖手动修改的配置。
修复步骤
- 打开项目
macos目录下的Podfile文件,找到原有post_install代码块,替换为如下内容,在钩子中强制统一所有依赖target的部署目标版本:
post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_macos_build_settings(target) # 强制所有Pods依赖使用10.11作为最低macOS部署版本 target.build_configurations.each do |config| config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '10.11' end end end
- 清理旧的构建产物和Pods生成文件,在项目根目录依次执行以下命令:
# 清理Flutter构建缓存 flutter clean # 删除macos目录下旧的Pods相关生成文件 rm -rf macos/Pods macos/Podfile.lock macos/Pods.xcodeproj # 重新拉取依赖 flutter pub get # 重新执行release构建 ~/fvm/versions/3.0.4/bin/flutter build macos --release
补充说明
构建日志中同时出现arm64、x86_64多个匹配目标的警告是M1芯片设备上Xcode的正常提示,不影响最终构建产物,无需额外处理。
不要直接修改Pods目录下的任何配置文件,所有自定义规则必须写在Podfile中,否则下次执行pod install时手动修改的内容会被自动覆盖。
内容的提问来源于stack exchange,提问作者Dolphin
相关产品推荐
相关产品推荐

