我可以删除 protobuf-net 引用的 System.Windows.Forms.dll

Can I remove System.Windows.Forms.dll referenced by protobuf-net

我目前正在努力减少从 Unity 生成的 APK 的最终构建大小

从Unity Editor Log中,我发现了一些不需要的DLL文件。最大的是 System.Windows.Forms.dll, referenced byprotobuf-net`。

层次结构是:protobuf-net -> System.ServiceModel -> System.Messaging -> System.Windows.Forms

为什么 protobuf-net 引用 windows 特定程序集?

如何删除它?

首先,System.Windows.Forms 实际上并不是 Windows 特定于 Unity 的情况。 Unity 在后台使用 Mono(Microsoft .NET Framework 的开源实现),并且它是完全跨平台的。 dll 的名称是 System.Windows.Forms 只是为了使 Mono 与 Microsoft 的 .NET Framework 实现兼容。

如果您有兴趣,这里有关于 Mono's WinForms implementation 的更多信息。

Protobuf-net 内部使用 System.Messaging,这意味着 System.Windows.Forms 也将被包含在内。在查看 Mono 的 System.Messaging 实现的源代码时,我在 MessageQueue.resx and Message.resx.

中至少看到了两个对 System.Windows.Forms 的引用

同样在Mono的System.Messaging的makefile中,可以看到如下代码:

ifdef NO_WINFORMS_DEPENDENCY
LIB_MCS_FLAGS += -d:NO_WINFORMS_DEPENDENCY
else
LIB_REFS += System.Windows.Forms
endif

总而言之,我认为您无法摆脱装配。我对 Unity 不是很熟悉,但是查看 protobuf-net 和 Mono 的源代码,您需要构建自己的版本或使用一些魔法来摆脱它。

编辑: 我想知道 Unity 是否内置了某种链接或程序集剥离机制?例如,当构建一个 Android 应用程序时,我可以使用一个链接器,它从我正在使用的程序集中删除所有未使用的方法、类 和其他内容。在您的情况下,只剩下 protobuf-net 使用的 System.Windows.Forms 程序集的一小部分。

编辑 2: This 可能值得研究,即使它是 iOS 特定的。