How to pass Java object to C#?

Hey Everyone,

I am attempting to implement some Java native code into my Unity project, and I want to be able to use a function in my Java code that passes back an object based on a class defined within my java code to my C# Unity code. So far I have been unable to do this.

Here is an example of the JAVA side code:

Inventory:

public List<Weapon> weapon;

public List<Weapon> getWeaponFromInventory(Context context)
	{
		activity = (Activity)context;
		activity.runOnUiThread(new Runnable(){
			public void run()
			{
				if(myInstance == null){
					 Log.e("Instance cannot be null");
			          throw new IllegalArgumentException("Instance was null when attempting to getWeaponFromInventory(context)");
				}
				weapon = instance.myInstance.getWeaponFromInventory(activity);
			}	
		});
		return weapon;
	}

I am then calling on this code from my C# in this way:

PlayerWeaponHandler:

private static AndroidJavaObject instance;
        // Create link to java based Inventory
	private static AndroidJavaClass inventoryPlug= new AndroidJavaClass("com.myth.player.Inventory");
	//Create link to Unity Player
	public static AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
	//Get the current activity to use as context
	public static AndroidJavaObject currentAct = jc.GetStatic<AndroidJavaObject>("currentActivity");
	
	public static List<AndroidJavaObject> Weapon;

	static PlayerWeaponHandler(){
		if(Application.platform == RuntimePlatform.Android){
			Debug.Log ("PlayerWeaponHandler- Created");
			instance = inventoryPlug.CallStatic<AndroidJavaObject>("getInstance");
		}
	}

	public static void getWeapon(bool show){
		if(Application.platform == RuntimePlatform.Android){
             //Here I am attempting to call on a Java method that returns a Java Object holding the information about the 
            //players Weapon
			Weapon = instance.Call<List<AndroidJavaObject>>("getWeaponFromInventory", currentAct);
			return;
		}
		return;
	}

The error I get back is:

I/Unity   (17813): JNI: Unknown signature for type 'System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]' (obj = System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]) equal

I/Unity   (17813):  

I/Unity   (17813): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)

I/Unity   (17813): 

I/Unity   (17813): JNI: Unknown signature for type 'System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]' (obj = System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]) equal

I/Unity   (17813):  

I/Unity   (17813): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)

I/Unity   (17813): 

E/Unity   (17813): getMethodID("getWeaponFromInventory", "(Lcom.unity3d.player.UnityPlayerNativeActivity;Z)") FAILED!

D/dalvikvm(17813): GetMethodID: method not found: Lcom/myth/player/Inventory;.getWeaponFromInventory:(Lcom.unity3d.player.UnityPlayerNativeActivity;Z)

I/Unity   (17813): JNI: Unable to find method id for 'getWeaponFromInventory'

I/Unity   (17813):  

I/Unity   (17813): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)

I/Unity   (17813): 

I/Unity   (17813): JNI: Unknown return type 'System.Collections.Generic.List`1[UnityEngine.AndroidJavaObject]'

I/Unity   (17813):  

I/Unity   (17813): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)

Has anyone ever dealt with trying to do this and found a way to make it work?

Would love to know if the Unity community has an answer to this question.

To my knowledge there is no good interoperability between the JVM and the CLR. My suggestion would be to port over your Java code to C#. However, if you truly couldn’t, you try some of the frameworks out there such as jni4net. I think you’re going to suffer big performance penalties from this though. Good luck, you make be stuck porting your code over.

So I was trying to do something similar,

Java Code:

public List<WifiAccessPointsData> GetBestThreeWifiSignalStrength() {
    List<WifiAccessPointsData> bestThreeWifiSignalsData = GetDataFromAccessPoints();
    return bestThreeWifiSignalsData;
}

Below is the C# code:

  public List<AndroidJavaObject> BestThreeSignalData()
    {
        List<AndroidJavaObject> bestThreeAccessPointsInfo = androidInstance.Call<List<AndroidJavaObject>>("GetBestThreeWifiSignalStrength");
        return bestThreeAccessPointsInfo;
    }

I was getting the following error:

Exception: JNI: Unknown signature for type 'System.Collections.Generic.List`1[System.String]' (obj = System.Collections.Generic.List`1[System.String]) instance

Consider the below example:

I am returning a List from a java function in android to C# in Unity

Below is my java code:

 public List<String> GetListOfNames() 
{
        List<String> newListString = new ArrayList<String>();
        newListString.add("C");
        newListString.add("K");
        return  newListString; 
}

Below is the C# code in Unity

public void GetNames()
{ 
AndroidJavaObject newNameList = androidInstance.Call<AndroidJavaObject>("GetListOfNames");
        string newName1 = newNameList.Call<string>("get", 0);
        Debug.Log("new name: " + newName1);
}

Output:

10-15 19:01:02.954 23278-23350/? I/Unity: new name: C

Hope this example will help others.