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

I referred to here until I called the jar in the first place 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 has occurred Apparently I lose track of the class itself when trying to use android context inside a method.

I found a way to send android context from Unity to java and the call was successful. https://www.itread01.com/content/1546405986.html

Unity:

        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);

// With or without step sensor "true" / "false"

Java:

        public String myMethod(Activity mainActivity) {
            Context context = (Context) mainActivity;
            PackageManager packageManager = context.getPackageManager();
            return String.valueOf(packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_COUNTER));

//歩数センサーが端末にあるかどうかを返す }

Recommended Posts

NoClassDefFoundError occurred when trying to call an Android function from Unity via jar
An error occurred when executing a function from MyBatis with the OUT parameter set to CURSOR in PostgreSQL.
Points I stumbled upon when creating an Android application [Updated from time to time]
407 error when trying to access an HTTPS site in Java via an authenticated proxy
I was addicted to WSl when trying to build an android application development environment with Vue.js
Periodically send accelerometer values from Android to PC via UDP
[Opens aml] NoClassDefFoundError occurs when migrating from Tomcat to weblogic
Send the accelerometer value from Android to PC via UDP