You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity iOS构建后自动执行Pod命令报错:Run命令未找到,如何解决?

问题修正方案

错误原因

  1. 无效的Run参数:你在pod命令的Arguments里添加了Run,但Pod没有这个子命令,这是报错的直接原因。
  2. 命令执行对象错误:rm -rf ~/Library/Developer/Xcode/DerivedData是Shell命令,不能通过Pod进程执行,需要用bash这类Shell来运行。
  3. 冗余命令:pod install和pod update无需连续执行,构建后场景下pod install通常足够满足需求。

修正后的代码

#if UNITY_EDITOR && UNITY_IOS
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Diagnostics;
using System.IO;

public class XcodePostProcess
{
    [PostProcessBuild]
    static void OnPostProcessGenPodfile(BuildTarget buildTarget, string pathToBuiltProject)
    {
        if (buildTarget != BuildTarget.iOS)
            return;

        // 更新Xcode项目配置
        var projPath = Path.Combine(pathToBuiltProject, "Unity-iPhone.xcodeproj/project.pbxproj");
        var project = new UnityEditor.iOS.Xcode.PBXProject();
        project.ReadFromFile(projPath);

        // 复制自定义Podfile到构建目录
        var target = project.GetUnityFrameworkTargetGuid();
        File.Copy("Assets/Editor/MyPodfile", Path.Combine(pathToBuiltProject, "Podfile"), true);

        project.AddBuildProperty(target, "CLANG_ENABLE_MODULES", "YES");
        File.WriteAllText(projPath, project.WriteToString());

        // 定义需要执行的命令列表
        List<ProcessStartInfo> commandList = new List<ProcessStartInfo>()
        {
            // 清理Xcode派生数据(Shell命令)
            new ProcessStartInfo
            {
                WorkingDirectory = pathToBuiltProject,
                FileName = "/bin/bash",
                Arguments = "-c \"rm -rf ~/Library/Developer/Xcode/DerivedData\"",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            },
            // Pod反集成
            new ProcessStartInfo
            {
                WorkingDirectory = pathToBuiltProject,
                FileName = "/usr/local/bin/pod",
                Arguments = "deintegrate",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            },
            // 清理Pod缓存
            new ProcessStartInfo
            {
                WorkingDirectory = pathToBuiltProject,
                FileName = "/usr/local/bin/pod",
                Arguments = "cache clean --all",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            },
            // 更新Pod仓库
            new ProcessStartInfo
            {
                WorkingDirectory = pathToBuiltProject,
                FileName = "/usr/local/bin/pod",
                Arguments = "repo update",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            },
            // 安装Pod依赖
            new ProcessStartInfo
            {
                WorkingDirectory = pathToBuiltProject,
                FileName = "/usr/local/bin/pod",
                Arguments = "install",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true
            }
        };

        try
        {
            foreach (var startInfo in commandList)
            {
                using (var process = new Process())
                {
                    process.StartInfo = startInfo;
                    process.Start();

                    // 实时读取输出和错误信息
                    while (!process.StandardOutput.EndOfStream)
                    {
                        UnityEngine.Debug.Log(process.StandardOutput.ReadLine());
                    }
                    while (!process.StandardError.EndOfStream)
                    {
                        UnityEngine.Debug.LogError(process.StandardError.ReadLine());
                    }

                    // 等待进程执行完成
                    process.WaitForExit();
                    UnityEngine.Debug.Log($"命令 [{startInfo.FileName} {startInfo.Arguments}] 执行完成,退出码: {process.ExitCode}");
                }
            }
        }
        catch (System.Exception ex)
        {
            UnityEngine.Debug.LogError($"执行命令出错: {ex.Message}\n{ex.StackTrace}");
        }
    }
}
#endif

额外优化点

  • 新增RedirectStandardError捕获命令执行的错误信息,便于排查问题。
  • 使用using语句管理Process对象,确保资源正确释放。
  • 调用WaitForExit()等待每个命令执行完成后再进行下一个,避免并发问题。

内容的提问来源于stack exchange,提问作者Kasım Furkan Ünal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:43:20