I’m fiddling with a card reader SDK where I’m trying to implement it without any plugin wrapper. Just straight AndroidJavaObject and/or AndroidJNI.
This SDK has a method that returns a java.util.ArrayList of type java.lang.String that is a list of usb connected card readers. In my Unity code, I’m invoking the method to get the list like so:
var deviceList = new AndroidJavaObject("java.util.ArrayList");
try {
long result = scard.Call<long>("SCardListReaders", unityContext, deviceList);
Debug.Log("SCardListReaders result: " + result);
}
catch (Exception e) {
Debug.LogError(e);
}
int deviceCount = 0;
try {
deviceCount = deviceList.Call<int>("size");
}
catch (Exception e) {
Debug.LogError(e);
}
Debug.Log("Device count: " + deviceCount);
Debug.Log("Connected devices:");
for (int i = 0; i < deviceCount; i++) {
try {
// var device = deviceList.Call<string>("get", i); // Soft crash here
var device = deviceList.Call<AndroidJavaObject>("get", i); // Soft crash here
Debug.Log(device);
}
catch (Exception e) {
Debug.LogError(e);
}
}
Line 24 is where a soft crash happens and the rest of the script doesn’t function. Not a single exception is caught either.
I’m womdering if something is wrong with the way the ArrayList is being made, but the API call I’m using is putting data into the ArrayList and I can see there is a element count. The issue is when I’m trying to get data. I tried several types that would seem correct.
To test the fuctionality of Java ArrayLists in Unity further, I made one and then tried to add elements to it just from Unity code. I get the same soft crash when invoking the add method like I did with the get method.
public void ArrayListTest () {
NanoDebug.Log("Array List Test derp");
try {
var arraylist = new AndroidJavaObject("java.util.ArrayList");
arraylist.Call("add", 1); // Soft crash here
}
catch (Exception e) {
NanoDebug.LogError(e);
}
NanoDebug.Log("Done");
}
Note that an exception is never caught, and my “done” print statement never executes. The script also becomes unreponsive.
Inspecting how Java ArrayLists work, I realize this invokcation from Unity is never giving the ArrayList a generic type. Shouldn’t be invoked with something like this syntax?
new AndroidJavaObject<string>("java.util.ArrayList")
new AndroidJavaObject("java.util.ArrayList", typeOf(string))
???
I have no idea how this would work. I just know that making an Java ArrayList from Unity code seems to not be giving it a generic type, so wonkyness starts to happen when I use add or get methods.
So I’m asking, is it even possible to work with a Java ArrayList from the Unity side?
