applejag/Newtonsoft.Json-for-Unity

Help: Error when building Unity project

gavinb80 opened this issue · 6 comments

Hi,

I've added the latest version of Json.net package (12.0.3) to my Unity project, but when I build the project to test on my iOS device I get the following error:

Exception: /Applications/2019.4.6f1/Unity.app/Contents/il2cpp/build/deploy/net471/UnityLinker.exe did not run properly!
UnityEditorInternal.Runner.RunProgram (UnityEditor.Utils.Program p, System.String exe, System.String args, System.String workingDirectory, UnityEditor.Scripting.Compilers.CompilerOutputParserBase parser) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildUtils.cs:128)
UnityEditorInternal.Runner.RunManagedProgram (System.String exe, System.String args, System.String workingDirectory, UnityEditor.Scripting.Compilers.CompilerOutputParserBase parser, System.Action`1[T] setupStartInfo) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/BuildUtils.cs:71)
UnityEditorInternal.AssemblyStripper.RunAssemblyLinker (System.Collections.Generic.IEnumerable`1[T] args, System.String& out, System.String& err, System.String linkerPath, System.String workingDirectory) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/AssemblyStripper.cs:166)
UnityEditorInternal.AssemblyStripper.StripAssembliesTo (System.String outputFolder, System.String& output, System.String& error, System.Collections.Generic.IEnumerable`1[T] linkXmlFiles, UnityEditorInternal.UnityLinkerRunInformation runInformation) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/AssemblyStripper.cs:159)
UnityEditorInternal.AssemblyStripper.RunAssemblyStripper (UnityEditorInternal.UnityLinkerRunInformation runInformation) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/AssemblyStripper.cs:301)
UnityEditorInternal.AssemblyStripper.StripAssemblies (System.String managedAssemblyFolderPath, UnityEditorInternal.BaseUnityLinkerPlatformProvider unityLinkerPlatformProvider, UnityEditorInternal.IIl2CppPlatformProvider il2cppPlatformProvider, UnityEditor.RuntimeClassRegistry rcr, UnityEditor.ManagedStrippingLevel managedStrippingLevel) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/AssemblyStripper.cs:178)
UnityEditorInternal.IL2CPPBuilder.Run () (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/Il2Cpp/IL2CPPUtils.cs:327)
UnityEditorInternal.IL2CPPUtils.RunIl2Cpp (System.String tempFolder, System.String stagingAreaData, UnityEditorInternal.IIl2CppPlatformProvider platformProvider, System.Action`1[T] modifyOutputBeforeCompile, UnityEditor.RuntimeClassRegistry runtimeClassRegistry) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/Il2Cpp/IL2CPPUtils.cs:65)
UnityEditor.iOS.PostProcessiPhonePlayer.CrossCompileManagedDlls (UnityEditor.iOS.PostProcessiPhonePlayer+BuildSettings bs, UnityEditor.iOS.PostProcessiPhonePlayer+ProjectPaths paths, UnityEditor.AssemblyReferenceChecker checker, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport buildReport) (at /Users/builduser/buildslave/unity/build/PlatformDependent/iPhonePlayer/Extensions/Common/BuildPostProcessor.cs:898)
UnityEditor.iOS.PostProcessiPhonePlayer.PostProcess (UnityEditor.iOS.PostProcessiPhonePlayer+BuildSettings bs, UnityEditor.iOS.PostProcessiPhonePlayer+ProjectPaths paths, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport buildReport) (at /Users/builduser/buildslave/unity/build/PlatformDependent/iPhonePlayer/Extensions/Common/BuildPostProcessor.cs:731)
UnityEditor.iOS.PostProcessiPhonePlayer.PostProcess (UnityEditor.iOS.PostProcessorSettings postProcessorSettings, UnityEditor.Modules.BuildPostProcessArgs args) (at /Users/builduser/buildslave/unity/build/PlatformDependent/iPhonePlayer/Extensions/Common/BuildPostProcessor.cs:679)
UnityEditor.iOS.iOSBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args) (at /Users/builduser/buildslave/unity/build/PlatformDependent/iPhonePlayer/Extensions/Common/ExtensionModule.cs:45)
Rethrow as BuildFailedException: Exception of type 'UnityEditor.Build.BuildFailedException' was thrown.
UnityEditor.iOS.iOSBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args) (at /Users/builduser/buildslave/unity/build/PlatformDependent/iPhonePlayer/Extensions/Common/ExtensionModule.cs:49)
UnityEditor.Modules.DefaultBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args, UnityEditor.BuildProperties& outProperties) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Modules/DefaultBuildPostprocessor.cs:27)
UnityEditor.PostprocessBuildPlayer.Postprocess (UnityEditor.BuildTargetGroup targetGroup, UnityEditor.BuildTarget target, System.String installPath, System.String companyName, System.String productName, System.Int32 width, System.Int32 height, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at /Users/builduser/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:340)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at /Users/builduser/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:197)

