我正在尝试构建一个针对macOS的Flutter应用程序。在添加了一些依赖项(如just_audio)后,我收到了关于MACOSX_DEPLOYMENT_TARGET
在不同位置被设置为不同值的警告,这些值需要更改为其他值。
所以,我打开Xcode,认真地按照指示设置值。在此之后,应用程序成功编译而没有警告,并按预期执行一次,两次,甚至三次,但不可避免地会返回警告。当我回到Xcode时,我发现所有的值都恢复到了原来的样子。我尝试了不同的目标版本,从10.15到13.1(当前安装),但警告最终总是回来。
我错过了重要的一步吗?我对软件开发并不陌生,但我对macOS和Xcode完全陌生。
下面是一个不断出现的错误示例:
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 doctor
输出:
[✓] 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
原因是因为当您尝试运行或构建时,flutter将执行pod install
。在Podfile
中编辑post_install
,如下所示。添加的代码将在flutter执行pod install
时强制部署目标为10.13。
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_macos_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '10.13'
end
end
end
完整示例如下
platform :osx, '10.13'
# 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)
target.build_configurations.each do |config|
config.build_settings['MACOSX_DEPLOYMENT_TARGET'] = '10.13'
end
end
end
比上面的答案更可靠的解决方案是:
post_install do |installer|
# Ensure pods also use the minimum deployment target set above
# https://stackoverflow.com/a/64385584/436422
puts 'Determining pod project minimum deployment target'
pods_project = installer.pods_project
deployment_target_key = 'MACOSX_DEPLOYMENT_TARGET'
deployment_targets = pods_project.build_configurations.map{ |config| config.build_settings[deployment_target_key] }
minimum_deployment_target = deployment_targets.min_by{ |version| Gem::Version.new(version) }
puts 'Minimal deployment target is ' + minimum_deployment_target
puts 'Setting each pod deployment target to ' + minimum_deployment_target
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings[deployment_target_key] = minimum_deployment_target
end
end
end
基于对iOS的类似问题的回答。