Calling static jar function from Unity3D

2019-04-02 05:14发布

问题:

I made and compiled a Android Library, containing a simple class and a simple static function:

package moo;

public class MyTestClass {

    public static String Foo(){
        return "Foo from Moo";
    }

}

I placed the .jar in my Assets/Plugins/Android Folder. Then In Unity:

    void OnGUI () {
        string somestring = "foooooooooooOOooo";

        AndroidJavaClass testClass = new AndroidJavaClass("moo.MyTestClass");
        somestring = testClass.CallStatic<string>("Foo");

        GUI.Label (new Rect (20, 20, 100, 20), somestring);
    }

And I get an error: JNI: Unable to find method id for 'Foo' (static) UnityEngine.AndroidJavaObject:CallStatic(String, Object[])

Am I missing something to call my static method? Thanks!

回答1:

there are 2 problem as far as I can see:

  1. you have to put your jar package to Assets/Plugins/Android/bin;
  2. you will always get this error on your windows/mac editor, you have to run this on your android device;