JsonConvert.DeserializeObject error on IL2CPP android build

I am getting an error when I call JsonConvert.DeserializeObject

here are the errors:

2024/09/17 14:05:00.044 26338 26362 Error Unity ExecutionEngineException: Attempting to call method 

'System.Collections.Generic.List1[[_MainScript+Helper_TransformStruct, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]::.cctor' for which no ahead of time (AOT) code was generated.

2024/09/17 14:05:00.044 26338 26362 Error Unity   at System.Reflection.RuntimeConstructorInfo.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00000] in <00000000000000000000000000000000>:0 

2024/09/17 14:05:00.044 26338 26362 Error Unity   at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory+<>c__DisplayClass5_01[T].<CreateDefaultConstructor>b__1 () [0x00000] in <00000000000000000000000000000000>:0 

2024/09/17 14:05:00.044 26338 26362 Error Unity   at Newtonsoft.Json.Serialization.JsonArrayContract.CreateTemporaryCollection () [0x00000] in <00000000000000000000000000000000>:0 

2024/09/17 14:05:00.044 26338 26362 Error Unity   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonArrayContract contract, System.Boolean& createdFromNonDefaultCreator) [0x00000] in <0000000000

here is what I am deserializing, it works correctly on editor and pc build:

as you see i have tried to add a constructor and to preserve, but the error persists

I also tried to create a link.xml file , but the error persists

my code stripping is set to minimal, but the error persists

Thanks in advance for any help

The only thing i can think of is that you check if your “link.xml” is propperly configured?

<linker>
	<assembly fullname="ASSEMBLY_NAME">
		<type fullname="YOUTYPENAME" preserve="all"/> <!-- Ensure a full typename, namespace included -->
	</assembly>
</linker>

Also maybe try using IUnityLinkerProcessor to provide the path to your “link.xml” file when building (Full absolute path does work for sure, i dont know if a relative path works)

PD: Ah, i can see your type is a Nested type from here

'System.Collections.Generic.List1[[_MainScript+Helper_TransformStruct, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]

Also the error you are getting is for the List<Helper_TransformStruct> ctor, not the struct.

So:

  • Make sure all your necesary types are properly written (this includes List’1 and List`1[[yourstruct]] )
  • Remember that a Nested type is written as “NAMESPACE.PARENT_TYPE+NESTED_TYPE” (note the ‘+’ character, instead of a ‘.’)

I think it should be something like

<linker>
	<assembly fullname="mscorlib">            
		<type fullname="System.Collections.Generic.List`1" preserve="all" />
		<type fullname="System.Collections.Generic.List`1[[_MainScript+Helper_TransformStruct, Assembly-CSharp]]" preserve="all"/>
	</assembly>

	<assembly fullname="Assembly-CSharp">   
		<type fullname="_MainScript" preserve="all" />
		<type fullname="_MainScript+Helper_TransformStruct" preserve="all" />
	</assembly>
</linker>

If this doesnt work, try extracting your type outside of its parent class (and rewrite the link.xml). Maybe it has a problem with preserving Nested types.

PD2: also, on a side note, i would recommend that you always write Types within Namespaces, and that your types should not contain strange characters like underscore. Also NEVER start with underscore (compiler-generated types often start with underscore to differentiate themselves)

dont know why its such an hassle just becase its an array of structs all with safe primitives only like byte string and float

It doesnt matter your struct, it is throwing an error on the List<T> ctor, did you read this?

what does it mean? is the fix to make a link.xml.? But I already have that file…

If you carefully read your error, you will see this (relevant lines in bold):

‘System.Collections.Generic.List1[[_MainScript+Helper_TransformStruct, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]::.cctor’ for which no ahead of time (AOT) code was generated.


2024/09/17 14:05:00.044 26338 26362 Error Unity at Newtonsoft.Json.Serialization.JsonArrayContract.CreateTemporaryCollection () [0x00000] in <00000000000000000000000000000000>:0

What seems to be happening, is that when deserializing, Newtonsoft is generating a temporary List<T> for your array. It cannot create the List because the type List<_MainScript.Helper_TransformStruct> does not exist because of stripping.

This is why i’ve told you to ensure you have both List and List<YourStruct> in the link.xml. Please test this before you complain.

PD: “cctor” means “class constructor”

sorry I tried with this:

<linker>
  <assembly fullname="Assembly-CSharp">
    <type fullname="_MainScript" preserve="all" />
    <type fullname="_MainScript+Helper_TransformStruct" preserve="all" />
    <type fullname="_MainScript+Save_Struct" preserve="all" />
  </assembly>
  
  <assembly fullname="mscorlib">
    <type fullname="System.Collections.Generic.List`1" preserve="all" />
    <type fullname="System.Collections.Generic.List`1[[_MainScript+Helper_TransformStruct, Assembly-CSharp]]" preserve="all" />
  </assembly>
</linker>

and its still same result

thanks for your patience

1 Like

Thanks for testing It, the only other thing that i can think off is this:

Also maybe try using IUnityLinkerProcessor to provide the path to your “link.xml” file when building (Full absolute path does work for sure, i dont know if a relative path works)

As i dont know if Unity automatically picks-up the link.xml file, or if It should be explicitly provided through that interface (for my use cases i’ve always used the interface)

Sorry i couldnt provide more help, good luck!

I saw you message yesterday about the Adapter, but couldnt respond. I see that you deleted it so i dont know if it ended up being a solution for you or not.

Anyways i remembered this other info from the official documentation . There, they recommend that you generate “somewhere” a method like this (it’s not to be called, it’s just for the stripper to find it and assume these types are not to be stripped):

public void UsedOnlyForAOTCodeGeneration()
{
    // call any type/method that you want to ensure it is not stripped for IL2CPP 
    // eg:
    new _MainScript.Helper_TransformStruct();
    new List<_MainScript.Helper_TransformStruct()>();
    // ... etc for every type that gives you a "No AOT code was generated" error
}

i dont know if just having a method with this name will work, or if maybe you need to [Preserve] this method. But seems easier to setup than the link.xml in case you want to give it a try

it was solved by having custom code to override writeJson and readJson

and adding that on json serializer settings

because it seems that code avoids reflection, although i dont know what it means

anyway it seems that using “reflection” is not supported in il2cpp and that was the cause of everything