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

Apple Silicon M1 MacBook上iOS项目单元测试失败求助

iOS单元测试架构兼容性问题解决方案

针对你遇到的两个单元测试执行错误,结合M1 Mac、Xcode 14.3.1的环境,给出以下具体解决步骤:

一、解决原生模拟器架构不兼容错误 (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))

  1. 统一主项目与测试目标的架构配置

    • 选中主项目Target,进入Build Settings → Architectures:
      • 将Architectures设置为$(ARCHS_STANDARD)(即标准架构arm64+x86_64)
      • 修改Excluded Architectures为条件式排除:在Debug配置下,添加EXCLUDED_ARCHS[sdk=iphonesimulator*] = arm64,避免全局排除arm64导致真机构建问题
    • 选中测试Target,重复上述架构检查,确保和主项目配置完全一致
  2. 强制Pod适配双架构
    在Podfile的post_install钩子中添加统一的架构配置,避免第三方Pod只编译单一架构:

    post_install do |installer|
      installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
          config.build_settings['ARCHS'] = '$(ARCHS_STANDARD)'
          config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
        end
      end
    end
    

二、解决Rosetta模拟器框架加载错误 (Library not loaded: @rpath/AppAuth.framework/AppAuth)

  1. 确保Podfile中测试目标正确继承依赖
    修改Podfile,让测试目标通过inherit! :search_paths继承主项目的依赖链,同时明确声明测试所需的框架:

    platform :ios, '你的最低iOS版本'
    use_frameworks! :linkage => :dynamic # 强制使用动态框架,解决Rosetta下的加载问题
    
    target '你的主项目名称' do
      pod 'AppAuth'
      # 其他主项目依赖...
    
      target '你的测试目标名称' do
        inherit! :search_paths
        pod 'AppAuth' # 明确添加测试所需的框架
        # 其他测试依赖...
      end
    end
    
  2. 修复测试目标的运行路径配置

    • 选中测试Target,进入Build Settings → Linking → Runpath Search Paths
    • 添加以下路径:@executable_path/Frameworks、@loader_path/Frameworks
  3. 验证框架嵌入配置
    进入测试Target的Build Phases,确认Embed Frameworks中包含AppAuth.framework(动态框架需嵌入,静态框架无需此步骤)

三、收尾操作

  1. 执行Pod清理与重装:pod deintegrate → pod install
  2. 清理Xcode缓存:Cmd+Shift+K清理构建缓存,Cmd+Option+Shift+K删除Derived Data
  3. 重启Xcode与模拟器,确保所有配置生效

如果仍有问题,检查Xcode的Rosetta运行设置:右键Xcode图标→Get Info,取消Open using Rosetta,优先使用原生arm64运行Xcode,避免不必要的架构转换。

内容的提问来源于stack exchange,提问作者DJ-Glock

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 19:10:22