How to use AndroidJavaClass.Call() to pass a data object

Hi, All,

I’ve not had much experience to play around with the JNI bridge. Now I have a problem that not being able to pass a customized Class to a Java method. What I need to achieve is basically pass the info from C# to the method in the Android Java Class. Thank you.

private AndroidJavaClass ajc;
private AndroidJavaObject ajo;
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaClass videoObj = new AndroidJavaClass("com.example.sdk.VidoSchema);

ajo = jc.GetStatic<AndroidJavaObject>("currentActivity"); 
ajc = new AndroidJavaClass("com.example.sdk.ExampleSDK");

How to pass the VidoSchema object to the method in java file?

ajc.CallStatic("lanuchSDKMethod", ajo, SERIAL,videoObj);

Also, how to pass the arraylist of this VidoSchema class?

ArrayList<VidoSchema> videoObjArrayList;
ajc.CallStatic("lanuchSDKMethod", ajo, SERIAL,videoObjArrayList);

In java ExampleSDK file, I have methods:

public static void lanuchSDKMethod(Activity activity, String serialKey, VidoSchema cArrayList) {
      ...
      ...
    }

    
public static void lanuchSDKMethod(Activity activity, String serialKey, ArrayList<VidoSchema> cArrayList) {
      ...
      ...
    }




 **This is the data structure file:** 
    
    public class VidoSchema implements Parcelable {

        public boolean isEntryVideo;
        public String branchId;
        public String video_location;
        public String xml_location;
        public String subtitle_location;
        public String assets_folder;

        public VidoSchema(Parcel in) {
            isEntryVideo = in.readByte() != 0x00;
            branchId = in.readString();
            xml_location = in.readString();
            subtitle_location = in.readString();
            assets_folder = in.readString();
        }

        public VidoSchema() {
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeByte((byte) (isEntryVideo ? 0x01 : 0x00));
            dest.writeString(branchId);
            dest.writeString(xml_location);
            dest.writeString(subtitle_location);
            dest.writeString(assets_folder);
        }

        @SuppressWarnings("unused")
        public static final Creator<VidoSchema> CREATOR = new Creator<VidoSchema>() {
            @Override
            public VidoSchema createFromParcel(Parcel in) {
                return new VidoSchema(in);
            }

            @Override
            public VidoSchema[] newArray(int size) {
                return new VidoSchema;
            }
        };
    }

First, you should understand the difference between AndoidJavaObject and AndroidJavaClass:

An AndroidJavaObject is a representation of an object instance in the JVM. When you create an AndroidJavaObject, you are essentially calling the Java constructor of that type (that matches the signature according to the parameters you pass in), then you can call methods and access fields of that object instance.

An AndroidJavaClass is the “type” representation of a JVM class. This is similar to the Type class in C#. Using this, you can query the class’s members (static members) or access “reflection-like” information about the type (not a specific instance of that type!).

With this knowledge, let’s see how to invoke your Java method:

private AndroidJavaClass ajc;
private AndroidJavaObject ajo;

// For static members you can use the AndroidJavaClass
// (you are not accessing any instance of the class)
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
ajo = jc.GetStatic<AndroidJavaObject>("currentActivity");

// Again, this is OK in case the methods you are trying to call are static
var exampleSDK = new AndroidJavaClass("com.example.sdk.ExampleSDK");

// Construct a new VideoSchema object. This is equivalent to calling the empty ctor
// If you need to use another ctor, add the parameters after the type name:
// var videoObj = new AndroidJavaObject("com.example.sdk.VidoSchema", "1", "2");
var videoObj = new AndroidJavaObject("com.example.sdk.VidoSchema");

exampleSDK.CallStatic("lanuchSDKMethod", ajo, SERIAL, videoObj);