GET ACCOUNT on Android 8

Hello dear community,

Since Android 8.0, the permission GET_ACCOUNT is no longer enough to access account of device owner.
Source : Android 8.0 Behavior Changes  |  Android Developers
To proceed, we are now suppose to do this :[AccountManager  |  Android Developers(android.accounts.Account, java.util.List<android.accounts.Account>, java.lang.String, java.lang.String, java.lang.String, java.lang.String, android.os.Bundle)](AccountManager  |  Android Developers(android.accounts.Account,%20java.util.List%3Candroid.accounts.Account%3E,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20android.os.Bundle))

Before 8.0, I used to do this, which worked perfectly (as long as the users grants the permission of course) :

        string accountName = string.Empty;
        AndroidJavaClass jc_unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo_Activity = jc_unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        // get android.accounts.AccountManager and call android.accounts.AccountManager.getAccountsByType
        AndroidJavaClass jc_AccountManager = new AndroidJavaClass("android.accounts.AccountManager");
        AndroidJavaObject jo_AccountManager = jc_AccountManager.CallStatic<AndroidJavaObject>("get", jo_Activity);
        AndroidJavaObject jo_Accounts = jo_AccountManager.Call<AndroidJavaObject>("getAccountsByType", "com.google");
      
        // convert java accounts into array
        AndroidJavaObject[] jo_AccountsArr = AndroidJNIHelper.ConvertFromJNIArray<AndroidJavaObject[]>(jo_Accounts.GetRawObject());
        if (jo_AccountsArr.Length > 0)
            accountName = jo_AccountsArr[0].Get<string>("name");

        Debug.Log("accountName = " + accountName);
        return accountName;

According to the documentation; I’m supposed to start an Intent that will return the list of account in the device and user will chose one, using the function newChooseAccountIntent

I tried this code using the parameter provided in the doc :

        AndroidJavaClass jc_unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo_Activity = jc_unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaClass jc_AccountManager = new AndroidJavaClass("android.accounts.AccountManager");
        AndroidJavaObject jo_AccountManager = jc_AccountManager.CallStatic<AndroidJavaObject>("new", jo_Activity); //also tried with "get" instead of new
        AndroidJavaObject jo_Accounts = jo_AccountManager.Call<AndroidJavaObject>
// also tried jc_AccountManager.Call(("newChooseAccountIntent", null, null, new String[] { "com.google" }, null, null, null,null);
("newChooseAccountIntent", null, null, new String[] { "com.google" }, null, null, null,
null);

→ This result to a Java NoSuchMethodError:

(Filename: ./artifacts/generated/common/runtime/DebugBindings.gen.cpp Line: 51)
11-02 09:53:23.294 32685-32708/com.xx.xxE/Unity: AndroidJavaException: java.lang.NoSuchMethodError: no static method with name=‘new’ signature=‘(Lcom.unity3d.player.UnityPlayerActivity;)Ljava/lang/Object;’ in class Ljava.lang.Object;
java.lang.NoSuchMethodError: no static method with name=‘new’ signature=‘(Lcom.unity3d.player.UnityPlayerActivity;)Ljava/lang/Object;’ in class Ljava.lang.Object;
at com.unity3d.player.ReflectionHelper.getMethodID(Unknown Source:49)
at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
at com.unity3d.player.UnityPlayer.c(Unknown Source:0)
at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source:151)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:164)
at com.unity3d.player.UnityPlayer$c.run(Unknown Source:20)
at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in :0
at UnityEngine.AndroidJNISafe.CallStaticObjectMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[ ] args) [0x00000] in :0
at UnityEngine.A

Help would be greatly appreciated !
PS : After fixing this bug I’ll post the game in a few weeks ! :slight_smile:

Thanks in advance,
Michael

Anyone ?

Regards,
Michael

Unity Engine - Unity Discussions Maybe try the Unity Android forum.

Personally I haven’t used GetAccount. Also don’t have access to an 8.0 device, so haven’t looked into any errors with it.

Finally I used a dirty workaround.
I’m not using GET_ACCOUNT anymore, user types email and password manually.
I think the function I need for Android 8 is not available in Unity at the moment, or I’m supposed to use google authent.

Hello, did you find any solution of this problem?