5
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

AndroidJavaObject activity = unityPlayer.GetStatic("currentActivity");

activity.CallStatic("testMethod");

This is how I call Android static function without argument, and its works perfect. But I have issue when I'm trying to call non-static function with arguments.

I'm trying to call it like that:

activity.Call("testMethod", "testString");

But Android throws exception:

AndroidJavaException: java.lang.NoSuchMethodError: no non-static method "L**myActivity**(Ljava/lang/String;)V"

What I do wrong ? What is the difference between static and non-static call ? I'm using the same activity. Here is my java method:

public void testMethod(String data) { }

UPDATE

Finally was able to run non static method:

activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
                                                                   {
                    activity.Call("testMethod", "testString");
            }));

That odd thats not working without runOnUIThread...Probably I did something wrong.

Jim
  • 8,874
  • 16
  • 68
  • 125

3 Answers3

12

While you might have fixed the issue, I believe the real issue lies elsewhere. I've helped create quite a few plugins for Unity, so I'm just going to put in my thoughts here for you to read and for anyone else who might face the same or similar issues.

Firstly, you've used a UIThread to call your non-static, public method. This is perfectly fine, but only if some UI operation is performed in the native code, such as showing a toast. Otherwise, the issue lies elsewhere.

Usually, if you create a method, it's going to be in a separate class that extends the UnityPlayerActivity. So for an example, let's say you've called it "MyClass.java"

This is probably how your Java Native class looks like

Native Android Code

package com.companyName.productName;

import com.unity3d.player.UnityPlayerActivity;
import com.unity3d.player.UnityPlayer;

public class MyClass extends UnityPlayerActivity {    

    public void testMethod(String data) { 
        Log.i("TAG", "The data was "+data);
    }
}


Now on the Unity side, your C# script should look like this

Unity C# code

AndroidJavaClass myClass = new AndroidJavaClass("com.companyName.productName.MyClass");

myClass.Call("testMethod", new object[] { "testString" } );


Note how I've used the actual Java Class rather than the UnityPlayerActivity (i.e. "com.companyName.productName.MyClass" rather than "com.unity3d.player.UnityPlayer")


Try the code above, it should print "The data was testString" to the logcat.

In your C# code, you've used the currentActivity field to call your method.

Rather, what you want to do is to create an AndroidJavaObject or AndroidJavaClass of your class that extends UnityPlayerActivity ("MyClass.java" in the above example) and call your methods.

JohnTube
  • 1,782
  • 27
  • 46
Venkat at Axiom Studios
  • 2,456
  • 1
  • 15
  • 25
0

It should work. Actually I'm using non-static method calling like yours.

Did you update your jar file under Assets/Plugins/Android?

Kazuki Sakamoto
  • 13,929
  • 2
  • 34
  • 96
  • I did. Finally I was able to run non static method. Check my update how I did it. – Jim Dec 18 '14 at 21:25
0

Declare testMethod(public) you in .jar file?

If you don't declare method, you try make .jar file.

Ethan Choi
  • 2,339
  • 18
  • 28