[SOLVED] Cloud Build fails when using PostProcessBuild

When I use cloud build for iOS build with a PostProcessBuild script it won’t recognize some of the UnityEditor features, gives some error messages and fails building. Here are the error messages;

108: [Unity] Assets/Scripts/XCodePostProcess.cs(5,19): error CS0234: The type or namespace name 'Callbacks' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)
109: [Unity] Assets/Scripts/XCodePostProcess.cs(6,19): error CS0234: The type or namespace name 'iOS' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)
110: [Unity] Assets/Scripts/XCodePostProcess.cs(12,43): error CS0246: The type or namespace name 'BuildTarget' could not be found (are you missing a using directive or an assembly reference?)
111: [Unity] Assets/Scripts/XCodePostProcess.cs(11,6): error CS0246: The type or namespace name 'PostProcessBuildAttribute' could not be found (are you missing a using directive or an assembly reference?)
112: [Unity] Assets/Scripts/XCodePostProcess.cs(11,6): error CS0246: The type or namespace name 'PostProcessBuild' could not be found (are you missing a using directive or an assembly reference?)
113: [Unity] ERROR: Assets/Scripts/XCodePostProcess.cs(5,19): error CS0234: The type or namespace name 'Callbacks' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)
114: [Unity] Assets/Scripts/XCodePostProcess.cs(5,19): error CS0234: The type or namespace name 'Callbacks' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)
115: [Unity] ERROR: Assets/Scripts/XCodePostProcess.cs(6,19): error CS0234: The type or namespace name 'iOS' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)
116: [Unity] Assets/Scripts/XCodePostProcess.cs(6,19): error CS0234: The type or namespace name 'iOS' does not exist in the namespace 'UnityEditor' (are you missing an assembly reference?)
117: [Unity] ERROR: Assets/Scripts/XCodePostProcess.cs(12,43): error CS0246: The type or namespace name 'BuildTarget' could not be found (are you missing a using directive or an assembly reference?)
118: [Unity] Assets/Scripts/XCodePostProcess.cs(12,43): error CS0246: The type or namespace name 'BuildTarget' could not be found (are you missing a using directive or an assembly reference?)
119: [Unity] ERROR: Assets/Scripts/XCodePostProcess.cs(11,6): error CS0246: The type or namespace name 'PostProcessBuildAttribute' could not be found (are you missing a using directive or an assembly reference?)
120: [Unity] Assets/Scripts/XCodePostProcess.cs(11,6): error CS0246: The type or namespace name 'PostProcessBuildAttribute' could not be found (are you missing a using directive or an assembly reference?)
121: [Unity] ERROR: Assets/Scripts/XCodePostProcess.cs(11,6): error CS0246: The type or namespace name 'PostProcessBuild' could not be found (are you missing a using directive or an assembly reference?)
122: [Unity] Assets/Scripts/XCodePostProcess.cs(11,6): error CS0246: The type or namespace name 'PostProcessBuild' could not be found (are you missing a using directive or an assembly reference?)

And here is my script;

using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

public class XCodePostProcess
{
    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget target, string path)
    {
        if (target == BuildTarget.iOS)
        {
            string projPath = PBXProject.GetPBXProjectPath(path);
            PBXProject proj = new PBXProject();

            proj.ReadFromString(File.ReadAllText(projPath));
            string targetGuid = proj.TargetGuidByName("Unity-iPhone");

            proj.SetBuildProperty(targetGuid, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "NO");

            File.WriteAllText(projPath, proj.WriteToString());
        }
    }
}

I need PostProcessBuild because of another build error caused by Facebook SDK I believe. When I build the iOS project with fb sdk imported, it caused some swift framework error that App Store won’t accept as it is. I have tried to put this file under the Editor folder but then it didn’t worked at all. Any help with that would be really appreciated.

Those errors you’re getting are because it’s not in the Editor folder (it gets compiled into the wrong assembly). I noticed that you said you moved it to the Editor folder and it didn’t run but I’m not sure why that would be. It’s probably worth adding some Debug.Log commands in and checking your cloud build log to make sure it’s even getting to that function.

p.s. Are you putting the file in Assets\Editor\Build.cs or something similar? Just checking in case you just put it in Editor\Build.cs by accident.

5 Likes

Firstly thanks for your suggestion @tonemcbride . The actual solution for my problem was a mixture of something that I found on stackoverflow and putting the csharp file back in the editor folder. Anyone who is having a similar problem with having their game uploaded to App Store because of the swift framework library error, the final solution is like this;

using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

public class XCodePostProcess
{
    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget target, string path)
    {
        if (target == BuildTarget.iOS)
        {
            string projPath = PBXProject.GetPBXProjectPath(path);
            PBXProject proj = new PBXProject();
            proj.ReadFromFile(projPath);

            string targetGuid = proj.GetUnityMainTargetGuid();

            foreach (var framework in new[] { targetGuid, proj.GetUnityFrameworkTargetGuid() })
            {
                proj.SetBuildProperty(framework, "ENABLE_BITCODE", "NO");
                proj.SetBuildProperty(framework, "EMBEDDED_CONTENT_CONTAINS_SWIFT", "YES");
                proj.SetBuildProperty(framework, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "NO");
                proj.SetBuildProperty(framework, "SWIFT_VERSION", "5.0");
            }

            proj.WriteToFile(projPath);
        }
    }
}
6 Likes

Thank you for this solution! I was running into this error:

1303: ▸ :x:; error: Value for SWIFT_VERSION cannot be empty. (in target ‘UnityFramework’ from project

And your snippet above did the trick. I only needed to set the SWIFT_VERSION line and everything else worked.

Exactly what I was looking for ! Thank you so much. So how do you set up this script in cloud build ? I’m new to this area…

1 Like
#if UNITY_EDITOR && UNITY_IOS
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

public class BuildPostProcessor
{
    [PostProcessBuild]
    public static void OnPostProcessBuild(BuildTarget target, string path)
    {
        if (target == BuildTarget.iOS)
        {
            string projPath = PBXProject.GetPBXProjectPath(path);

            PBXProject proj = new PBXProject();
            proj.ReadFromFile(projPath);

            string targetGUID = proj.GetUnityFrameworkTargetGuid();
            proj.AddBuildProperty(targetGUID, "SWIFT_VERSION", "5.1");

            proj.WriteToFile(projPath);
        }
    }
}
#endif

This is everything needed to solve the issue, put it in a C# script anywhere in the project.

2 Likes

In my project, I had different assemblies for each plugin, and after importing a plugin, its Editor namespaces had not been recognized. So, by creating an assembly definition for its Editor root folder my problem was solved.

I am having the same issue but with android, what should i change in that script to make it for android pls ?

Hey @Oknaa ,

You can attempt to set paths, to your C# class files. However, that should not be necessary, if it is in the Unity Editor.
I recommend attempting to make a local batchmode build in a clean project folder, to see if the error persists.

Since you are attempting to build for Android, you can try and change the builder’s OS, in the configuration settings.

Lastly, please check if this is not a cached library issue, by attempting a clean build.

I came here because I was getting the same error, mine was platform specific switching to iOS helped haha