Unable to retrieve return values from android library (aar file) to c# in unity 5.3.4p3 (string,int,bool) using AndroidJavaClass.Call()

Hello I’m trying it include an android library in my unity project. My android code is as follows:

package com.androidtester;

public class ATests {
    public String teststr() {
        return "testing...";
    }
    public boolean testbool() {
        return true;
    }
    public int testint() {
        return 123;
    }
}

My c# code is as follows:

public class GameControl : MonoBehaviour {
	void Start ()
	{
		try {
			AndroidJavaClass atests = new AndroidJavaClass ("com.androidtester.ATests");
			Debug.Log ("Test Str:" + atests.Call<string> ("teststr"));
			Debug.Log ("Test Int:" + atests.Call<int> ("testint"));
			Debug.Log ("Test Bool:" + atests.Call<bool> ("testbool"));
		} catch (Exception e) {
			Debug.Log ("ATests Error..." + e.ToString ());
		}
	}
}

This is within a blank unity 2D project with an empty game object containing GameControl. My folder structure is as so:

When I run my unity application on an android device I see this in adb’s logcat:

I/Unity   (29910): GameControl:Start() (at ./Assets/GameControl.cs:10)
I/Unity   (29910):  
I/Unity   (29910): (Filename: ./Assets/GameControl.cs Line: 10)
I/Unity   (29910): 
I/Unity   (29910): Test Str:
I/Unity   (29910): UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
I/Unity   (29910): UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
I/Unity   (29910): UnityEngine.Logger:Log(LogType, Object)
I/Unity   (29910): UnityEngine.Debug:Log(Object)
I/Unity   (29910): GameControl:Start() (at ./Assets/GameControl.cs:13)
I/Unity   (29910):  
I/Unity   (29910): (Filename: ./Assets/GameControl.cs Line: 13)
I/Unity   (29910): 
I/Unity   (29910): Test Int:0
I/Unity   (29910): UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
I/Unity   (29910): UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
I/Unity   (29910): UnityEngine.Logger:Log(LogType, Object)
I/Unity   (29910): UnityEngine.Debug:Log(Object)
I/Unity   (29910): GameControl:Start() (at ./Assets/GameControl.cs:14)
I/Unity   (29910):  
I/Unity   (29910): (Filename: ./Assets/GameControl.cs Line: 14)
I/Unity   (29910): 
I/Unity   (29910): Test Bool:False
I/Unity   (29910): UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
I/Unity   (29910): UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
I/Unity   (29910): UnityEngine.Logger:Log(LogType, Object)
I/Unity   (29910): UnityEngine.Debug:Log(Object)
I/Unity   (29910): GameControl:Start() (at ./Assets/GameControl.cs:15)
I/Unity   (29910):  
I/Unity   (29910): (Filename: ./Assets/GameControl.cs Line: 15)
I/Unity   (29910): 

For some reason the return values from android are all wrong. If I enter a different function name or return type they generate exceptions so I know the signatures are matching up. I should mention that if I switch the android function to ‘public static’ then use 'AndroidJavaClass.CallStatic() in c# everything works fine. It’s the plain AndroidJavaClass.Call() that gives incorrect return values.

In my last plugin I had to Create an instance inside the plugin class.

After my AndroidJavaClass call I used my handle to call the AndroidJavaObject ‘instance’. I use this instance handle, then, to retrieve members.

This is probably why static works. Non-static requires an instance I do believe.

The other thing is that Unity expects a .JAR file. I’m not sure it’ll handle .AAR

Try to use AndroidJavaObject instead of AndroidJavaClass.
As far as I am aware AndroidJavaObject is used for creating instances and AndroidJavaClass is used for classes with static methods.

I call non-static methods fine this way, I also .aar file for my plugin