MACOSX_DEPLOYMENT_TARGET值反复回退的Flutter macOS构建问题
解决Flutter macOS项目MACOSX_DEPLOYMENT_TARGET警告反复重置的问题
问题现象
构建面向macOS的Flutter应用时,添加just_audio等依赖后出现多处MACOSX_DEPLOYMENT_TARGET版本不符合要求的警告。在Xcode中手动修改该值后应用可正常编译,但不久警告重现,Xcode中的设置也会恢复原状。尝试过10.15到13.1多个目标版本,问题仍反复出现。
警告示例:
Launching lib/main.dart on macOS in debug mode... --- xcodebuild: WARNING: Using the first of multiple matching destinations: { platform:macOS, arch:arm64, id:00006000-000210D03EB8401E } { platform:macOS, arch:x86_64, id:00006000-000210D03EB8401E } /Users/foo/projects/just_audio_background_test/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.13 to 13.1.99. (in target 'FMDB' from project 'Pods') /Users/foo/projects/just_audio_background_test/macos/Pods/Pods.xcodeproj: warning: The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 10.11, but the range of supported deployment target versions is 10.13 to 13.1.99. (in target 'sqflite' from project 'Pods') /Users/foo/projects/just_audio_background_test/macos/Pods/Pods.xcodeproj: warning: The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 10.11, but the range of supported deployment target versions is 10.13 to 13.1.99. (in target 'audio_session' from project 'Pods') /Users/foo/projects/just_audio_background_test/macos/Pods/Pods.xcodeproj: warning: The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 10.12.2, but the range of supported deployment target versions is 10.13 to 13.1.99. (in target 'audio_service' from project 'Pods') /Users/foo/projects/just_audio_background_test/macos/Runner.xcodeproj: warning: The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 10.11, but the range of supported deployment target versions is 10.13 to 13.1.99. (in target 'Flutter Assemble' from project 'Runner') /Users/foo/projects/just_audio_background_test/macos/Pods/Pods.xcodeproj: warning: The macOS deployment target 'MACOSX_DEPLOYMENT_TARGET' is set to 10.11, but the range of supported deployment target versions is 10.13 to 13.1.99. (in target 'FlutterMacOS' from project 'Pods')
环境信息:
[✓] Flutter (Channel stable, 3.3.10, on macOS 13.1 22C65 darwin-arm, locale en-TW) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 14.2) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.3) [✓] VS Code (version 1.74.1) [✓] Connected device (2 available) [✓] HTTP Host Availability
解决方案
直接在Xcode中修改Pod项目的部署目标无效,因为每次flutter pub get或CocoaPods重新安装依赖时,Pod的配置会被重新生成,覆盖手动修改的设置。需要通过配置文件强制固定部署目标:
1. 固定Runner项目的部署目标
打开项目的macos/Runner.xcodeproj,在Xcode中设置Runner的Deployment Target为你需要的版本(比如10.13或更高)。这一步是基础,确保主项目的目标版本符合要求。
2. 修改Podfile添加post_install钩子
打开项目的macos/Podfile,在文件末尾添加以下代码,强制所有Pod依赖使用和Runner一致的部署目标:
post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| # 读取Runner的部署目标版本 runner_target = installer.pods_project.targets.detect { |t| t.name == 'Runner' } if runner_target config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = runner_target.build_configurations.first.build_settings['MACOSX_DEPLOYMENT_TARGET'] end end end end
如果希望手动指定固定版本(比如10.13),也可以直接写死:
post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '10.13' end end end
3. 清理缓存并重新安装依赖
执行以下命令清理旧的Pod配置和Flutter缓存:
# 进入macos目录 cd macos # 删除Pod相关文件 rm -rf Pods Podfile.lock # 重新安装Pod依赖 pod install # 返回项目根目录 cd .. # 清理Flutter缓存 flutter clean # 重新获取依赖 flutter pub get
4. 重新编译项目
执行flutter run重新编译,警告应该不会再出现。
原理说明
Flutter的macOS项目依赖CocoaPods管理原生依赖,每个Pod包自身会指定默认的MACOSX_DEPLOYMENT_TARGET。通过Podfile的post_install钩子,可以在每次安装Pod后统一修改所有目标的部署版本,避免手动修改被覆盖。
内容的提问来源于stack exchange,提问作者Christopher Perry
相关产品推荐
相关产品推荐

