- Home /
How do I pass a string array to Java via JNI?
Hello!
I need to pass a string array to Java with the Java Native Interface.
However I receive the following error in Java:
! getMethodID("ReceiveScenes", "(Ljava/lang/String;)V") FAILED!
My UnityCode:
AndroidJavaClass jc;
jc = new AndroidJavaClass("com.lwptest.lwp.arrayTest");
jc.CallStatic("ReceiveArray", arr);
"arr" is an array of string which is filled elsewhere (It's not null when I try to call the function, I checked this).
Java Code
public static void ReceiveArray(String[] theArray) {
Log.i(arrayTest.TAG, "ReceiveArray " + theArray[0]);
}
When I only try to send a single string and change the methods accordingly everything works perfectly and the error is gone.
What am I missing? Why can't I pass an array? Can anybody help?
Answer by pweeks · Dec 16, 2015 at 10:09 PM
i made an array of objects and then assigned the string array to one of the entries
object[] objs = new object[3];
objs[0] = string array;
objs[1] = string;
objs[2] = string;
jo.call("javafunc",objs);
then the java signature just matches the items in each entry in the object array so
public void Javafunc(String[] arg1, String arg2, String arg3)
Yep, it took me several days to figure this out a while back. If you just need a single String[], just create object[] objs = new object[1]; and assign obj[0] to the array. I know that sounds strange, but it causes the Java method declaration to be generated properly whereas passing a String[] makes it think it's only a single String.
For someone who visit this link
Java code
package com.Abhi.test.Unity.persist;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class callUnity
{
public static String[] getStringArray()
{
String[] strs = { "Test1", "Test2" };
return strs; // Or alternatively, return null;
}
}
C# calling
public void ShowData()
{
string javapluginaddress="com.Abhi.test.Unity.persist";
AndroidJavaClass cls = new AndroidJavaClass(javapluginaddress);
AndroidJavaObject obj = cls.CallStatic<AndroidJavaObject>("getStringArray");
if (obj.GetRawObject().ToInt32() != 0)
{
// String[] returned with some data!
string[] result = AndroidJNIHelper.ConvertFromJNIArray<string[]>
(obj.GetRawObject());
foreach (string str in result)
{
Debug.Log("Data from java is "+str);
// Do something with the strings
}
}
else
{
// null String[] returned
}
obj.Dispose();
cls.Dispose();
}
Answer by $$anonymous$$ · Aug 09, 2018 at 03:21 PM
All you need to do is cast the string array to an object, like this:
string[] strings = new string[] { "string1", "string2", ...};
myAndroidJavaObject.Call("myMethod", (object) strings);
Right, the issue is that the Call method takes a "params" array. Therefore when you pass a single array it will treat the array elements as single parameters. The "params" keyword is actually just syntactic sugar. So if you have a method like this:
void $$anonymous$$y$$anonymous$$ethod(params int[] aParams)
{
}
Then you can simply call this method like this:
$$anonymous$$y$$anonymous$$ethod(5, 1, 42, 55);
This is just syntactic suger for this:
$$anonymous$$y$$anonymous$$ethod(new int[]{5, 1, 42, 55});
In the case of the Call method when you cast the strings array to "object" the compiler will treat it as single value and actually does:
myAndroidJavaObject.Call("my$$anonymous$$ethod", new object[] { strings });
Your answer
![](https://koobas.hobune.stream/wayback/20220613174959im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Undeclared transform array 0 Answers
Instantiating on passed parameters? 1 Answer
How to use texture array in shader? 1 Answer
How to Skip Parameter in C# 2 Answers
calling an void from another script and passing paramters... 1 Answer