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

Problems Building a native plugin for iOS Library with swift in Unity 2019.3

Discussion in 'iOS and tvOS' started by PatientZero, Apr 28, 2020.

  1. PatientZero

    PatientZero

    Joined:
    Apr 8, 2015
    Posts:
    30
    Hi,

    I'm having trouble building my iOS swift library since the update to 2019.3. With 2019.2 everything still works.
    The problem seems to be that the properties that I set for the Xcode project like so do not get set properly in the Xcode project in 2019.3.

    Code (CSharp):
    1.  
    2.                 var targetGuid = proj.TargetGuidByName(PBXProject.GetUnityTargetName());
    3.                 //// Configure build settings
    4.                 proj.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
    5.                 proj.SetBuildProperty(targetGuid, "SWIFT_OBJC_BRIDGING_HEADER", "Libraries/UnitySwift/UnitySwift-Bridging-Header.h");
    6.                 proj.SetBuildProperty(targetGuid, "SWIFT_OBJC_INTERFACE_HEADER_NAME", "unityswift-Swift.h");
    7.                 proj.AddBuildProperty(targetGuid, "LD_RUNPATH_SEARCH_PATHS", "@executable_path/Frameworks");
    8.                 proj.SetBuildProperty(targetGuid, "SWIFT_VERSION", "5.0");
    9.  
    If I replace
    Code (CSharp):
    1.  
    2. var targetGuid = proj.TargetGuidByName(PBXProject.GetUnityTargetName());
    3.  
    with
    Code (CSharp):
    1.  
    2. var targetGuid = proj.GetUnityMainTargetGuid();
    3.  
    all properties in XCode get set as user defined variables in 2019.3 while in 2019.2 they get set properly as config for the swift compiler.
    If I set it as
    Code (CSharp):
    1.  
    2. var targetGuid = proj.GetUnityFrameworkTargetGuid();
    3.  
    the properties do get properly set for the swift compiler but I get an error saying "using bridging in headers with framework targets is unsupported"

    Any suggestions how to fix that?

    Thx in advance!
     
    Last edited: May 28, 2020
    cloutiertyler likes this.
  2. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    505
    This is specific to Xcode's restriction. There are some Q&A on StackOverflow with the same error message may help you investigate. The following is one of them. Hope it helps.

    https://stackoverflow.com/questions...headers-with-framework-targets-is-unsupported

    I do recommend to investigate build issues like this in Xcode project to help iterate faster. Once the fix is found, you can replicate it in the post-build processing scripts.
     
  3. qoobit

    qoobit

    Joined:
    Aug 19, 2014
    Posts:
    51
    The culprit is that the post build processors only get sent to the target but not the project in you xcode project. You need to copy them over post build. I've been trying to find a way to automate this but their current functions in 2019.3 don't seem to allow it. I've tried
    PBXProject.ProjectGuid
    as a test but have yet to compile correctly.
     
    Last edited: Jun 4, 2020
  4. PatientZero

    PatientZero

    Joined:
    Apr 8, 2015
    Posts:
    30
    It is possible with Unity 2019.3 but I had to fiddle around quite a bit to get it working since it is documented nowhere.
    The actual setup isn't all that different from 2019.2
    This is my whole PostProcess.cs


    Code (CSharp):
    1. #if UNITY_IOS
    2. using UnityEngine;
    3. using UnityEditor;
    4. using UnityEditor.Callbacks;
    5. using UnityEditor.iOS.Xcode;
    6. using System.Collections;
    7. using System.Collections.Generic;
    8. using System.Diagnostics;
    9. using System.IO;
    10. using System.Linq;
    11.  
    12. namespace UnitySwift {
    13.     public static class PostProcessor {
    14.  
    15.         [PostProcessBuild]
    16.         public static void OnPostProcessBuild(BuildTarget buildTarget, string buildPath) {
    17.             if(buildTarget == BuildTarget.iOS) {
    18.              
    19.                 ChangeXcodePlist(buildPath);
    20.              
    21.                 // So PBXProject.GetPBXProjectPath returns wrong path, we need to construct path by ourselves instead
    22.                 // var projPath = PBXProject.GetPBXProjectPath(buildPath);
    23.                 var projPath = buildPath + "/Unity-iPhone.xcodeproj/project.pbxproj";
    24.                 var proj = new PBXProject();
    25.                 proj.ReadFromFile(projPath);
    26.  
    27.                 var targetGuid = proj.GetUnityFrameworkTargetGuid();
    28.  
    29.                 UnityEngine.Debug.Log("targetGuid: " + targetGuid);
    30.  
    31.                 //// Configure build settings
    32.                 proj.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
    33.  
    34.                 proj.SetBuildProperty(targetGuid, "SWIFT_OBJC_INTERFACE_HEADER_NAME", "UnityFramework-Swift.h");
    35.                 proj.AddBuildProperty(targetGuid, "LD_RUNPATH_SEARCH_PATHS", "@executable_path/Frameworks");
    36.                 proj.SetBuildProperty(targetGuid, "SWIFT_VERSION", "5.0");
    37.  
    38.                 proj.WriteToFile(projPath);
    39.  
    40.                 ProjectCapabilityManager projCapability = new ProjectCapabilityManager(projPath, "Unity-iPhone/mmk.entitlements", "Unity-iPhone");
    41.  
    42.                 projCapability.AddHealthKit();
    43.                 projCapability.WriteToFile();
    44.             }
    45.         }
    46.  
    47.          public static void ChangeXcodePlist(string path)
    48.          {
    49.                 string plistPath = path + "/Info.plist";
    50.                 PlistDocument plist = new PlistDocument();
    51.                 plist.ReadFromFile(plistPath);
    52.  
    53.                 PlistElementDict rootDict = plist.root;
    54.  
    55.                 UnityEngine.Debug.Log(">> Automation, plist ... <<");
    56.  
    57.                 // example of changing a value:
    58.                 // rootDict.SetString("CFBundleVersion", "6.6.6");
    59.  
    60.                 // example of adding a boolean key...
    61.                 // < key > ITSAppUsesNonExemptEncryption </ key > < false />
    62.                 rootDict.SetString("NSHealthShareUsageDescription", "Recording of fitness data!!");
    63.  
    64.                 File.WriteAllText(plistPath, plist.WriteToString());
    65.         }
    66.     }
    67. }
    68. #endif
     
  5. qoobit

    qoobit

    Joined:
    Aug 19, 2014
    Posts:
    51
    In the end with additional tinkering I managed to split my post build into references from
    ProjectGuid
    and
    GetUnityMainTargetGuid
    referencing a mixture of
    AddFileToBuild
    ,
    AddBuildProperty
    ,
    SetBuildProperty
    . Also using the platform dependent compilation helps with backwards support (
    UNITY_2019_3_OR_NEWER
    )
     
  6. aasiq

    aasiq

    Joined:
    Dec 29, 2016
    Posts:
    16
    @qoobit , Can you share the post process snippet for me to understand it better? I've tried the solution suggested by @PatientZero and the issue still exist. I am using Unity 19.4.1, Xcode 11.5
     
  7. aasiq

    aasiq

    Joined:
    Dec 29, 2016
    Posts:
    16
    @PatientZero Sorry, I didn't explain the issue clearly. The issue now is after building fro Unity, it throws
    'UnityFramework-Swift.h' file not found in .mm file on Xcode
     
    cloutiertyler likes this.
  8. qoobit

    qoobit

    Joined:
    Aug 19, 2014
    Posts:
    51
    That's a new one for me as I haven't ran into any swift related issues. Might have to see the project in order to have a better idea. The only suggestion I can recommend is to isolate the asset/plugin/script into a fresh project and tackle it there to make sure there is no conflicting build processes.
     
  9. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    848
    I just ran into the same problem. Did you mange to fix it?
     
  10. tahahalabi

    tahahalabi

    Joined:
    Feb 13, 2021
    Posts:
    1
    Also me, did anyone fixed it?
     
  11. Deleted User

    Deleted User

    Guest

  12. RubenGarciaHernandez

    RubenGarciaHernandez

    Joined:
    Jan 13, 2022
    Posts:
    6