flutter踩坑插件:Swift架构不兼容

最近接手一个老项目,净是遇到插件swift架构不兼容,报错如下:

复制代码
User-Defined Issue (Xcode): Unsupported Swift architecture
/Users/yiner/code/app/app/build/ios/Debug-iphonesimulator/url_launcher_ios/url_launcher_ios.framework/Headers/url_launcher_ios-Swift.h:325:1

User-Defined Issue (Xcode): Unsupported Swift architecture
/Users/yiner/code/app/app/build/ios/Debug-iphonesimulator/webview_flutter_wkwebview/webview_flutter_wkwebview.framework/Headers/webview_flutter_wkwebview-Swift.h:364:1

User-Defined Issue (Xcode): Unsupported Swift architecture
/Users/yiner/code/app/app/build/ios/Debug-iphonesimulator/shared_preferences_foundation/shared_preferences_foundation.framework/Headers/shared_preferences_foundation-Swift.h:330:1

User-Defined Issue (Xcode): Unsupported Swift architecture
/Users/yiner/code/app/app/build/ios/Debug-iphonesimulator/mobile_scanner/mobile_scanner.framework/Headers/mobile_scanner-Swift.h:340:1

User-Defined Issue (Xcode): Unsupported Swift architecture
/Users/yiner/code/app/app/build/ios/Debug-iphonesimulator/connectivity_plus/connectivity_plus.framework/Headers/connectivity_plus-Swift.h:337:1

User-Defined Issue (Xcode): Unsupported Swift architecture
/Users/yiner/code/app/app/build/ios/Debug-iphonesimulator/flutter_new_badger/flutter_new_badger.framework/Headers/flutter_new_badger-Swift.h:326:1

User-Defined Issue (Xcode): Unsupported Swift architecture
/Users/yiner/code/app/app/build/ios/Debug-iphonesimulator/path_provider_foundation/path_provider_foundation.framework/Headers/path_provider_foundation-Swift.h:324:1

Could not build the application for the simulator.
Error launching application on iPhone 16 Pro.

网上查一堆资料,也问了很多次AI都解决不了,也不知道啥问题,怀疑是mac 的m1、m2芯片的虚拟机和m4芯片的不一样,博主是M4。

废话不多说,下面是解决方案:

在ios下的Podfile文件中添加如下代码:

复制代码
# 添加以下配置来解决Swift架构问题
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.6'
      config.build_settings['SWIFT_VERSION'] = '5.0'
      config.build_settings['ENABLE_BITCODE'] = 'NO'
      
      # 关键:排除模拟器的arm64架构
      config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
      
      # 确保支持正确的架构
      config.build_settings['VALID_ARCHS'] = 'arm64 x86_64'
      config.build_settings['ONLY_ACTIVE_ARCH'] = 'NO'

然后完全清理并重建:

复制代码
# 停止所有Flutter进程
flutter devices
# 如果有设备在运行,先停止

# 清理Flutter缓存
flutter clean

# 删除iOS构建缓存
cd ios
rm -rf Pods Podfile.lock
rm -rf ~/Library/Developer/Xcode/DerivedData
cd ..

# 重新获取依赖
flutter pub get

# 重新安装Pods
cd ios
pod install --repo-update
cd ..

最后再运行,就OK了

我的完整Podfile:

复制代码
# Uncomment this line to define a global platform for your project
platform :ios, '15.6'

# 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!
 

  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)
    
    # 添加以下配置来解决Swift架构问题
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.6'
      config.build_settings['SWIFT_VERSION'] = '5.0'
      config.build_settings['ENABLE_BITCODE'] = 'NO'
      
      # 关键:排除模拟器的arm64架构
      config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
      
      # 确保支持正确的架构
      config.build_settings['VALID_ARCHS'] = 'arm64 x86_64'
      config.build_settings['ONLY_ACTIVE_ARCH'] = 'NO'
    end
  end
end
相关推荐
Highcharts.js1 小时前
缺失数据可视化图表开发实战|Highcharts创建人员出生统计面积图表示例
开发语言·前端·javascript·信息可视化·highcharts·图表开发
测试员周周5 小时前
【Appium 系列】第16节-WebView-H5上下文切换 — 混合应用的自动化难点
运维·开发语言·人工智能·功能测试·appium·自动化·测试用例
杜子不疼.8 小时前
【C++ AI 大模型接入 SDK】 - DeepSeek 模型接入(上)
开发语言·c++·chatgpt
加号38 小时前
【C#】 串口通信技术深度解析及实现
开发语言·c#
sycmancia9 小时前
Qt——编辑交互功能的实现
开发语言·qt
石山代码9 小时前
C++ 内存分区 堆区
java·开发语言·c++
无风听海9 小时前
C# 隐式转换深度解析
java·开发语言·c#
SoaringHeart10 小时前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter
一只大袋鼠10 小时前
Git 进阶(二):分支管理、暂存栈、远程仓库与多人协作
java·开发语言·git
LuminousCPP11 小时前
数据结构 - 线性表第四篇:C 语言通讯录优化升级全记录(踩坑 + 思考)
c语言·开发语言·数据结构·经验分享·笔记·学习