Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Call non-static methods on custom Unity Android Plugins

Discussion in 'Scripting' started by L0tan, Feb 29, 2020.

  1. L0tan

    L0tan

    Joined:
    Jul 16, 2017
    Posts:
    75
    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:

    Code (CSharp):
    1. AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    2. AndroidJavaObject currentActivity = UnityPlayer.Get<AndroidJavaObject>("currentActivity");
    3. currentActivity.Call("SayHi");
    And my activity on Android looks like:

    Code (CSharp):
    1. public class MainActivity extends UnityPlayerActivity {
    2.    private static final String TAG = "LibraryTest";
    3.  
    4.    @Override
    5.    protected void onCreate(Bundle savedInstanceState) {
    6.        super.onCreate(savedInstanceState);
    7.        Log.d(TAG, "Created!");
    8.    }
    9.  
    10.    public void SayHi()
    11.    {
    12.        Log.d(TAG, "HI_");
    13.    }
    14. }
    My ADB is throwing this message:


    This is my AndroidManifest.xml

    Code (csharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    3.    package="com.example.eric.librarytest"
    4.    android:versionCode="1"
    5.    android:versionName="1.0" >
    6.  
    7.    <uses-sdk
    8.        android:minSdkVersion="24"
    9.        android:targetSdkVersion="28" />
    10.  
    11.    <application android:label="@string/app_name" >
    12.        <activity
    13.            android:name="com.example.eric.librarytest.MainActivity"
    14.            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
    15.            android:label="@string/app_name" >
    16.            <intent-filter>
    17.                <action android:name="android.intent.action.MAIN" />
    18.  
    19.                <category android:name="android.intent.category.LAUNCHER" />
    20.            </intent-filter>
    21.        </activity>
    22.    </application>
    23. </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!
     
  2. joshuaGnol

    joshuaGnol

    Joined:
    Feb 10, 2020
    Posts:
    4
    "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.
     
  3. L0tan

    L0tan

    Joined:
    Jul 16, 2017
    Posts:
    75
    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.
     
    Kenroukuen likes this.
  4. ABiao

    ABiao

    Joined:
    Jul 25, 2014
    Posts:
    1
    After change
    Code (csharp):
    1. UnityPlayer.Get<AndroidJavaObject>("currentActivity");
    to
    Code (csharp):
    1. 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:
    upload_2020-12-2_17-1-23.png

    Would anyone help me please?
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,573
    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

    Code (CSharp):
    1. public int regToWX()
    2. {
    3.     // ...
    4. }
    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.
     
    hidd33n likes this.