I have a third party library I want to implement into my Unity projects. It’s buitl in Java so I have a JAR file I can throw into my project as a plugin. But I’m not developing for Android. This is purely a Windows project.
To test the waters, I’m trying to conduct the following:
I’m trying to 100% confirm that using the Java Native Interface in Unity doesn’t work on any platform other than Android.
Unity’s API is labeled as being Android specific, but I’m trying to tool around with the API to see if I can have Unity execute non-Android Java code on Windows.
Here’s an example:
try {
/// In Java Code: String str = new String("HI");
var str = new AndroidJavaObject("java.lang.String", new object[]{"HI"});
/// In Java Code: int strLen = str.length();
var strLen = str.Call("length");
Debug.Log(strLen);
/// Outputs 2 on Android.
/// Outputs 0 on Windows.
}
catch (Exception e) {
/// No exception is caught.
Debug.LogError(e);
}
If this absolutely doesn’t work, I’d expect Unity to throw a compile error, editor error, runtime error, or runtime exception. Instead nothing happens.
Unity seems to create my Java String object without problems, but when I try to access it’s properties on Windows, they’re zeroed out.
That means AndroidJavaObject
isn’t invoking the String
constructor as expected or it’s silently failing due to some unknown reason. This unknown reasons is what I want to know, or why the object contructor isn’t being invoked.
UPDATE:
I tried writing some custom Java code to see if just calling a method even returns a value instead of trying to create Java primitives on the Unity side.
I made a Java class and exported it to a jar file and imported that into Unity and ran the following code:
/// JAVA:
package com.vcom3d;
public class DoTheThing {
public int GetTheThing () {
return 5;
}
}
/// C#:
var doTheThing = new AndroidJavaObject("com.vcom3d.DoTheThing");
var num = doTheThing.Call<int>("GetTheThing");
Debug.Log(num); /// Outputs 0
This behavior of any returned data being zeroed or null is very baffling. I can’t tell if this is suppose to work or not because it’s not throwing any error telling me otherwise.