Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How does Unity Cloud build iOS handle modifications to the Xcode Project?

Discussion in 'Unity Build Automation' started by petey, Oct 25, 2021.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Hi there,

    I am a solo developer and I am a little interested in cloud build as it seems it could remove a time consuming step for my pipeline. I just don't quite understand how iOS builds could be automated completely.
    Pretty much every time I've had to make an Xcode build, I've had to modify the Xcode project in some way to workaround an issue or add a feature from an asset or something.

    How could that work with cloud build? Do you have some way of making those modifications yourself through cloud build?

    Thanks!
    Pete
     
    ScottAdams likes this.
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    I've found that you don't normally need to modify the project very often, it's mostly the info.plist I have to add to. I've made some project updates before, mainly things like adding frameworks or modifying settings. You do all this through a 'post process' script that runs after the Unity build has finished but before the xCode compilation has started.

    If the functionality you're looking for is missing you can always do a hacky 'string find/replace' to modify the project file itself if it's an edge case.

    Here's an example of a script I wrote, it modifies the plist file a bit (adds some keys) and then adds a few frameworks to the xCode project and disables bitcode. The file goes into Assets\Editor.

    Even if you don't use cloud build I find this is the best way to do it as you don't have to manually fix the project every time you do a build in xCode. Also means you don't forget important setting changes.

    Code (CSharp):
    1. #if UNITY_IOS
    2. using UnityEngine;
    3. using UnityEditor.Callbacks;
    4. using UnityEditor;
    5. using UnityEditor.iOS.Xcode;
    6. using System.IO;
    7.  
    8. public class ProvideExportCompliance
    9. {
    10.     [PostProcessBuild]
    11.     public static void ChangeXcodePlist(BuildTarget buildTarget, string pathToBuiltProject)
    12.     {
    13.         // Performs any post build processes that we need done
    14.         if( buildTarget == BuildTarget.iOS )
    15.         {
    16.             // PList modifications
    17.             {
    18.                 // Get plist
    19.                 string plistPath = pathToBuiltProject + "/Info.plist";
    20.                 var plist = new PlistDocument();
    21.                 plist.ReadFromString(File.ReadAllText(plistPath));
    22.  
    23.                 // Get root
    24.                 var rootDict = plist.root;
    25.  
    26.                 // Add export compliance for TestFlight builds
    27.                 var buildKeyExportCompliance = "ITSAppUsesNonExemptEncryption";
    28.                 rootDict.SetString( buildKeyExportCompliance , "false" );
    29.              
    30.                 // Add camera usage description (you can do this in Unity now anyway so just an example)
    31.                 var buildKeyCameraUsage = "NSCameraUsageDescription";
    32.                 rootDict.SetString( buildKeyCameraUsage , "Video recording plugin - not used in the app" );
    33.              
    34.                 // Write to file
    35.                 File.WriteAllText( plistPath , plist.WriteToString() );
    36.             }
    37.          
    38.             // xCode workspace modifications
    39.             {
    40.                 // Get xCode Project
    41.                 string projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
    42.                 PBXProject project = new PBXProject();
    43.                 project.ReadFromFile( projectPath );
    44.                 string targetGuid = project.GetUnityMainTargetGuid();
    45.              
    46.                 // Add AdSupport(AdMob) & CloudKit(iCloud) framework
    47.                 project.AddFrameworkToProject( targetGuid , "iAd.framework" , false );
    48.                 project.AddFrameworkToProject( targetGuid , "CloudKit.framework" , false );
    49.                 project.AddFrameworkToProject( targetGuid , "CoreTelephony.framework" , false );
    50.                 project.AddFrameworkToProject( targetGuid , "AdSupport.framework" , false );
    51.                 project.AddFrameworkToProject( targetGuid , "AdServices.framework" , false );
    52.              
    53.                 // Disable bitcode
    54.                 project.SetBuildProperty( targetGuid , "ENABLE_BITCODE" , "NO" );
    55.  
    56.                 // Update xCode project to use the iCloud entitlements file and iCloud/AdSupport frameworks
    57.                 string projectString = project.WriteToString();
    58.              
    59.                 // Save the xCode project file
    60.                 File.WriteAllText( projectPath , projectString );
    61.             }
    62.         }
    63.     }
    64. }
    65. #endif
     
    Ubrano and ScottAdams like this.
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,771
    Awesome @tonemcbride! That doesn’t look too bad, I’ll have a go.
    Thanks so much,
    P.
     
  4. ScottAdams

    ScottAdams

    Joined:
    Nov 23, 2016
    Posts:
    72
    Works great! Thank-you!
     
  5. elaine_unity694

    elaine_unity694

    Joined:
    Oct 12, 2020
    Posts:
    26
    Hi, I am using Cloud Build, is it means that I need to add the Post-build script path or Post-Export method in Cloud Build Advance Setting ? Because I found that our plist was not modify or change after the cloud build.
     
  6. phuong_unity

    phuong_unity

    Unity Technologies

    Joined:
    Apr 25, 2018
    Posts:
    52
    @elaine_unity694 If your project already uses a post-process method, then you don't need to do anything else on the Cloud Build dashboard as that method will also execute when building on Cloud Build. You can also configure on the Cloud Build dashboard to execute a method after the Unity Editor exports your project. You can find more information here.

    It's the same with shell scripts. You can find the settings for both in the Advance settings on Cloud Build.
     
    elaine_unity694 likes this.