Using protobuf to communicate between JNI and Unity - android

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 protoc for 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

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.

Related

LibC++_shared is Present in APK but Not Found When Loading Libraries During Execution?

I'm working on an Android project with a pre-compiled .so file for OpenCV. The original distribution I used as my base (courtesy of QuickBird Studios) contains 2 .so files: libopencv_java4 and libc++_shared. The two .so files are taken from a .aar library which I had to unpack in order to address a known, long-standing issue in the OpenCV Android implementation. These two SOs are stored in the same folder in my project tree, as seen here. The other ABIs share identical structure to arm64-v8a.
When compiling the APK with Intellij IDEA (v2022.1.3), the APK analyzer shows that both .so files have been packaged in to lib/{ABI}, as can be seen here (in this case, the ABI is arm64-v8a). So far, so good.
However, when I go to load the OpenCV library (which calls upon libc++_shared), I receive the following error:
W/System.err: java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found: needed by {omitted}/base.apk/lib/arm64-v8a/libopencv_java4.so in namespace classloader-namespace
I've attempted building the APK without including libstdc++_shared, under the pretense that Gradle claims to be able to include the C++ shared libraries automatically. This failed with the same error message as above, with no libc++_shared.so in the APK.
I've also attempted to create a dummy native C++ file, in order to get libc++_shared added via CMake during the Gradle build process. This also failed with the above error. I did not think to check the APK during this attempt.
In a previous iteration of this project (before I discovered the bug in the OpenCV Android implementation), I was able to compile and run without issue by including the .aar file as an external library.
Between each of the above attempts, I've performed a full Gradle cache invalidation to make sure I'm not keeping broken files from previous tries.
I'm completely flummoxed. Did I break something when I unpacked the .aar to fix the bug? How can I get Android to acknowledge what's inside the APK?
I had the same issue after upgrading the Gradle plugin. the nonsense disappear after I also upgrade the Gradle build tool.
dependencies {
//after upgrad from 3.5.1 the link error disappear
classpath "com.android.tools.build:gradle:3.5.4"
...
}

Java.Lang.UnsatisfiedLinkError: 'No implementation found for

I'm trying to use OpenCv 4 on Xamarin.Android by Java Binding Template. These are the steps that i've done:
0) I've compiled opencv binaries through cmake and mingw64 to get .jar and .a
I've put the .jar and the static libraries (.a) in Jars forlder of Xamarin Java Binding Template and i've compiled the template.
1.a) .jar Build Action is EmbeddedJar
1.b) libs Build Action is EmbeddedNativeLibrary
I've added a reference to that template in my Xamarin.Android project: the opencv methods were recognized correctly!
But, when i try to execute:
Mat Source = Imgcodecs.Imread(ImagePath, Imgcodecs.ImreadGrayscale);
i get an error:
Java.Lang.UnsatisfiedLinkError: 'No implementation found for long org.opencv.imgcodecs.Imgcodecs.imread_0(java.lang.String, int) (tried Java_org_opencv_imgcodecs_Imgcodecs_imread_10 and Java_org_opencv_imgcodecs_Imgcodecs_imread_10__Ljava_lang_String_2I)'
I think that there could be a missmatch of method name, maybe due to a wrong java parsing.
I've also tried to use shared libries (.so) by loading them through JavaSystem.LoadLibrary("LibraryNameWithoutInitialLib"), but i have the same error :/
Do you know why?
You cannot link static libraries with Xamarin.Android as the Xamarin/Mono NDK-based runtime is a static main entry executable and does not dynamically get built per project. If you do not need to use a .jar/.aar high-level wrapper, then you will need to use runtime shared libraries and define DllImportAttribute entries for the exported functions that you need to call.
Xamarin.Android supports the use of native libraries via the standard PInvoke mechanism.
Using Native Libraries
Use C/C++ libraries with Xamarin
Note: There are numerous OpenCV C# wrappers / DllImport files in open source ( i.e. a github search away 😁)
Note: If you are using a 3rd-party .jar/.aar , make sure that they are using OpenCV shared libraries and thus not requiring a gradle script to link them into an NDK-based Android app.

build opencv 3.0 for android with extra modules

I built the OpenCV 3.0 beta with extra modules. The build was without java libraries and that was the only way that I was able to build the OpenCV 3.0 beta without getting errors and there is no problem because I only need the native part.
The problem that I have is when I try to implement the text module I get an error that the library can´t be resolved. The line to implement the library is this:
#include <opencv2/text.hpp>
So my question is: What can I do to resolve this problem? Do I have to move some include files to some location?
Note that OCRTesseract class (inside of opencv2/text.hpp) provides an interface with the tesseract-ocr API (v3.02.02) in C++ and it is compiled only when tesseract-ocr is correctly installed.
First of all you should compile tesseract (and its dependencies) to Android.

Android linking jar JRE1.7 NoClassDefFoundError

I have an android 2.1 app and a separate JRE jar library with core methods of the app, compatible with other web apps. The core uses sqlite4java.
When I specify to compile the library with JRE1.6 there is no problem accessing the classes of the jar library from the app, but as the library uses sqlite4java it doesn't work because it needs to be JRE1.7 (Complains with "Couldn't load sqlite4java-android-armv6l")
When I specify to the library to compile in JRE1.7, it compiles ok, runs ok the app but when the app tries to access to any class in the library it fails with NoClassDefFoundError. During compiling I get the following error with each class of the library:
[2012-05-31 13:00:21 - AndroidApp-0.2] Dx bad class file magic (cafebabe) or version (0033.0000)
...while parsing com/androidapp/core/RES.class
...while processing com/tonads/androidapp/RES.class
I tried to put in classpath the directory where my library is but no result. Also in manifest included every .jar and .class file that the library uses.
After exhaustive googling, couldn't find any solution. Any ideas would be appreciated.
but as the library uses sqlite4java it doesn't work because it needs to be JRE1.7
First, Android does not support Java 7.
Second, since that project has an outstanding issue to confirm Java 7 compatibility, I suspect that your theory regarding Java 7 is incorrect.

How to build Android project with Scala sources in IntelliJ IDEA (Community Edition)?

I am trying to build an Android project with Scala sources in IntelliJ IDEA (Community Edition). I am using Scala sources from Java files: auto-complete works correctly, but build cannot resolve Scala classes in Java: cannot find symbol class MyScalaClass.
(I managed that in Eclipse, but Scala support in Eclipse is poor.)
Any hints or tutorials?
Update: I have added Scala module in addition to Android module. Now, build fails during generating classes.dex with message:
Error: trouble writing output: format == null
I found that it is a limitation of Dalvik: issue 7147.
How can I strip unused methods/classes in IDEA without using Ant script? (I was using ProGuard in Ant script, but I was unable make IDEA to parse Scala error messages from Ant results.)
The scala/java system requires that you:
- Build scala first, giving the compiler both scala and java files.
- Build the java sources with the compiled scala classes in the path for javac.
The second is probably what you lack. Refer to how to add directory to classpath in an application run profile in intellij idea? to add a classpath to Intellij (I suppose you did it for the android runtime, so you probably already know it).
You should use ProGuard to strip down unused classes.
Follow the instructions here (Specifically take a look at the progaurd-project.txt:
https://github.com/yareally/android-scala-intellij-no-sbt-plugin

Categories

Resources