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:

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:

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>
  • Taras Leskiv from SO suggest me 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.
  • I’ve also tried calling instead of UnityPlayer call it like:
    AndroidJavaClass pluginClass = new AndroidJavaClass("com.example.eric.librarytest.MainActivity");
    But doesn’t work either for non-static methods, it works only for static methods if I do pluginClass.CallStatic(“”).
    Any idea? Thanks for the help!

“E/Unity: AndroidJavaException: java.lang.NoSuchFieldError: no “Ljava/lang/Object;” field “currentActivity” in class “Lcom/unity3d/player/UnityPlayer;” or its superclasses”

Having the same issue in Android release variant with proguard, minifyEnabled true.

My code was correct, the problem was the Project structure.

My Project was like:

Assets
├── Plugins
│ ├── classes.jar

And should be like:

Assets
├── Plugins
│ ├── Android
│ │ ├── classes.jar

Was obvius (not for me) cause the error spam the message that could not find the signature, so the software can’t read those classes.

1 Like

After change

UnityPlayer.Get<AndroidJavaObject>("currentActivity");

to

UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

, and put the arr(same as jar I thind in this discussion) library in /Assets/Plugins/Android/,
still encountered this problem:

Would anyone help me please?

We can’t help you when we don’t see the relevant code. The error suggests that you should have a method with this signature

public int regToWX()
{
    // ...
}

on the java side. If that’s not the case, of course you’re doing something wrong when you try to call the method.

Of course in order for that to work you have to modify your manifest like shown in the OP and as explained in the documentation so that Unity will actually use your activity instead of the default one.

1 Like