My class:

namespace TestGame
{

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class GameModel
    {
        [JsonProperty("playerTwoColor")]
        public string PlayerTwoColor { get; set; }

        [JsonProperty("tokens")]
        public Token[] Tokens { get; set; }

        [JsonProperty("playerOneColor")]
        public string PlayerOneColor { get; set; }

        [JsonProperty("isLocalPlayersTurn")]
        public bool IsLocalPlayersTurn { get; set; }

        [JsonProperty("gameGrid")]
        public long[][] GameGrid { get; set; }

        [JsonProperty("gridSize")]
        public long GridSize { get; set; }
    }

    public partial class Token
    {
        [JsonProperty("player")]
        public long Player { get; set; }

        [JsonProperty("coord")]
        public Coord Coord { get; set; }
    }

    public partial class Coord
    {
        [JsonProperty("x")]
        public long X { get; set; }

        [JsonProperty("y")]
        public long Y { get; set; }
    }

    public partial class GameModel
    {
        public static GameModel FromJson(string json) => JsonConvert.DeserializeObject<GameModel>(json, TestGame.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this GameModel self) => JsonConvert.SerializeObject(self, TestGame.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}

Then from one of my game C# scripts I call:

        GameModel game;// = GameModel();

        if (match.Data != null)
        {
            var x = System.Text.Encoding.UTF8.GetString(match.Data);

            Debug.Log($"Data: {x}");
            game = GameModel.FromJson(x)
        }
        else
        {
            Debug.Log("New Game");
        }

The TestGame class was generated via https://app.quicktype.io and it seems to work ok in a small C# Console app via the standard Newtonsoft.Json library from Nuget.

Any help appreciated

Hi @gavinb80! Thanks for reporting this! I reformatted your description a little. I conclude from your error message that you're using Unity 2019.4.6.f1, but I cannot make any other informative guesses on what could be the cause here, sorry to say. I will try reproduce the issue and report back on my findings.

tsira commented

Two suggestions I have looking in a very cursory manner at the error @gavinb80:

  1. Look at the stripping level setting in the Project Settings, also if it is for specific classes being stripped out and add them to the link.xml to prevent that;
  2. Make sure you are using the AOT version of Newtonsoft for the iOS build.

cc: @jilleJr

  1. Make sure you are using the AOT version of Newtonsoft for the iOS build.

If you're using the UPM package, or even importing the package as-is with it's entire content, then it should already be configured to automatically select the AOT compatible build on iOS. Good to double-check though!

If you have 2019.4.* Unity version, don`t use "jillejr.newtonsoft.json-for-unity": "12.0.301" , use "jillejr.newtonsoft.json-for-unity": "12.0.201" instead inside manifest.xml and it will build it sucessfully.

I got this error when build webgl on 12.0.301 version. On older I still build webgl success.
Fatal error in Unity CIL Linker System.ArgumentException: Member 'System.Void System.Runtime.CompilerServices.NullableAttribute::.ctor(System.Byte)'

@trongcongdn2 That error seems related to what's mentioned in #54. A fix for that can be found in #72 and is coming in v12.0.302

As for your sample @gavinb80, I was unable to reproduce it on both Low and Medium stripping level as it built successfully. On High stripping level issue #54 took precedence. That error may be the one you refered to here, but I'm unsure.

I will be including the following link.xml config just to make sure:

<linker>
	<assembly fullname="Newtonsoft.Json">
		<type fullname="Newtonsoft.Json.Converters.IsoDateTimeConverter" preserve="all"/>
	</assembly>
</linker>

I will close this issue via PR #72. Please don't refrain from reopening this or creating a new issue if any similar errors ever appear.