GitHub Actions上Expo iOS构建失败:CocoaPods错误
Expo React Native iOS GitHub Actions构建失败问题排查与解决
问题背景
使用Expo开发React Native应用,Android端APK构建成功,但iOS端在GitHub Actions中构建失败,应用基于Expo模板开发,需要使用工作流生成的iOS产物。
相关信息
GitHub Actions工作流片段
- name: Install CocoaPods dependencies run: | cd ios pod setup pod cache clean --all pod install
完整GitHub Actions工作流
jobs: prepare-js-bundle-ios: name: Prepare JavaScript Bundle with Metro for iOS runs-on: macos-latest steps: - uses: actions/checkout@v3 - name: Cache node modules uses: actions/cache@v3 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node- - name: Cache Watchman uses: actions/cache@v3 with: path: | /usr/local/Cellar/watchman key: ${{ runner.os }}-watchman-${{ hashFiles('**/Brewfile.lock.json') }} restore-keys: | ${{ runner.os }}-watchman- - name: Setup Node.js environment uses: actions/setup-node@v3 with: node-version: '21' - name: Install Watchman run: | if ! watchman --version; then brew install watchman else echo "Watchman is already installed." fi - name: Setup CocoaPods run: | sudo gem install cocoapods sudo gem update cocoapods - name: Install Xcode Command Line Tools run: xcode-select --install || true # The `|| true` ensures that if command line tools are already installed, the workflow doesn't fail. - name: Install npm dependencies run: npm i - name: Bundle JavaScript with Metro for iOS run: | npx expo prebuild -p ios npx react-native bundle --platform ios --dev false --entry-file index.js --bundle-output ios/main.jsbundle --assets-dest ios - name: Update Podfile platform version run: | sed -i '' "s/^platform :ios,.*/platform :ios, '13.4'/" ios/Podfile - name: Modify Podfile run: | echo "use_modular_headers!" >> ios/Podfile - name: Install CocoaPods dependencies run: | cd ios pod setup pod cache clean --all pod install - name: Build iOS app (Xcode build) run: | cd ios xcodebuild -workspace wordguesser-app.xcworkspace -scheme wordguesser-app -configuration Release -sdk iphoneos BUILD_LIBRARY_FOR_DISTRIBUTION=YES
构建错误信息
Setting REACT_NATIVE build settings [!] `<PBXResourcesBuildPhase UUID=`13B07F8E1A680F5B00A75B9A`>` attempted to initialize an object with an unknown UUID. `83FE304946FB4310A55A14A4` for attribute: `files`. This can be the result of a merge and the unknown UUID is being discarded. Setting CLANG_CXX_LANGUAGE_STANDARD to c++20 on /Users/runner/work/wordguesser-app/wordguesser-app/ios/myapp.xcodeproj Pod install took 150 [s] to run [!] An error occurred while processing the post-install hook of the Podfile. undefined method `__apply_Xcode_12_5_M1_post_install_workaround' for #<Pod::Podfile:0x00007fbab12f82a8 @defined_in_file=#<Pathname:/Users/runner/work/wordguesser-app/wordguesser-app/ios/Podfile>, @internal_hash={"installation_method"=>{"name"=>"cocoapods", "options"=>{:deterministic_uuids=>false}}}, @root_target_definitions=[#<Pod::Podfile::TargetDefinition label=Pods>], @current_target_definition=#<Pod::Podfile::TargetDefinition label=Pods>, @post_install_callback=#<Proc:0x00007fbab1912f80 /Users/runner/work/wordguesser-app/wordguesser-app/ios/Podfile:60>, @post_integrate_callback=#<Proc:0x00007fbab1912f08 /Users/runner/work/wordguesser-app/wordguesser-app/ios/Podfile:80>, @installation_options=#<Pod::Installer::InstallationOptions:0x00007fbaae4ee970 @clean=true, @deduplicate_targets=true, @deterministic_uuids=false, @integrate_targets=true, @lock_pod_sources=true, @warn_for_multiple_pod_sources=true, @warn_for_unused_master_specs_repo=true, @share_schemes_for_development_pods=false, @disable_input_output_paths=false, @preserve_pod_file_structure=false, @generate_multiple_pod_projects=false, @incremental_installation=false, @skip_pods_project_generation=false, @parallel_pod_downloads=false, @parallel_pod_download_thread_pool_size=40>>
app.json配置
{ "expo": { "name": "my-app", "slug": "my-app", "version": "1.0.0", "orientation": "portrait", "icon": "./assets/icon.png", "userInterfaceStyle": "light", "splash": { "image": "./assets/splash.png", "resizeMode": "contain", "backgroundColor": "#ffffff" }, "assetBundlePatterns": [ "**/*" ], "ios": { "bundleIdentifier": "com.test.test", "supportsTablet": true }, "android": { "adaptiveIcon": { "foregroundImage": "./assets/adaptive-icon.png", "backgroundColor": "#ffffff" }, "package": "com.sebastian.arthur.kull.test" }, "web": { "favicon": "./assets/favicon.png" } } }
问题分析
核心错误是undefined method '__apply_Xcode_12_5_M1_post_install_workaround',说明Podfile的post_install钩子中调用了一个不存在的方法:
- 这个方法是旧版React Native/Expo针对Xcode 12.5和M1芯片的临时兼容修复,新版CocoaPods或Expo已经移除了该方法,导致调用失败。
- 工作流中手动修改Podfile(添加
use_modular_headers!)可能与Expo prebuild生成的默认配置冲突,加剧了依赖问题。 - 未指定CocoaPods版本,自动安装的最新版可能与Expo生成的Podfile不兼容。
- UUID警告属于次要问题,是Xcode项目文件的小冲突,不影响核心构建,可忽略。
解决方案
方案1:移除Podfile中的过时方法
在Expo prebuild步骤后,添加一个步骤删除Podfile中对该过时方法的调用:
- name: Fix outdated post_install method in Podfile run: | sed -i '' '/__apply_Xcode_12_5_M1_post_install_workaround/d' ios/Podfile
将此步骤放在Modify Podfile和Install CocoaPods dependencies之间。
方案2:使用Expo官方构建工具替代手动构建
既然是Expo项目,建议直接使用EAS Build处理所有依赖和配置,避免手动操作的兼容性问题:
- 在GitHub Secrets中添加
EXPO_TOKEN(从Expo Dashboard获取)。 - 修改工作流中的构建步骤:
- name: Install EAS CLI run: npm install -g eas-cli - name: Build iOS app with EAS run: eas build --platform ios --profile production --non-interactive env: EXPO_TOKEN: ${{ secrets.EXPO_TOKEN }}
此方案无需手动prebuild、pod install和xcodebuild,Expo会自动处理所有配置。
方案3:指定兼容的CocoaPods版本
如果坚持手动构建,指定与Expo版本兼容的CocoaPods版本(比如1.12.1),避免新版移除方法的问题:
- name: Setup CocoaPods run: | sudo gem install cocoapods@1.12.1
方案4:通过Expo配置启用模块化头
工作流中手动添加use_modular_headers!可能引发冲突,建议通过app.json配置让Expo自动生成该设置:
修改app.json的ios字段:
"ios": { "bundleIdentifier": "com.test.test", "supportsTablet": true, "useModularHeaders": true }
然后移除工作流中的Modify Podfile步骤。
额外优化建议
- 移除
pod setup步骤:GitHub Actions的macos-latest环境已预装CocoaPods,无需重复初始化。 - 简化Xcode Command Line Tools步骤:macos-latest已预装该工具,可直接删除
xcode-select --install || true步骤。
内容的提问来源于stack exchange,提问作者sebastian kull
相关产品推荐
相关产品推荐

