After doing my own bit of testing/research with this, I now understand why it doesn’t work and thought I would share my findings. The problem is that AndroidJavaObject.CallStatic() has for its 3rd parameter “params object[ ] args”. What’s happening with arrays, such as int[ ], is they are being passed in and parsed by the compiler as an object[ ]. So if you enable debug (AnrdoidJNIHelper.debug = true), you will see LogCat say no method exists with a signature of int;int;int, if you for instance had an array of 3 ints. It’s an absurdly easy thing to fix in the end (after I spent all day, I love a good challenge!).
Just create an object[ ], then assign the first element to the array you want to send.
// Create object array that will be passed to the CallStatic method
object[] objParams = new object[1];
string[] myStrings = new string[2];
myStrings[0] = "Hello";
myStrings[1] = "World";
// By assigning item 1 in the object[], you are preventing the compiler from parsing them into the function call, so the Unity code sees the string[] and creates the Java signature properly
objParams[0] = myStrings;
// Just for grins, let's also receive a string[] back from java (String[] in java, string[] in C#) and do a manual conversion on it treating it like an Object
AndroidJavaClass myClassInst = new AndroidJavaClass("com.your.company.your_class");
AndroidJavaObject returnedStringArray = myClassInst.CallStatic<AndroidJavaObject>("getStringsAfterPassingThemIn", objParams);
if (returnedStringArray != null)
{
// Make sure the resulting java object is not null
if (returnedStringArray.GetRawObject().ToInt32() != 0)
{
string[] returnedStrings = AndroidJNIHelper.ConvertFromJNIArray<string[]>(obj.GetRawObject());
}
}
At first, it may seem like doing the following is the best choice to receive a string array (or any array) back, but if null is returned from the Java side, then you get a seg fault because null is not a string[ ], so use the code above to be safe:
// I don't recommend doing this, unless you know you'll never return null from the Java side
AndroidJavaClass myClassInst = new AndroidJavaClass("com.your.company.your_class");
string[] returnedStrings = myClassInst.CallStatic<string[]>("getStringsAfterPassingThemIn", objParams);
If you really want to go crazy and just do everything by hand:
// Create two strings and assign to an object array
IntPtr clazz = AndroidJNI.FindClass("java.lang.String");
IntPtr methId = AndroidJNI.GetMethodID(clazz, "<init>", "(Ljava/lang/String;)V");
IntPtr strId = AndroidJNI.NewStringUTF("Hello");
IntPtr strObj = AndroidJNI.NewObject(clazz, methId, new jvalue[] { new jvalue() { l = strId } });
IntPtr objArray = AndroidJNI.NewObjectArray(2, clazz, strObj);
// Add second string
strId = AndroidJNI.NewStringUTF("World");
strObj = AndroidJNI.NewObject(clazz, methId, new jvalue[] {new jvalue() { l = strId } });
objArray.SetObjectArrayElement(strObj, 1, strId);
// Call your class method
IntPtr clazz2 = AndroidJNI.FindClass("com.your.company.your_class");
IntPtr methId2 = AndroidJNI.GetStaticMethodID(clazz2, "getStringsAfterPassingThemIn", "([Ljava/lang/String;)[Ljava/lang/String;");
AndroidJNI.CallStaticObjectMethod(clazz2, methId2, new jvalue[] { new jvalue() { l = objArray } } );