M1芯片Mac运行iOS单元测试出现XCTest bundle无效错误
M1芯片Mac运行iOS单元测试bundle加载错误解决方案
错误表现
在M1芯片Mac设备上运行iOS单元测试时,测试套件会直接崩溃,抛出三类bundle相关错误:
- Failed to load test bundle
- No suitable image found
- Trying to load an unsigned library
完整错误日志如下:
2021-11-04 08:53:33.378375-0600 xxx[81944:1274939] Failed to load test bundle from file:///Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit%20Tests.xctest/: Error Domain=NSCocoaErrorDomain Code=3587 "dlopen_preflight(/Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests): no suitable image found. Did find: /Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests: code signature in (/Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests) not valid for use in process using Library Validation: Trying to load an unsigned library" UserInfo={NSLocalizedFailureReason=The bundle is damaged or missing necessary resources., NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSFilePath=/Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests, NSDebugDescription=dlopen_preflight(/Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests): no suitable image found. Did find: /Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests: code signature in (/Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests) not valid for use in process using Library Validation: Trying to load an unsigned library, NSBundlePath=/Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest, NSLocalizedDescription=The bundle “Unit Tests.xctest” couldn’t be loaded because it is damaged or missing necessary resources.} 2021-11-04 08:53:33.683690-0600 xxx[81944:1274939] The bundle “Unit Tests.xctest” couldn’t be loaded because it is damaged or missing necessary resources. Try reinstalling the bundle. 2021-11-04 08:53:33.683757-0600 xxx[81944:1274939] (dlopen_preflight(/Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests): no suitable image found. Did find: /Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests: code signature in (/Users/xxx/Library/Developer/XCTestDevices/FDCD6684-3D06-48C9-81E3-DE82C6AB2CCB/data/Containers/Bundle/Application/7143E7F2-E59A-4578-971A-A2A20D42DDB6/xxx.app/PlugIns/Unit Tests.xctest/Unit Tests) not valid for use in process using Library Validation: Trying to load an unsigned library)
上述相同测试逻辑在Intel芯片Mac设备上可正常运行。
问题原因
该错误是M1芯片arm64架构下模拟器独有的签名校验和架构适配问题:
- Intel芯片Mac的iOS模拟器默认运行x86_64架构,该架构下的Library Validation校验规则更宽松,不会拦截测试场景下的未签名bundle
- M1芯片默认以arm64原生架构运行模拟器,对加载的测试bundle、动态库有更严格的签名校验要求,不符合要求的组件会被直接拦截加载
解决方案
方案1:排除模拟器arm64架构(快速生效,无需修改签名配置)
操作步骤:
- 打开Xcode项目,选中单元测试Target
- 进入
Build Settings页面,找到Excluded Architectures配置项 - 在Debug配置下,为
Any iOS Simulator SDK添加值arm64,强制模拟器在M1设备上也通过Rosetta转译运行x86_64架构,完全对齐Intel设备的运行逻辑 - 按
Command+Shift+K清理Xcode构建缓存,重新运行测试即可
方案2:调整签名配置(保留arm64原生运行,性能更好)
如果需要使用M1原生架构运行模拟器获得更高性能,可以调整签名相关配置:
- 同时选中主项目Target和单元测试Target,分别进入
Build Settings页面 - 找到
Other Code Signing Flags配置项,在Debug配置下添加参数--generate-entitlement-der - 找到
Enable Hardened Runtime配置项,Debug模式下设置为NO - 清理构建缓存后重新运行测试
内容的提问来源于stack exchange,提问作者Colin Humber
相关产品推荐
相关产品推荐

