Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

System.Text.Json and Il2CPP code stripping

Discussion in 'Web' started by poprev-3d, Mar 17, 2021.

  1. poprev-3d

    poprev-3d

    Joined:
    Apr 12, 2019
    Posts:
    71
    Hi everyone,

    I'm trying to use the brand new System.Text.Json library with Il2CPP builds of Unity (using 2019.4.16f).

    The issue is that for some reason, I cannot prevent the builder from stripping some parts of the library, which results in errors at runtime.

    When trying to serialize an simple float array
    float[3]
    , I get the error :


    ExecutionEngineException: Attempting to call method 'System.Text.Json.Serialization.Converters.ArrayConverter`2[[System.Single[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Single, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]::.ctor' for which no ahead of time (AOT) code was generated.
    at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00000] in <00000000000000000000000000000000>:0
    at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x00000] in <00000000000000000000000000000000>:0


    It seems that the generic System.Text.Json.Serialization.Converters.ArrayConverter class of System.Text.Json could not be included in the build.

    I had this issue with string and base types before, but I solved it by adding a link.xml to my assets folder to prevent some stripping, which for some reason doesn't work for arrays and other more complex types.


    <linker>
    <assembly fullname="System.Text.Json">
    <namespace fullname="System.Text.Json" preserve="all" />
    <namespace fullname="ystem.Text.Json.Serialization" preserve="all" />
    <namespace fullname="System.Text.Json.Serialization.Converters" preserve="all" />
    <type fullname="System.Text.Json.Serialization.Converters.*" preserve="all"/>
    </assembly>
    </linker>


    To use System.Text.Json on Unity, I use the netstandard2.0 version of the System.Text.Json package, which does not use System.Reflection.Emit (unsupported on Il2CPP).

    Did anyone manage to make System.Text.Json to work with Il2CPP ?
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,920
    The error message here is not related to managed code stripping, but is rather an ahead-of-time compilation limitation.

    IL2CPP never sees the type ArrayConverter<float[], float> at compile time, so it does not know to generate the code for that type.

    Note that we do have some new changes that can allow this code to work and avoid this specific issue. See the forum post here: https://forum.unity.com/threads/il2cpp-build-time-improvements-seeking-feedback.1064135/

    You will need to try a 2021.2 alpha version to test this though. If you are able to do that, I'd love to know what happens.
     
  3. poprev-3d

    poprev-3d

    Joined:
    Apr 12, 2019
    Posts:
    71
  4. poprev-3d

    poprev-3d

    Joined:
    Apr 12, 2019
    Posts:
    71
    @JoshPeterson I tried with and without the IL2CPP_ADDITIONAL_ARGS=--generics-option=EnableFullSharing environment variable (setx IL2CPP_ADDITIONAL_ARGS --generics-option=EnableFullSharing) with the 2021.2.0a9.1376 and it does not seem to change anything, i still get the same error.
     
  5. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,042
  6. poprev-3d

    poprev-3d

    Joined:
    Apr 12, 2019
    Posts:
    71
    @bdovaz yup i know, I'm the one who wrote the topic ;)
     
    bdovaz likes this.
  7. PoweredOnSoftware

    PoweredOnSoftware

    Joined:
    Apr 10, 2015
    Posts:
    8
    i'm having the same issue with Unity 2021.3.11f1

    i'm trying to use YoutubeExplode which depends on System.Text.Json

    but i keep getting errors related to missing methods when use IL2CPP (both .NET standard 2.1 and .NET framework paths)

    i tried adding a link.xml, and disabling Strip Engine Code, and low / disabled stripping levels

    https://github.com/Tyrrrz/YoutubeExplode/discussions/679#discussioncomment-4639222

    https://www.nuget.org/packages/YoutubeExplode/6.2.5#dependencies-body-tab

    NOTE: when i switch to Mono scripting backend, it works. But then I can't target ARM64 / v8a :(

    which means i can't publish to google play https://i.imgur.com/BoBXowU.png

    cross-posted here: https://forum.unity.com/threads/il2...-seeking-feedback.1064135/page-3#post-8721021

    **Edit**

    Oh this wasn't fixed until unity 2022. I need to upgrade from 2021 LTS ‍

    https://blog.unity.com/technology/feature-preview-il2cpp-full-generic-sharing-in-unity-20221-beta

    **Edit 2**

    well, upgraded from 2021 to 2022, but now i have a build error instead of a runtime error:

    https://twitter.com/PoweredOnSoft/status/1613297639155728384?s=20
     
    Last edited: Jan 11, 2023