1

i have a projet in Unity 3d who use a jave plugin. In this plugin i need to check if user have already enable the permission. But everytime i call checkSelfPermission the app crash and i dont know why.

Java plugin(module)

public class callFromUnity{

    public String checkPermission(Context context)
    {
        if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            return  "is ok";
        }
        else
        {
            return "is not ok";
        }
    }
}

Unity C#

    AndroidJavaClass unityClass;
    AndroidJavaObject unityActivity;
    AndroidJavaObject pluginsInstance;
    AndroidJavaObject context;
    public Text texte;

    void Start()
    {
       enablePlugins("com.mypackage.test.callFromUnity");
       test();
    }


    void enablePlugins(string pluginName)
    {
        unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
       context = unityActivity.Call<AndroidJavaObject>("getApplicationContext");
        pluginsInstance = new AndroidJavaObject(pluginName);

           if (pluginsInstance == null)
           {
           }
           else
           {      
  // this method is hide in the Java plugins because i remove the code for simplify this question
            pluginsInstance.CallStatic("receiveUnityActivity", unityActivity);
            }
     
    }

    void test()
    {
        texte.text = pluginsInstance.Call<string>("checkPermission", context);
    }

in debug i have this log

2023-04-21 01:10:57.881 28263 28303 Error Unity AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/content/ContextCompat;
2023-04-21 01:10:57.881 28263 28303 Error Unity java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/content/ContextCompat;
2023-04-21 01:10:57.881 28263 28303 Error Unity     at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
2023-04-21 01:10:57.881 28263 28303 Error Unity     at com.unity3d.player.UnityPlayer.access$300(Unknown Source:0)
2023-04-21 01:10:57.881 28263 28303 Error Unity     at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:95)
2023-04-21 01:10:57.881 28263 28303 Error Unity     at android.os.Handler.dispatchMessage(Handler.java:106)
2023-04-21 01:10:57.881 28263 28303 Error Unity     at android.os.Looper.loop(Looper.java:219)
2023-04-21 01:10:57.881 28263 28303 Error Unity     at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
2023-04-21 01:10:57.881 28263 28303 Error Unity Caused by: java.lang.ClassNotFoundException: androidx.core.content.ContextCompat
2023-04-21 01:10:57.881 28263 28303 Error Unity     ... 6 more
2023-04-21 01:10:57.881 28263 28303 Error Unity   at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0 
2023-04-21 01:10:57.881 28263 28303 Error Unity   at UnityEngine.AndroidJNISafe.FindClass (System.String name) [0x00000] in <00000000000000000000000000000000>:0 
2023-04-21 01:10:57.881 28263 28303 Error Unity   at UnityEngine.AndroidJavaObject._AndroidJavaObject (System.String className, System.Object[] args) [0x00000] in <00000000000000000000000000000000>

i have try to expand my class to Activity, or ContextCompat or remove the context arguments and remplace by "this". But for now nothing work.

Shawlong
  • 11
  • 3

1 Answers1

0

The error is because your project does not include the androidx library. In fact you don't have to reinvent the wheel, Unity already has the permission APIs.

using UnityEngine.Android;

....

void test()
{
    texte.text = Permission.HasUserAuthorizedPermission(Permission.CoarseLocation)
                 ? "is ok" : "is not ok";
}
shingo
  • 18,436
  • 5
  • 23
  • 42
  • Hi thanks for you answer. But I can't use unity API for the permission because I need to check permission in a foreground service I use when the app is close. I will try to install the androidx library – Shawlong Apr 21 '23 at 05:40