AndroidJavaProxy & lang.reflect.Array Exception

Dear Unity Community,

I’ve encountered an issue within AndroidJavaProxy logic and since I couldn’t find any solution for it maybe you will be able to support me.

The issue lies within AndroidJavaProxy (naturally used in Android plugin) which doesn’t work if you try to pass an byte array.

If you try to create a sample interface in java:

interface IDataListener {
void onDataReceived(String source, byte[] data);
}

Use it as an AndroidJavaProxy object:

public GroupPlayDataListener() : base("com.your.library.IDataListener") {}

// The issue appears while passing back byte array from android to c#
// Please note that changing byte[] to string will work fine.
// I'm not sure about other array types.
public void onDataReceived(string source,  byte[] data) {
    Debug.Log(source);
     }
}

...
// Here we call an method from java that sets the listener
activity.Call<int>("someMethod", newGroupPlayDataListener());

It will produce the following result:

Basically if you try to pass back byte array to your unity logic it will crash since it won’t be able to find getLength method (it is looking for a non-static getLength(byte[ ]) method, from Android source it seems that it’s a static getLength(Object)).

Does anyone know any solution for this issue?

Thank you!

Sending byte[ ] (byte array) from Unity to Droid works without problem, Droid to Unity 4.5 NOT. CONFIRMED. So create wrapper object around your byte[ ] on Droid side:

package ws.winx.hid;

public class ReadData {

    public byte[] Buffer;
   
    public ReadData(byte[] buffer) {
        Buffer=buffer;
    }
   
   

}


....
listener.onYourFunctionNameInsideProxy(new ReadData(someByteArray));

And in:

 void onYourFunctionNameInsideProxy(AndroidJavaObject jo){

AndroidJavaObject bufferObject = jo.Get<AndroidJavaObject>("Buffer");

       

            byte[] buffer = AndroidJNIHelper.ConvertFromJNIArray<byte[]>(bufferObject.GetRawObject());

}

Why this is not auto? Unity team should answer as most of the transfer anywhere is byte[ ] based.
Also be carefully that Java byte is signed (-128 to 127) and C# byte is (0 to 255) not signed (use sbyte C#)

2 Likes

Hi,
First of all thank you for the response and sorry for my late reply.
For the people who are facing the same issues, the workaround proposed by Winxlex works perfect.
Thank you for the support!

So for Unity versions that are newer than 4.5, wrappers like this ^ are no longer needed? I’m also using a byte array in c# passing that to java by doing AndroidJavaClass(“com.class.name”).CallStatic(“javaFunctionName”, byte);

I think I found my answer, I can use this https://docs.unity3d.com/455/Documentation/ScriptReference/AndroidJNI.ToByteArray.htmlhttps://docs.unity3d.com/455/Documentation/ScriptReference/AndroidJNI.ToByteArray.html

to send my byte array

I am currently facing this issue with AndroidJavaProxy with java String[ ] and java int[ ] to .Net string[ ] and int[ ]. When I run adb logcat I can see it’s an issue with com.unity3d.player.RefelectionHelper that is raising this exception. It doesn’t cause a crash but my AndroidJavaProxy object never gets called for that specific method that is expecting a string[ ] coming from Android’s side.

I also tried to change my method signature from

myMethod(string[ ] param1, int[ ] param2) to myMethod(AndroidJavaObject param1, AndroidJavaObject param2) so that I could use the AndroidJNIHelper and convert it myself. This doesn’t work since the method never gets invoked. Is there a way around this?

Thanks a lot! works like charm

AndroidJavaException: java.lang.NoSuchMethodError: no method with name='getLength' signature

Exception will be fixed in the future release 2018.3