Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call non-static methods on custom Unity Android Plugins

I'm trying to understand how to build my own Android plugins for Android.

I achieve to call static methods on my Java class (the one created on AndroidStudio), but I really CAN'T call non-static methods.

I check those links:

  • https://answers.unity.com/questions/884503/cant-call-non-static-method-in-androidjavaclass.html

  • http://leoncvlt.com/blog/a-primer-on-android-plugin-development-in-unity/

  • https://answers.unity.com/questions/1327186/how-to-get-intent-data-for-unity-to-use.html

  • how to call Non-static method from Unity Android Plugin?

But none works.

I'm trying to get the call from a button from Unity like:

AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = UnityPlayer.Get<AndroidJavaObject>("currentActivity");
currentActivity.Call("SayHi");

And my activity on Android looks like:

public class MainActivity extends UnityPlayerActivity {
    private static final String TAG = "LibraryTest";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "Created!");
    }

    public void SayHi()
    {
        Log.d(TAG, "HI_");
    }
}

My ADB is throwing this message: enter image description here

I've also tried calling instead of UnityPlayer call it like:

AndroidJavaClass pluginClass = new AndroidJavaClass("com.example.eric.librarytest.MainActivity");

EDIT: This is my AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.eric.librarytest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="24"
        android:targetSdkVersion="28" />

    <application android:label="@string/app_name" >
        <activity
            android:name="com.example.eric.librarytest.MainActivity"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

But doesn't work either for non-static methods, it works only for static methods if I do pluginClass.CallStatic(""), any idea?

EDIT 2:

  • Taras Leskiv suggest to change UnityPlayer.Get to UnityPlayer.GetStatic, but then i get the follow error:

    error: java.lang.NoSuchMethodError: no non-static method with name='SayHi' signature='()V' in class Ljava.lang.Object;

  • Proguard is not active.
like image 964
Lotan Avatar asked Feb 24 '20 19:02

Lotan


People also ask

How can call non-static method from static method in Android?

static methods and fields belong to classes and non- static methods and fields belong to class instances. To call a non- static method you need an instance of a class, and that's all. Hope this helps. The member functions are being called on a specific instance.

Can we call non-static method?

A static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables.

How do I call a non-static method in C#?

We can call non-static method from static method by creating instance of class belongs to method, eg) main() method is also static method and we can call non-static method from main() method . Even private methods can be called from static methods with class instance.

How do you call a non-static method from a static method in flutter?

The only way to call a non-static method from a static method is you should have an instance of the class containing the non-static method. You have instance of CheckConnection so you can call it.


1 Answers

When you are doing this:

AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = UnityPlayer.Get<AndroidJavaObject>("currentActivity");
currentActivity.Call("SayHi");

It doesn't work because the currentActivity field is static, what you should do is:

AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("SayHi");

Notice the UnityPlayer.GetStatic part

Here is other convenience snippet from one of my plugins:

    static AndroidJavaObject _activity;

    public static AndroidJavaObject Activity
    {
        get
        {
            if (_activity == null)
            {
                var unityPlayer = new AndroidJavaClass(C.ComUnity3DPlayerUnityPlayer);
                _activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            }
            return _activity;
        }
    }
like image 87
Taras Leskiv Avatar answered Oct 06 '22 01:10

Taras Leskiv