'Using protobuf to communicate between JNI and Unity
My Unity game is using a Android library. They communicate by sending strings over a JNI bridge. Now I want to send protobuf messages over that bridge instead. I already have Google protobuf as a dependency in both Unity and my Android library Project. However I'm not sure how to go ahead.
What I did
- create a .proto file?
- download
protocfor compiling the .proto file for both Unity (c#) and Android (Java) - add the compiled files to the repective projects
In the Android / Java part, everything seems to compile fine. However in Unity I'm getting a ton of compilation errors when adding the compiled .proto file ( == auto generated file)
Here as a placeholder the first compilation error:
Assets\MyApp\ProtobufExample\Detection.cs(62,13): error CS0234: The type or namespace name 'IBufferMessage' does not exist in the namespace 'Google.Protobuf' (are you missing an assembly reference?)
To me this looks like my GoogleProtobuf dependency is not up to date with the compiler, but this is just a guess.
Update
It was a dependency problem. Apparantly I cannot use the latest protobuf compiler (v3.20), as it adds more dependencys that are unknown to the Protobuf library inside Unity. As pointed out in the protobuf github issues, an old version (v3.0) does the trick.
Now I don't have any compilation issues anymore. However I'm getting runtime issues. JNI is complaining that
System.Exception: JNI: Unknown signature for type 'My.Proto.Example.DetectionType' (obj = My.Proto.Example.DetectionType) equal
Could be still an issue of the protoc version being different for Java and C#. Looking at the gradle file I have "protoc:3.18.0" there which indeed might be incompatible with v3.0 which I needed for Unity.
Downgrading Java protobuf leads to gradle compilation errors Unknown generator option: lite.
If anyone has ever done this, could you please recommend a version number that works for both Uniy and Java
Solution 1:[1]
I've also had this problem, I've solved it by upgrading the plugin version (to 2.46) in unity. This allows me to use the newest version of protobuf (3.20) and grpctool (2.45). The plugin is provided by official grpc, you can download it here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | seekingStillness |
