简体   繁体   中英

Unity iOS development: How to add frameworks from pods via post build script?

We are using the Facebook Unity SDK 15.1.0 in Unity 2019.4.40f1. This SDK has some serious bugs. One of them is that it won't add the required frameworks to the Unity-iPhone target. The project will build, but immediately crash on startup.

The frameworks are there, but in a Pod:

在此处输入图像描述

I can add them manually in the General => Frameworks, Libraries, and Embedded Content section of the target:

在此处输入图像描述

Everything works fine then.

However, doing this after every build is quite tedious, so I would like to automate this task via a post build script. I am scratching my head about this, since I cannot find good samples online that actually work.

So my question is: How do you add a.xcframework buried in Facebook SDK's pods so it correctly shows up in the target?

Here is how i would do it. This may not be 100% correct but good enough to modify it to make it work for you. Basically you need to use PBXProject.AddFrameworkToProject to add frameworks.

#if UNITY_IOS
[PostProcessBuild(1)]
public static void ChangeXcodePlist(BuildTarget buildTarget, string pathToBuiltProject) {

    if (buildTarget == BuildTarget.iOS) {
   
        // get pbx project path
        var projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
        if (File.Exists(projPath))
        {
            var proj = new PBXProject();
            proj.ReadFromString(File.ReadAllText(projPath));

            string mainTargetGuid = null, testTargetGuid = null, frameworkTargetGuid = null;

#if UNITY_2019_4_OR_NEWER // APIs are different for getting main unity targets changes based on versions
            mainTargetGuid = proj.GetUnityMainTargetGuid();
            frameworkTargetGuid = proj.GetUnityFrameworkTargetGuid();
#else
            mainTargetGuid = 
proj.TargetGuidByName(PBXProject.GetUnityTargetName());
            testTargetGuid = 
proj.TargetGuidByName(PBXProject.GetUnityTestTargetName());
            frameworkTargetGuid = proj.TargetGuidByName("UnityFramework");
#endif
            
            // add your frameworks here
            if (!String.IsNullOrEmpty(mainTargetGuid))
            {
                Debug.Log("Adding targets to mainTargetGuid")
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKCoreKit.xcframework", false);
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKGamingServicesKit.xcframework", false);
            }
            // add to test target aswell if exists
            if (!String.IsNullOrEmpty(testTargetGuid))
            {
                Debug.Log("Adding targets to testTargetGuid")
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKCoreKit.xcframework", false);
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKGamingServicesKit.xcframework", false);
            }

            if (!String.IsNullOrEmpty(frameworkTargetGuid))
            {
                Debug.Log("Adding targets to frameworkTargetGuid")
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKCoreKit.xcframework", false);
                proj.AddFrameworkToProject(mainTargetGuid, "FBSDKGamingServicesKit.xcframework", false);
            }

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

You can also use PBXProject.ContainsFramework before you include the frameworks.

The code in the previous answer is not working. Freimworks are not added like that.

Correct code for adding facebook's frameworks (working):

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

     string projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
     PBXProject project = new PBXProject();
     project.ReadFromFile(projectPath);
     string mainTargetGuid = project.GetUnityMainTargetGuid();

     List<string> frameworks = new List<string> {
         "FBSDKCoreKit",
         "FBSDKGamingServicesKit"
     };
     foreach (string framework in frameworks) {
         string frameworkName = framework + ".xcframework";
         var src = Path.Combine("Pods", framework, "XCFrameworks", frameworkName);
         var frameworkPath = project.AddFile(src, src);
         project.AddFileToBuild(mainTargetGuid, frameworkPath);
         project.AddFileToEmbedFrameworks(mainTargetGuid, frameworkPath);
     }
     // Write.
     project.WriteToFile(projectPath);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM