[Java] NoClassDefFoundError occurred when trying to call an Android function from Unity via jar

less than 1 minute read

In the first place I referred to here until I called the jar https://ghoul-life.hatenablog.com/entry/2019/01/26/030622

When I tried to call the sensor related function of the terminal in the called java method, NoClassDefFoundError occurred Apparently, when I try to use android context in a method, I lose track of the class itself.

I found a way to send android context from Unity to java, and it was called successfully. https://www.itread01.com/content/1546405986.html


        AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject activity = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject javaPlayer = new AndroidJavaObject("jp.co.sample.MyClass");
        string isSensorAvalable = javaPlayer.Call<string>("myMethod", activity);
        // Presence or absence of step sensor "true"/"false"


        public String myMethod(Activity mainActivity) {
            Context context = (Context) mainActivity;
            PackageManager packageManager = context.getPackageManager();
            return String.valueOf(packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_COUNTER));
            // Return whether the pedometer is on the device