# Call static methods in plugin

**URL:** https://discussions.unity.com/t/call-static-methods-in-plugin/598847
**Category:** Mobile
**Tags:** Android
**Created:** [September 24, 2015, 11:08am UTC](https://discussions.unity.com/t/call-static-methods-in-plugin/598847 "2015-09-24T11:08:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ababab5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/ababab5/32/235728_2.png) [@ababab5](https://discussions.unity.com/u/ababab5)
#### Post date: [September 24, 2015, 11:08am UTC](https://discussions.unity.com/t/call-static-methods-in-plugin/598847/1 "2015-09-24T11:08:52Z")

</div>

Hi,

I have an error when I called this methods :

package com.baringsprod.naandroidunity;

import com.unity3d.player.UnityPlayerActivity;

public class UnityBridge extends UnityPlayerActivity  
{  
public static final String STATS\_NAME = “NaStats”;  
public static final String STATS\_MAXLEVEL\_TOK = “NaStatsMaxLevel”;

public enum NaGameType  
{  
kEasy,  
kMedium,  
kExpert  
}

private int getMaxLevelForType\_( NaGameType type )  
{  
return getSharedPreferences(STATS\_NAME, 0).getInt(STATS\_MAXLEVEL\_TOK + type, 0);  
}

public int getMaxLevelEasy()  
{  
return getMaxLevelForType\_( NaGameType.kEasy );  
}

public int getMaxLevelMedium()  
{  
return getMaxLevelForType\_( NaGameType.kMedium );  
}

public int getMaxLevelExpert()  
{  
return getMaxLevelForType\_( NaGameType.kExpert );  
}

I call them with this C# script :

// Get Android context  
AndroidJavaClass unityPlayer = new AndroidJavaClass(“com.unity3d.player.UnityPlayerActivity”);  
AndroidJavaObject activity = unityPlayer.GetStatic(“currentActivity”);  
AndroidJavaObject app = activity.Call(“getApplicationContext”);  
// Install DeployGate, make sure it will not called twice in your app  
deployGate = new AndroidJavaClass(“com.baringsprod.naandroidunity.UnityBridge”);  
activity.Call(“runOnUiThread”, new AndroidJavaRunnable(() =\>  
{  
recupDiff0 = deployGate.CallStatic(“getMaxLevelEasy”, app);  
recupDiff1 = deployGate.CallStatic(“getMaxLevelMedium”, app);  
recupDiff2 = deployGate.CallStatic(“getMaxLevelExpert”, app);

NAUtil.UnlockLevelForDifficulty(recupDiff0,0);  
NAUtil.UnlockLevelForDifficulty(recupDiff1,1);  
NAUtil.UnlockLevelForDifficulty(recupDiff2,2);

Debug.Log("!!! GetPreferenceString success - level returned = " + recupDiff0 + " / " + recupDiff1 + " / " + recupDiff2);  
}));

What am I doing wrong ?

Thanks for the help !

---

<div class="post-metadata">

### Author: ![liortal](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/liortal/32/238558_2.png) [@liortal](https://discussions.unity.com/u/liortal)
#### Post date: [October 1, 2015, 4:17pm UTC](https://discussions.unity.com/t/call-static-methods-in-plugin/598847/2 "2015-10-01T16:17:54Z")

</div>

What is the error your are receiving ? and on which line is it thrown ?

Basically, if you extend Unity’s default activity, then your activity (e.g: UnityBridge _is_ the default activity now).  
You should define it in the AndroidManifest.xml as well, but after you do that, this call will return UnityBridge:

```csharp
AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); // This is UnityBridge, returned as an Activity reference

```

The code you’re later invoking on UnityBridge is using calling it on the **class** and not on the specific **instance.**

Try the following code (i can’t test it, but it should be the right direction):

```csharp
// Get Android context
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayerActivity");
AndroidJavaObject deployGate = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

deployGate.Call("runOnUiThread", new AndroidJavaRunnable(() =>
{
    recupDiff0 = deployGate.Call<int>("getMaxLevelEasy");
    recupDiff1 = deployGate.Call<int>("getMaxLevelMedium");
    recupDiff2 = deployGate.Call<int>("getMaxLevelExpert");

    NAUtil.UnlockLevelForDifficulty(recupDiff0,0);
    NAUtil.UnlockLevelForDifficulty(recupDiff1,1);
    NAUtil.UnlockLevelForDifficulty(recupDiff2,2);

    Debug.Log("!!!!!! GetPreferenceString success - level returned = " + recupDiff0 + " / " + recupDiff1 + " / " + recupDiff2);
}));

```

Let me know if that helped 🙂

---

<div class="post-metadata">

### Author: ![ababab5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/ababab5/32/235728_2.png) [@ababab5](https://discussions.unity.com/u/ababab5)
#### Post date: [October 2, 2015, 10:04am UTC](https://discussions.unity.com/t/call-static-methods-in-plugin/598847/3 "2015-10-02T10:04:25Z")

</div>

Hi,

First of all :thank you very much for your help.

But I still have an error :

10-02 11:58:31.945: W/Unity(15034): !!! EXCEPTION PlayerPrefsAndroid : UnityEngine.AndroidJavaException: java.lang.NoSuchFieldError: no “Ljava/lang/Object;” field “currentActivity” in class “Lcom/unity3d/player/UnityPlayerActivity;” or its superclasses  
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in :0  
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.AndroidJNISafe.GetStaticFieldID (IntPtr clazz, System.String name, System.String sig) [0x00000] in :0  
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.\_AndroidJNIHelper.GetFieldID (IntPtr jclass, System.String fieldName, System.String signature, Boolean isStatic) [0x00000] in :0  
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.AndroidJNIHelper.GetFieldID (IntPtr javaClass, System.String fieldName, System.String signature, Boolean isStatic) [0x00000] in :0  
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.\_AndroidJNIHelper.GetFieldID[AndroidJavaObject] (IntPtr jclass, System.String fieldName, Boolean isStatic) [0x00000] in :0  
10-02 11:58:31.945: W/Unity(15034): at UnityEngine.AndroidJNIHelper.GetFieldID[AndroidJavaObject] (IntPtr jclass, Syste

My manifest :

\<?xmlversion="1.0"encoding="utf-8"?\>

\<manifestxmlns:android=“[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)”  
package=“com.baringsprod.naandroidunity”  
android:versionCode=“1”  
android:versionName=“1.0”\>

And the code I used :

try{  
intrecupDiff0=0;  
intrecupDiff1=0;  
intrecupDiff2=0;

//GetAndroid context  
AndroidJavaClassunityPlayer=newAndroidJavaClass(“com.unity3d.player.UnityPlayerActivity”);  
AndroidJavaObjectdeployGate=unityPlayer.GetStatic(“currentActivity”);

deployGate.Call(“runOnUiThread”,newAndroidJavaRunnable(()=\>  
{  
recupDiff0=deployGate.Call(“getMaxLevelEasy”);  
recupDiff1=deployGate.Call(“getMaxLevelMedium”);  
recupDiff2=deployGate.Call(“getMaxLevelExpert”);

NAUtil.UnlockLevelForDifficulty(recupDiff0,0);  
NAUtil.UnlockLevelForDifficulty(recupDiff1,1);  
NAUtil.UnlockLevelForDifficulty(recupDiff2,2);

Debug.Log(“!!!PlayerPrefsAndroidsuccess-levelreturned=”+recupDiff0+“/”+recupDiff1+“/”+recupDiff2);  
}));  
}catch(Exception ee){  
Debug.LogWarning(“!!!EXCEPTIONPlayerPrefsAndroid:”+ee.ToString());  
}

Sorry and thanks again !

---

<div class="post-metadata">

### Author: ![liortal](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/liortal/32/238558_2.png) [@liortal](https://discussions.unity.com/u/liortal)
#### Post date: [October 2, 2015, 1:03pm UTC](https://discussions.unity.com/t/call-static-methods-in-plugin/598847/4 "2015-10-02T13:03:13Z")

</div>

Ok i see 2 things now:

- The currentActivity static field is on com.unity3d.player.UnityPlayer and not on UnityPlayerActivity. Change the code to init the androidjavaclass with that.
- Are you declaring your new activity in the manifest? You should do that in case you don’t.

Let me know if that helped 🙂

---

<div class="post-metadata">

### Author: ![ababab5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/ababab5/32/235728_2.png) [@ababab5](https://discussions.unity.com/u/ababab5)
#### Post date: [October 2, 2015, 1:41pm UTC](https://discussions.unity.com/t/call-static-methods-in-plugin/598847/5 "2015-10-02T13:41:10Z")

</div>

Hi,

I test it asap, thanks !

About 2/ =\> I create a new AndroidManifest :

\<?xmlversion="1.0"encoding="utf-8"?\>

\<manifestxmlns:android=“[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)”  
package=“com.baringsprod.naandroidunity”  
android:versionCode=“1”  
android:versionName=“1.0”\>

Thanks !
