Search Unity

[SOLVED] Cloud Build fails when using PostProcessBuild

Discussion in 'Unity Build Automation' started by bferah, Aug 22, 2020.

  1. bferah

    bferah

    Joined:
    Jul 8, 2017
    Posts:
    13
    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;

    Code (CSharp):
    1.  
    2. 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?)
    3. 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?)
    4. 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?)
    5. 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?)
    6. 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?)
    7. 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?)
    8. 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?)
    9. 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?)
    10. 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?)
    11. 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?)
    12. 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?)
    13. 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?)
    14. 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?)
    15. 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?)
    16. 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?)
    17.  
    And here is my script;
    Code (CSharp):
    1.  
    2. using System.IO;
    3. using UnityEditor;
    4. using UnityEditor.Callbacks;
    5. using UnityEditor.iOS.Xcode;
    6.  
    7. public class XCodePostProcess
    8. {
    9.     [PostProcessBuild]
    10.     public static void OnPostprocessBuild(BuildTarget target, string path)
    11.     {
    12.         if (target == BuildTarget.iOS)
    13.         {
    14.             string projPath = PBXProject.GetPBXProjectPath(path);
    15.             PBXProject proj = new PBXProject();
    16.  
    17.             proj.ReadFromString(File.ReadAllText(projPath));
    18.             string targetGuid = proj.TargetGuidByName("Unity-iPhone");
    19.  
    20.             proj.SetBuildProperty(targetGuid, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "NO");
    21.  
    22.             File.WriteAllText(projPath, proj.WriteToString());
    23.         }
    24.     }
    25. }
    26.  
    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.
     
    Last edited: Aug 22, 2020
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    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.
     
  3. bferah

    bferah

    Joined:
    Jul 8, 2017
    Posts:
    13
    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;
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.Callbacks;
    3. using UnityEditor.iOS.Xcode;
    4.  
    5. public class XCodePostProcess
    6. {
    7.     [PostProcessBuild]
    8.     public static void OnPostprocessBuild(BuildTarget target, string path)
    9.     {
    10.         if (target == BuildTarget.iOS)
    11.         {
    12.             string projPath = PBXProject.GetPBXProjectPath(path);
    13.             PBXProject proj = new PBXProject();
    14.             proj.ReadFromFile(projPath);
    15.  
    16.             string targetGuid = proj.GetUnityMainTargetGuid();
    17.  
    18.             foreach (var framework in new[] { targetGuid, proj.GetUnityFrameworkTargetGuid() })
    19.             {
    20.                 proj.SetBuildProperty(framework, "ENABLE_BITCODE", "NO");
    21.                 proj.SetBuildProperty(framework, "EMBEDDED_CONTENT_CONTAINS_SWIFT", "YES");
    22.                 proj.SetBuildProperty(framework, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "NO");
    23.                 proj.SetBuildProperty(framework, "SWIFT_VERSION", "5.0");
    24.             }
    25.  
    26.             proj.WriteToFile(projPath);
    27.         }
    28.     }
    29. }
     
    yuliyF, ilmario, TractGames and 3 others like this.
  4. PorpoiseStudios

    PorpoiseStudios

    Joined:
    Oct 12, 2019
    Posts:
    6
    Thank you for this solution! I was running into this error:

    1303: ▸ ❌; 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.
     
  5. Qhuhuit

    Qhuhuit

    Joined:
    Feb 17, 2018
    Posts:
    39
    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..
     
    Keyveo_Dev_04 likes this.
  6. gaps

    gaps

    Joined:
    Jan 1, 2014
    Posts:
    15
    Code (CSharp):
    1. #if UNITY_EDITOR && UNITY_IOS
    2. using UnityEditor;
    3. using UnityEditor.Callbacks;
    4. using UnityEditor.iOS.Xcode;
    5.  
    6. public class BuildPostProcessor
    7. {
    8.     [PostProcessBuild]
    9.     public static void OnPostProcessBuild(BuildTarget target, string path)
    10.     {
    11.         if (target == BuildTarget.iOS)
    12.         {
    13.             string projPath = PBXProject.GetPBXProjectPath(path);
    14.  
    15.             PBXProject proj = new PBXProject();
    16.             proj.ReadFromFile(projPath);
    17.  
    18.             string targetGUID = proj.GetUnityFrameworkTargetGuid();
    19.             proj.AddBuildProperty(targetGUID, "SWIFT_VERSION", "5.1");
    20.  
    21.             proj.WriteToFile(projPath);
    22.         }
    23.     }
    24. }
    25. #endif
    26.  
    This is everything needed to solve the issue, put it in a C# script anywhere in the project.
     
    Deleted User and Qhuhuit like this.
  7. masoudarvishian

    masoudarvishian

    Joined:
    Jan 15, 2015
    Posts:
    9
    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.
     
  8. Oknaa

    Oknaa

    Joined:
    Jul 22, 2019
    Posts:
    8
    I am having the same issue but with android, what should i change in that script to make it for android pls ?
     
  9. Carl_UnityDSE

    Carl_UnityDSE

    Unity Technologies

    Joined:
    May 25, 2022
    Posts:
    59
    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.
     
  10. BlindsidedGames

    BlindsidedGames

    Joined:
    Aug 24, 2020
    Posts:
    9
    I came here because I was getting the same error, mine was platform specific switching to iOS helped haha