React Native iOS项目XCode报错:多命令生成同一文件
解决React Native iOS项目中XCode多命令生成同一头文件的报错
针对你遇到的Multiple commands produce '/Users/admin/Library/Developer/Xcode/DerivedData/myapps-cebtqkqvjkhixpaaqvrgdmyzymyv/Build/Products/Debug-iphonesimulator/react-native-test/react_native_test.framework/Headers/test.h'报错,且无法升级依赖、不能切换旧构建系统的情况,可以尝试以下几个针对性方案:
方案1:通过Podpost_install脚本自动清理重复的复制命令
在项目根目录的Podfile末尾添加以下脚本,它会在每次pod install后自动移除react-native-test目标中重复的头文件复制命令,避免手动修改被覆盖:
post_install do |installer| installer.pods_project.targets.each do |target| next unless target.name == 'react-native-test' # 筛选出所有"Copy Headers"构建阶段 copy_headers_phases = target.build_phases.select { |phase| phase.display_name == 'Copy Headers' } next if copy_headers_phases.size <= 1 first_phase = copy_headers_phases[0] second_phase = copy_headers_phases[1] # 合并两个阶段的文件列表并去重 all_file_paths = first_phase.files.map(&:path) + second_phase.files.map(&:path) unique_file_paths = all_file_paths.uniq # 重置第一个阶段的文件列表 first_phase.files.clear unique_file_paths.each do |path| file_ref = installer.pods_project.new_file(path) first_phase.add_file_reference(file_ref) end # 删除重复的第二个构建阶段 second_phase.remove_from_project end end
添加完成后执行pod install,脚本会自动处理重复的Copy Headers阶段。
方案2:调整react-native-test的头文件暴露配置
若项目直接依赖react-native-test的源码,可检查它的react-native-test.podspec文件,修正header_files配置项,避免重复匹配路径:
比如原配置如果是:
s.header_files = "ios/**/*.h", "ios/test.h"
可修改为更精确的匹配规则:
s.header_files = "ios/**/*.h"
修改后执行pod install更新依赖配置。
方案3:手动修改Build Phases(临时应急)
如果上述脚本无法生效,可手动修改Pods项目的Build Phases(注意:执行pod install后配置会被重置,需重复操作):
- 打开XCode,定位到Pods项目下的
react-native-test目标 - 切换到
Build Phases标签页,找到两个Copy Headers阶段 - 对比两个阶段的文件列表,将重复文件从其中一个阶段移除,保留所有唯一文件
- 若两个阶段文件完全重复,可直接删除其中一个
Copy Headers阶段
内容的提问来源于stack exchange,提问作者Matrix
相关产品推荐
相关产品推荐

