升级React Native后Fastlane构建iOS应用遇main.m编译错误
React Native 0.71.12升级后iOS编译main.m失败的解决方案
问题背景
将React Native从0.65.1升级到0.71.12后,使用Xcode 14+Fastlane构建iOS应用时,出现以下编译错误:
CompileC /Users/du/Library/Developer/Xcode/DerivedData/YourApp-foebefkgvngyxaaoudhbitahrqts/ArchiveIntermediates/YourApp/IntermediateBuildFilesPath/YourApp.build/Release-iphoneos/YourApp.build/Objects-normal/arm64/main.o /Users/du/repos/safepush/ios/YourApp/main.m normal arm64 objective-c com.apple.compilers.llvm.clang.1_0.compiler (in target 'YourApp' from project 'YourApp')
解决方案
1. 修复Podfile中重复的use_frameworks!配置
你的Podfile中重复声明了框架链接配置,这会导致静态/动态框架冲突,RN 0.70+版本对框架链接逻辑的校验更严格。
修改Podfile相关代码:
# 替换原有的linkage判断和重复use_frameworks!代码 linkage = ENV['USE_FRAMEWORKS'] || :static Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green use_frameworks! :linkage => linkage.to_sym
删除原Podfile中单独的这一行:
use_frameworks! :linkage => :static
2. 调整Flipper配置(Release构建禁用)
Flipper在Release场景下容易引发编译冲突,建议仅在Debug类环境启用:
# 修改flipper_config定义 flipper_config = if ENV['NO_FLIPPER'] == "1" FlipperConfiguration.disabled else FlipperConfiguration.enabled(["Debug", "StagingDebug"]) # 仅Debug类环境启用 end # 取消use_react_native!中flipper配置的注释 use_react_native!( :path => config[:reactNativePath], :hermes_enabled => true, :flipper_configuration => flipper_config, # 启用这一行 :app_path => "#{Pod::Config.instance.installation_root}/.." )
3. 彻底清理缓存与依赖
执行以下命令清理所有可能的缓存:
# 清理RN缓存 npx react-native clean # 清理Pod依赖与缓存 cd ios && pod deintegrate && pod cache clean --all && pod install # 删除Xcode DerivedData rm -rf ~/Library/Developer/Xcode/DerivedData
4. 验证main.m文件内容
确保ios/YourApp/main.m内容符合RN 0.71的标准格式,无语法错误:
#import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
5. 调整Xcode编译设置
打开Xcode项目,进入Build Settings:
- 搜索
Dead Code Stripping,设置Release配置为YES - 搜索
Optimization Level,Release配置设置为Fastest, Smallest [-Os] - 确认
Architectures为Standard Architectures (arm64, x86_64)
6. 优化Fastlane构建参数
在Fastfile的gym动作中添加清理参数,确保构建环境干净:
gym( scheme: "YourApp", configuration: "Release", clean: true, # 构建前强制清理 export_method: "app-store", # 根据你的分发方式调整(ad-hoc/enterprise等) silent: true )
内容的提问来源于stack exchange,提问作者Dragos Udrea
相关产品推荐
相关产品推荐

