-
Notifications
You must be signed in to change notification settings - Fork 15.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reflection error with Unity when using il2cpp #5422
Comments
Hmm. I've managed to get the same code to pass in a standalone app with il2cpp: using Google.Protobuf;
using System;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
try
{
var res = (IMessage) Activator.CreateInstance(typeof(TestApp.Error));
var msg = JsonParser.Default.Parse("{\"code\":\"PIT-000\",\"msg\":\"GetError is returning a custom error\"}", res.Descriptor);
Console.WriteLine(msg);
}
catch (TypeInitializationException ex)
{
Console.WriteLine("Type initialization exception");
Console.WriteLine(ex.InnerException);
}
}
}
} Output:
Here's the il2cpp invocation I'm using:
That's using Google.Protobuf 3.6.1 as well. I don't know which il2cpp flags are used by Unity. Hmm. |
That's interesting. I'll maybe try with another version of Unity. |
The version of Unity I have installed is 2018.2.0b2. Unfortunately I can't see a way of finding a version of il2cpp... Here's my complete build script, if you want to try my test app for yourself - change the directories in the obvious way:
(I note that I'm using protoc from 3.5.1, but I'd be astonished if that's changed enough to be an issue.) |
Hey, My team also recently had a few problems with C# reflections using Unity with il2cpp and enums using the 3.6.1 build. Note I am not the unity dev who dug into this, but this is a list of things we worked through. This sounds like the same issue as this, but maybe it's not?
This is probably something that should be mentioned in Protobuf documentation? Relevant part of Unity documentation: https://docs.unity3d.com/Manual/IL2CPP-BytecodeStripping.html
Attempting to call method 'Google.Protobuf.Reflection.ReflectionUtil+ReflectionHelper`2[[Cdm.SerializeStateData, DataModelBindings, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null],[Cdm.eSerialization+Types+Type, DataModelBindings, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]::.ctor' for which no ahead of time (AOT) code was generated. (eSerialization is an enum) The issue appears to be with this file in particular: https://github.com/protocolbuffers/protobuf/blob/master/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs The use of generics through reflection + not letting the IL2CPP compiler know what classes will be needed is what caused the error - code was requested that was not generated, and there was no way to generate it on demand (as it would normally happen with JIT runtimes). To get past this we ended up replacing the smarts and quickness of the generics via reflection in ReflectionUtils.cs with pretty simple code that calls invoke instead. But I don't think this is going to be something we want in protubuf as invoke can be kind of slow, but there is probably some way to apply Google.Protobuf.Reflection.FileDescriptor.ForceReflectionInitialization to the custom types to make sure AOT code is generated for them but I'm not sure how that's done exactly, and how easy it would be. Again, this issue description is a summary of notes of what we did over a week or so and maybe difficult to follow. Please let me know if parts need further explanation. But with all these changes we were able to create and pass protobuf JSON from C# to C++ in our packaged Unity UWP game. |
Note that we don't version il2cpp separately from Unity. The IL2CPP version is the same as the Unity editor version. |
is there any update on this issue? |
@na-ka-na: Not from me, I'm afraid. This is only an area I can dip into occasionally, and this problem requires significantly more time. (This sort of brittleness is precisely why I've always been very nervous of supporting Unity - things that work on one version then failing on a different one.) |
i try Unity 18.3.8f1 same Error
in this Simple Code
Is the cause Unity Version? |
Well sort of - in that I've had this working with some versions of Unity. But I'm not suggesting that you should change which version of Unity you're using. I'm suggesting that fixing this properly requires a significant amount of time and effort, ideally with automated testing against multiple versions of il2cpp. |
ok, very thank you. |
Unfortunately at the moment I would suggest avoiding any requirement on reflection - that's calling |
ok i try other Parse |
I have managed to fix the issue with the latest version of protobuf by adding a link.xml file in the Assets folder of the project with the following contents: <linker>
<assembly fullname="Google.Protobuf" preserve="all"/>
</linker> Since we also have to support .NET3.5, we compiled protobuf for that platform as well. |
Thank You! |
Fixed the issue for me - thanks! |
I'm having a similar issue: when trying to get a message descriptor I got a
I can't seem to use I'm using version 3.6.1 compiled to .NET 3.5 and using Unity 2018.4.23f1. |
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment. This issue is labeled |
We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please reopen it. This issue was closed and archived because there has been no new activity in the 14 days since the |
What version of protobuf and what language are you using?
Version: v3.6.1
Language: C#
What operating system (Linux, Windows, ...) and version?
MacOS Mojave 10.14
What runtime / compiler are you using (e.g., python version or gcc version)
Apple LLVM version 10.0.0 (clang-1000.11.45.2)
Target: x86_64-apple-darwin18.0.0
Thread model: posix
What did you do?
Test.cs
executes correctly.The text was updated successfully, but these errors were encountered: