如何解决"Newtonsoft.Json.JsonSerializationException无法找到要用于类型的构造函数"的问题. Android错误? [英] How to solve "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" Android error?

查看:309
本文介绍了如何解决"Newtonsoft.Json.JsonSerializationException无法找到要用于类型的构造函数"的问题. Android错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Unity3D在App中工作.

I'm working in an App with Unity3D.

我将自己的实用程序DLL与某些类一起使用.

Im using my own utilities DLL with some classes.

我已经工作了几个月,并且进行了许多构建/编译,没有问题.

I've been working for months now, and made lots of builds/compilations with no problem.

上周向DLL添加了一个新类,它在我的PC上运行良好,但是当我构建该项目并在我的Android设备上使用它时,它停止工作. Android Monitor程序向我显示该错误:

Last week added a new class to the DLL, it worked well in my PC but when I builded the project and used it in my Android device it stopped working. The Android Monitor program shows me that error:

"Newtonsoft.Json.JsonSerializationException无法找到用于类型的构造函数"

Newtonsoft可以为其找到构造函数的类型是我最近添加的新类.

The types that Newtonsoft can find constructors for, are the new classes I added recently.

重要:所有其他类均已反序列化,没有错误.

Important: All the other classes are deserialized with no errors.

新类具有的唯一不同之处是命名空间类型.变量,方法和构造函数仍然相同.

The only different think that the new class has is the namespace and the type. Variables, methods and constructors still the same.

该项目在PC上正常运行也很重要,该问题仅在Android上出现.

It is also important that the project works fine on PC, the problem only occurs in Android.

我知道我可以对类进行一些自定义反序列化或添加标签等.但事实是,只有那个类会导致问题,并且没有理由更改JSon反序列化系统.

I know I can do some custom deserializers or add tags, etc.. to my class. But the thing is that only that class causes the problem and it is no reason to change the JSon deserialization system.

谢谢.

推荐答案

背景:这是一个常见的问题,它源于UnityLinker的字节码剥离算法.发生的是,UnityLinker无法看到对类型构造函数的任何使用(因为Newtonsoft.Json仅通过反射来使用它,而编译器无法预见),因此将其删除.仅当使用IL2CPP脚本后端进行构建时,才会激活此字节码剥离,这就是为什么在PC上构建(或其他仅使用Mono的情况)构建时不会遇到任何问题的原因.

Background: This is a common problem that roots from the UnityLinker's bytecode stripping algorithms. What's happening is that the UnityLinker cannot see any use of your types constructor (as Newtonsoft.Json only uses it via reflection, which a compiler cannot foresee), so it removes it. This bytecode stripping is only activated when building with the IL2CPP scripting backend, which is why you didn't experience any problem when building on your PC (or any other solely using Mono for that matter).

修复程序::要解决此问题,您可以为该类型(甚至仅针对该构造函数)禁用字节码剥离.您可以使用link.xml文件执行此操作.示例:

The fix: To overcome this, you can disable bytecode stripping for that type (or even just for that constructor). You can do this using a link.xml file. Example:

<linker>
    <assembly fullname="MyAssembly">
        <type fullname="MyAssembly.MyCSharpClass">
            <!-- disables stripping just for the constructor -->
            <method signature="System.Void .ctor()"/>
        </type>

        <!-- disables stripping for the entire type -->
        <type fullname="MyAssembly.MyCSharpClass" preserve="all" />
    </assembly>
</linker>

我已经写了一些有用的页面,其中涉及使用link.xml以及其他工具来解决AOT问题,因为我发现Unity的文档缺乏而且分散: https://github.com/jilleJr /Newtonsoft.Json-for-Unity/wiki/Fix-AOT-using-link.xml

I've written some helpful pages on using link.xml among other tools to cope with AOT issues, as I find Unity's documentation to be lacking and scattered: https://github.com/jilleJr/Newtonsoft.Json-for-Unity/wiki/Fix-AOT-using-link.xml

要详细了解原因,请参阅我的说明页: https://github.com/jilleJr/Newtonsoft.Json-for-Unity/wiki/What-even-is-AOT

To read more about why this is, please refer to my explanation page: https://github.com/jilleJr/Newtonsoft.Json-for-Unity/wiki/What-even-is-AOT

这篇关于如何解决"Newtonsoft.Json.JsonSerializationException无法找到要用于类型的构造函数"的问题. Android错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