React Native iOS端构建失败 字体资源CpResource拷贝命令报错
问题描述
我被这个构建错误卡了两天,项目在Android端运行正常,但iOS端完全跑不起来。我已经按React Native官方iOS端文档完成了所有操作,安装了全部所需依赖,我当前的系统是Mac OS Catalina,Xcode版本为12.4。
我的Podfile内容如下:
require_relative '../node_modules/react-native/scripts/react_native_pods' require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' platform :ios, '11.0' target 'frontend' do config = use_native_modules! use_react_native!( :path => config[:reactNativePath], # to enable hermes on iOS, change `false` to `true` and then install pods :hermes_enabled => false ) target 'frontendTests' do inherit! :complete # Pods for testing end # Enables Flipper. # # Note that if you have use_frameworks! enabled, Flipper will not work and # you should disable the next line. use_flipper!() post_install do |installer| react_native_post_install(installer) __apply_Xcode_12_5_M1_post_install_workaround(installer) end end
我收到的报错信息如下:
** BUILD FAILED ** The following build commands failed: CpResource /Users/alibarznji/Desktop/Projects/grade_application/frontend/node_modules/react-native-vector-icons/Fonts/FontAwesome5_Brands.ttf /Users/alibarznji/Library/Developer/Xcode/DerivedData/frontend-dgqzfuyktcahthepzaqwwqkxxvej/Build/Products/Debug-iphonesimulator/frontend.app/FontAwesome5_Brands.ttf CpResource /Users/alibarznji/Desktop/Projects/grade_application/frontend/node_modules/react-native-vector-icons/Fonts/FontAwesome5_Regular.ttf /Users/alibarznji/Library/Developer/Xcode/DerivedData/frontend-dgqzfuyktcahthepzaqwwqkxxvej/Build/Products/Debug-iphonesimulator/frontend.app/FontAwesome5_Regular.ttf CpResource /Users/alibarznji/Desktop/Projects/grade_application/frontend/node_modules/react-native-vector-icons/Fonts/FontAwesome5_Solid.ttf /Users/alibarznji/Library/Developer/Xcode/DerivedData/frontend-dgqzfuyktcahthepzaqwwqkxxvej/Build/Products/Debug-iphonesimulator/frontend.app/FontAwesome5_Solid.ttf CpResource /Users/alibarznji/Desktop/Projects/grade_application/frontend/src/assets/fonts/regular.ttf /Users/alibarznji/Library/Developer/Xcode/DerivedData/frontend-dgqzfuyktcahthepzaqwwqkxxvej/Build/Products/Debug-iphonesimulator/frontend.app/regular.ttf (4 failures)
补充说明:我项目里并没有使用Font-Awesome,另外报错最后一行指向的src/assets/fonts目录下的regular.ttf实际已经不存在,我已经把这个文件重命名为K2D-Regular.ttf,重命名后我也执行了pod install和pod repo update,但问题还是没解决。请问我哪里操作有问题?
解决方案
这个错误是Xcode资源配置残留+缓存未清理导致的,和Pod安装操作无关,按以下步骤操作即可解决:
1. 修复自定义字体配置残留
你重命名字体文件后,Xcode的构建配置里还保留了旧文件的引用,需要手动修改:
- 打开Xcode项目,选中左侧项目根目录,在TARGETS列表中选择你的项目名
frontend - 切换到Build Phases标签,展开Copy Bundle Resources选项
- 从列表中找到
regular.ttf条目,点击左侧减号删除;确认重命名后的K2D-Regular.ttf在列表中,不存在则点击加号手动添加 - 再切换到Info标签,展开Fonts provided by application数组,删除旧的
regular.ttf条目,添加K2D-Regular.ttf条目
2. 移除不需要的字体包引用
react-native-vector-icons默认会将全量字体文件加入iOS构建配置,你不需要的FontAwesome5系列字体可以直接移除:
- 还是在Copy Bundle Resources列表中,找到三个
FontAwesome5_开头的ttf文件条目,全部删除即可 - 如果你完全没有用到react-native-vector-icons库,直接执行
npm uninstall react-native-vector-icons或yarn remove react-native-vector-icons卸载该库即可
3. 清理缓存重新构建
修改完配置后必须清理所有旧缓存和构建产物,避免残留引用报错:
执行以下命令清理缓存:
rm -rf ~/Library/Developer/Xcode/DerivedData rm -rf ios/build cd ios && pod deintegrate && pod install
命令执行完成后重新打开Xcode执行构建即可。
内容的提问来源于stack exchange,提问作者Ali Barznji
相关产品推荐
相关产品推荐

