Newtonsoft.Json-for-Unity icon indicating copy to clipboard operation
Newtonsoft.Json-for-Unity copied to clipboard

This package still won't work on unity (IL2CPP builds)

Open lordaltair opened this issue 4 years ago • 9 comments

Hi, I'm trying to use this package on my unity project. As the description says, the purpose of the package is to get IL2CPP builds. It is working on Unity editor but it is not working on android.

I'm using Unity 2019.3.7.f1 and the Newtonsoft.Json-for-Unity package version is 13.0.102. I tried other versions, too like 12.0.302 or 10.0.302. My OS is windows 10 and unity android built target version is 26. Is there any specific settings that I have to use? I didn't say anything in the description.

Thanks for your help.

lordaltair avatar Aug 13 '21 18:08 lordaltair

Hi @lordaltair! Thanks for reporting this!

In what way does it not work on Android?

Are there errors during compilation/building or during runtime? And if so, could you post the errors you receive?

Kind regards

applejag avatar Aug 14 '21 09:08 applejag

Hi @lordaltair! Thanks for reporting this!

In what way does it not work on Android?

Are there errors during compilation/building or during runtime? And if so, could you post the errors you receive?

Kind regards

Hi, Thanks for helping. No, it is on runtime, I have an android build but nothing works. The game works but the newtonsoft JSON package won't work. It isn't serialize or deserialize JSON or ...

lordaltair avatar Aug 14 '21 12:08 lordaltair

Hi, Thanks for helping. No, it is on runtime, I have an android build but nothing works. The game works but the newtonsoft JSON package won't work. It isn't serialize or deserialize JSON or ...

If it's during runtime then I suggest you look into potential AOT issues. I've written some guides in the wiki. Recommend starting with What even is AOT followed by Fix AOT compilation errors.

To see the errors that occurs, I would suggest something like making a development/debug build, potentially by attaching your local Unity Editor to your Android phone so errors are relayed to your Unity Editor

applejag avatar Aug 14 '21 12:08 applejag

Hi, Thanks for helping. No, it is on runtime, I have an android build but nothing works. The game works but the newtonsoft JSON package won't work. It isn't serialize or deserialize JSON or ...

If it's during runtime then I suggest you look into potential AOT issues. I've written some guides in the wiki. Recommend starting with What even is AOT followed by Fix AOT compilation errors.

To see the errors that occurs, I would suggest something like making a development/debug build, potentially by attaching your local Unity Editor to your Android phone so errors are relayed to your Unity Editor

Well. I got no error. I use my mobile and I saw the errors. there was no error in the project. It is like that the compiler does not compile codes in which I used the package. Thanks for the links but those weren't helpful

lordaltair avatar Aug 14 '21 13:08 lordaltair

Could you screenshot the errors you saw on your phone and post them here?

applejag avatar Aug 14 '21 14:08 applejag

It happens to me too. Running Unity 2020.3.7f1 and Windows 10. It works perfectly on the editor, it deserializes a JSON string as you would expect. But when I make a build for Android, it just doesn't deserialize, everything is created but with null values. No errors are shown. I'm using a custom JsonConverter on some properties with the attribute [JsonConverter(typeof(JsonToRxConverter))], but I've tried without it, and seems to do the same. The funny thing is that even if the values are null it creates the correct number of elements from the array

albermotion avatar Aug 17 '21 12:08 albermotion

After adding a custom TraceWriter to try to find any info in the logs, I was able to see this log on every property:

Unable to deserialize value to non-writable property

Even though all my properties are public properties with public get and set

albermotion avatar Aug 17 '21 15:08 albermotion

After adding a custom TraceWriter to try to find any info in the logs, I was able to see this log on every property:

Unable to deserialize value to non-writable property

Even though all my properties are public properties with public get and set

As IL2CPP has a much stricter stripping algorithm, my guess is that your code never explicitly sets those values, but it does get them. A property setter is just a method, and if it's unused then IL2CPP will strip it out.

Suggest using link.xml or a [Preserve] attribute on the type so that the property setters are not stripped out.

applejag avatar Aug 17 '21 15:08 applejag

You should add the following to the link.xml: <assembly fullname="Newtonsoft.Json" preserve="all"/> That should solve your issue with IL2CPP

godboutj avatar Aug 17 '21 18:08 godboutj