You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter iOS带watchOS目标归档时提示No such module问题求助

Flutter iOS项目添加watchOS目标后归档构建Pod模块找不到的解决方案

问题描述

我是Flutter新手,给现有Flutter iOS项目添加了watchOS应用目标,并为该watch应用引入了Pods。Debug模式下(设备和模拟器)一切正常,但尝试归档构建时,先是出现Kingfisher模块找不到的错误;移除该模块后,又出现watch应用下其他Pods的错误。移除watchOS目标后,归档成功。

已尝试过以下无效方法:

  • 手动移除Pods并添加框架
  • 修改watch应用目标支持平台为watchOS(导致应用与watch应用通信中断且无法安装watch应用)
  • 检查Pods的podspec兼容性
  • 移除watch应用目标名称空格
  • 在框架搜索路径添加$(SRCROOT)并设为递归
  • Release模式排除arm64

我的Podfile代码

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!
  use_modular_headers!
  platform :ios, '14.0'
 # pod 'Alamofire', '5.6.4'
  pod 'Google-Mobile-Ads-SDK'
  pod 'GoogleMobileAdsMediationUnity'
  pod 'GoogleMobileAdsMediationFyber'
  pod 'GoogleMobileAdsMediationInMobi'
  pod 'GoogleMobileAdsMediationChartboost'
  pod 'GoogleMobileAdsMediationFacebook'
  pod 'GoogleMobileAdsMediationAppLovin'
  pod 'GoogleMobileAdsMediationIronSource'
  pod 'GoogleMobileAdsMediationVungle'
  pod 'GoogleMobileAdsMediationAdColony'
  pod 'GoogleMobileAdsMediationTestSuite'
  pod 'FBAudienceNetwork'
 # pod 'CryptoSwift', '0.13.1'

  
  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  target 'RunnerTests' do
    inherit! :search_paths
  end
end

target 'MyWatchApp' do
    use_frameworks!
    use_modular_headers!
    platform :watchos, '9.0'
    pod 'Alamofire', '5.6.4'
    pod 'ObjectMapper', '4.2'
    pod 'CryptoSwift', '0.13.1'
    pod 'Kingfisher'
end


post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    # Set iPhoneOS deployment target
        target.build_configurations.each do |config|
          config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0'
        end
  end
end

解决方案

1. 修复Podfile中跨平台部署版本冲突

你的post_install脚本给所有target强制设置了IPHONEOS_DEPLOYMENT_TARGET,这会覆盖watchOS目标的部署版本配置,导致编译时出现架构或依赖不匹配。修改脚本,针对iOS和watchOS目标分别设置部署版本:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
    
    # 区分iOS和watchOS目标设置对应的部署版本
    if target.platform_name == :ios
      target.build_configurations.each do |config|
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0'
      end
    elsif target.platform_name == :watchos
      target.build_configurations.each do |config|
        config.build_settings['WATCHOS_DEPLOYMENT_TARGET'] = '9.0'
      end
    end
  end
end

2. 彻底重置Pods集成

执行以下步骤清理并重新安装Pods,避免缓存或配置残留:

  • 进入项目的ios目录:cd ios
  • 删除Pod锁文件和Pods文件夹:rm Podfile.lock && rm -rf Pods
  • 清理Xcode派生数据:rm -rf ~/Library/Developer/Xcode/DerivedData
  • 执行Pods清理命令:pod deintegrate
  • 重新安装Pods:pod install
  • 重启Xcode,清理项目(快捷键Cmd+Shift+K)后再尝试归档

3. 检查watchOS目标的Build Settings关键配置

打开Xcode中watchOS目标的Build Settings,确认以下配置:

  • Framework Search Paths:必须包含$(inherited),且放在自定义路径之前
  • Other Linker Flags:添加$(inherited),确保Pods的链接配置被正确继承
  • Build Active Architecture Only:Release模式下设置为NO,避免只构建当前设备架构导致的归档失败
  • ENABLE_BITCODE:设置为NO,Flutter项目通常不启用Bitcode,与watchOS兼容更好

4. 明确指定Pods的兼容版本

确保引入的Pods版本明确支持watchOS 9.0,修改watchOS目标的Pod配置:

target 'MyWatchApp' do
    use_frameworks!
    use_modular_headers!
    platform :watchos, '9.0'
    pod 'Alamofire', '5.6.4' # 确认支持watchOS 2.0+
    pod 'ObjectMapper', '4.2' # 支持watchOS
    pod 'CryptoSwift', '0.13.1' # 支持watchOS
    pod 'Kingfisher', '~> 7.6.0' # 指定明确支持watchOS的版本
end

5. 验证watchOS目标的编译模式

在Xcode中选择watchOS目标,进入Build Settings -> Build Options:

  • 设置DEAD_CODE_STRIPPING为YES(Release模式下自动清理无用代码)
  • 设置STRIP_DEBUG_SYMBOLS_DURING_COPY为YES(Release模式下剥离调试符号,减少包体积并避免冲突)

内容的提问来源于stack exchange,提问作者jitender-skycap

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 08:32:16