System.Text.Json and Il2CPP code stripping

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 ?

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: IL2CPP Build Time Improvements - Seeking Feedback

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.

Thank you @JoshPeterson for the fast answer. Will try that!

@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.

@pohype

@bdovaz yup i know, I’m the one who wrote the topic :wink:

1 Like

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

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

which means i can’t publish to google play Imgur: The magic of the Internet

cross-posted here: IL2CPP Build Time Improvements - Seeking Feedback page-3#post-8721021

Edit

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

Edit 2

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