使用 flutter 构建 iOS 项目时出现的问题
升级 Xcode 15 beta 5 后,无法构建项目。
显示错误
Firebase 1 issue DT_TOOLCHAIN_DIR cannot be used to evaluate LIBRARY_SEARCH_PATHS, use TOOLCHAIN_DIR instead
FirebaseAnalytics 1 issue DT_TOOLCHAIN_DIR cannot be used to evaluate LIBRARY_SEARCH_PATHS, use TOOLCHAIN_DIR instead
尝试分解、重新安装和更新 Pod,问题并未解决。
Flutter V. 3.10.6
有什么办法可以解决这个问题吗?
Xcode 2023 年 9 月 15 日解决方案
下面的解决方案应该处理以下所有问题
第1步:更新Podfile(Flutter / Xcode 15)
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
xcconfig_path = config.base_configuration_reference.real_path
xcconfig = File.read(xcconfig_path)
xcconfig_mod = xcconfig.gsub(/DT_TOOLCHAIN_DIR/, "TOOLCHAIN_DIR")
File.open(xcconfig_path, "w") { |file| file << xcconfig_mod }
end
end
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
end
end
end
第 2 步:更新构建阶段(扩展)
- 打开Xcode
- 单击项目导航器中的“Runner”(左上角)
- 单击侧栏中的“Targets ”->“Runner”
- 单击“Build Phases”(顶部栏)
- 将“Embed App Extension”(或“Embed Foundation Extension”或类似内容)移至“运行脚本”步骤(其中包括有关框架的代码)上方
如果这不起作用,请检查https://developer.apple.com/forums/thread/730974 了解更多想法。
清除一切
flutter clean
flutter pub get
cd ios && rm podfile.lock && arch -x86_64 pod install --repo-update
注意:此问题已在最新的 cocaopod 版本中得到解决。
看来 Cocoapods 问题已经解决了, 等待下一次公开发布 https://github.com/CocoaPods/CocoaPods/pull/12009
使用 Cocoapods 旧版本,您可以通过编辑 Podfile 来解决此问题,如下所示
post_install do |installer|
installer.aggregate_targets.each do |target|
target.xcconfigs.each do |variant, xcconfig|
xcconfig_path = target.client_root + target.xcconfig_relative_path(variant)
IO.write(xcconfig_path, IO.read(xcconfig_path).gsub("DT_TOOLCHAIN_DIR", "TOOLCHAIN_DIR"))
end
end
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if config.base_configuration_reference.is_a? Xcodeproj::Project::Object::PBXFileReference
xcconfig_path = config.base_configuration_reference.real_path
IO.write(xcconfig_path, IO.read(xcconfig_path).gsub("DT_TOOLCHAIN_DIR", "TOOLCHAIN_DIR"))
end
end
end
end
在发布修复程序之前,只需打开终端窗口并从项目文件夹运行以下命令:
find . -name "*.xcconfig" -type f -exec grep -l 'DT_TOOLCHAIN_DIR' {} \; \
| while IFS= read -r file; do sed -i '' 's/DT_TOOLCHAIN_DIR/TOOLCHAIN_DIR/g' "$file"; done
之后,错误消失并且不会返回,除非您再次运行pod update
或pod install
,在这种情况下,您必须再次运行该命令。
我通过以下方式解决了这些问题:
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
xcconfig_path = config.base_configuration_reference.real_path
xcconfig = File.read(xcconfig_path)
xcconfig_mod = xcconfig.gsub(/DT_TOOLCHAIN_DIR/, "TOOLCHAIN_DIR")
File.open(xcconfig_path, "w") { |file| file << xcconfig_mod }
end
end
end
如果您使用 inAppWebview 进行 flutter,则会出现如下错误:
Parse Issue (Xcode): Could not build module 'WebKit'
因此,您可以在 pubspec.yaml 文件中添加如下内容:
flutter_inappwebview:
git:
url: https://github.com/Estrelio/flutter_inappwebview.git
ref: fix-xcode-17
我通过以下方式解决了修改 pod_install 的问题:
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
xcconfig_path = config.base_configuration_reference.real_path
xcconfig = File.read(xcconfig_path)
xcconfig_mod = xcconfig.gsub(/DT_TOOLCHAIN_DIR/, "TOOLCHAIN_DIR")
File.open(xcconfig_path, "w") { |file| file << xcconfig_mod }
end
end
结尾
我保留了这行代码:
flutter_additional_ios_build_settings(target)
因为没有它我发现了其他问题,例如:
Swift Compiler Error (Xcode): No such module 'Flutter'
/Users/xxxxxxxxxx/.pub-cache/hosted/pub.dev/modal_progress_hud_nsn-0.4.0/ios/Classes/ModalProgressHudNsnPlugin.swift:0:7
Encountered error while building for device.
对我来说,我必须结合几个解决方案 - 其中之一是将最低部署目标提高到 13。最终运行的 pod_install 看起来像这样
post_install do |installer|
installer.aggregate_targets.each do |target|
target.xcconfigs.each do |variant, xcconfig|
xcconfig_path = target.client_root + target.xcconfig_relative_path(variant)
IO.write(xcconfig_path, IO.read(xcconfig_path).gsub("DT_TOOLCHAIN_DIR", "TOOLCHAIN_DIR"))
end
end
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
if config.base_configuration_reference.is_a? Xcodeproj::Project::Object::PBXFileReference
xcconfig_path = config.base_configuration_reference.real_path
IO.write(xcconfig_path, IO.read(xcconfig_path).gsub("DT_TOOLCHAIN_DIR", "TOOLCHAIN_DIR"))
end
end
end
end
将 Cocoapods 更新到 v1.13:
sudo arch -x86_64 gem update cocoapods
然后在您的 flutter 项目中运行:
flutter clean
flutter pub get
cd ios && arch -x86_64 pod install --repo-update && cd ..
cd macos && arch -x86_64 pod install --repo-update && cd ..
经过寻找更多解决方案,我终于解决了这个问题。
post_install do |installer|
installer.aggregate_targets.each do |target|
target.xcconfigs.each do |variant, xcconfig|
xcconfig_path = target.client_root + target.xcconfig_relative_path(variant)
IO.write(xcconfig_path, IO.read(xcconfig_path).gsub("DT_TOOLCHAIN_DIR", "TOOLCHAIN_DIR"))
end
end
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.0'
if config.base_configuration_reference.is_a? Xcodeproj::Project::Object::PBXFileReference
xcconfig_path = config.base_configuration_reference.real_path
IO.write(xcconfig_path, IO.read(xcconfig_path).gsub("DT_TOOLCHAIN_DIR", "TOOLCHAIN_DIR"))
end
end
end
end
是的,不仅改变,而且这解决了我的问题。
将我的podfile
更改为这个后,我遇到了一个新问题,那就是:
Error (Xcode): type argument 'nw_proxy_config_t' (aka 'struct nw_proxy_config *') is neither an Objective-C object nor a block type
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator17.0.sdk/System/Library/Frameworks/WebKit.framework/Headers/WKWebsi
teDataStore.h:119:46
Parse Issue (Xcode): Could not build module 'WebKit'
/Users/rokomari/rokomari-ios-flutter/rokomari_ios/build/ios/Debug-iphonesimulator/flutter_inappwebview/flutter_inappwebview.framework/Headers/flutter_inappwebview-Swift.h:893:
8
所以我也需要解决这个问题。
要解决这个问题,你必须更改 iOS 目录中的一些文件:
第 1 步:转到此目录/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.0.sdk/System/Library/Frameworks/WebKit.framework/Headers/WKWebsiteDataStore.h
第 2 步:打开名为 WKWebsiteDataStore.h
的文件,并在第 113 行将代码更改为:
更改自:
|| ((TARGET_OS_IOS || TARGET_OS_MACCATALYST) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 170000) \
到:
|| ((TARGET_OS_IOS || TARGET_OS_MACCATALYST) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 180000) \
这就是我解决问题的方法!!
注意:WKWebsiteDataStore.h
文件在XCode
上不可编辑。因此,您必须在默认TextEditor
上打开此文件,然后更改我上面的代码并用新文件替换该文件。我希望这能解决您的问题。
如果您在编辑时遇到困难:
- 打开终端
- 运行:
open /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.0.sdk/System/Library/Frameworks/WebKit.framework/Headers/
如果您使用 XCode beta,请将行 Xcode.app
替换为 Xcode-beta.app
。
- 然后在finder中找到
WKWebsiteDataStore.h
,右键用TextEdit打开。编辑文件并再次保存。