Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GET ACCOUNT on Android 8

Discussion in 'Scripting' started by Michael512, Nov 2, 2017.

  1. Michael512

    Michael512

    Joined:
    Jan 26, 2015
    Posts:
    10
    Hello dear community,

    Since Android 8.0, the permission GET_ACCOUNT is no longer enough to access account of device owner.
    Source : https://developer.android.com/about/versions/oreo/android-8.0-changes.html#aaad
    To proceed, we are now suppose to do this :https://developer.android.com/reference/android/accounts/AccountManager.html#newChooseAccountIntent(android.accounts.Account, java.util.List<android.accounts.Account>, java.lang.String[], java.lang.String, java.lang.String, java.lang.String[], android.os.Bundle)

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

    Code (CSharp):
    1.  
    2.         string accountName = string.Empty;
    3.         AndroidJavaClass jc_unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    4.         AndroidJavaObject jo_Activity = jc_unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    5.         // get android.accounts.AccountManager and call android.accounts.AccountManager.getAccountsByType
    6.         AndroidJavaClass jc_AccountManager = new AndroidJavaClass("android.accounts.AccountManager");
    7.         AndroidJavaObject jo_AccountManager = jc_AccountManager.CallStatic<AndroidJavaObject>("get", jo_Activity);
    8.         AndroidJavaObject jo_Accounts = jo_AccountManager.Call<AndroidJavaObject>("getAccountsByType", "com.google");
    9.      
    10.         // convert java accounts into array
    11.         AndroidJavaObject[] jo_AccountsArr = AndroidJNIHelper.ConvertFromJNIArray<AndroidJavaObject[]>(jo_Accounts.GetRawObject());
    12.         if (jo_AccountsArr.Length > 0)
    13.             accountName = jo_AccountsArr[0].Get<string>("name");
    14.  
    15.         Debug.Log("accountName = " + accountName);
    16.         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 :
    Code (CSharp):
    1.         AndroidJavaClass jc_unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    2.         AndroidJavaObject jo_Activity = jc_unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    3.         AndroidJavaClass jc_AccountManager = new AndroidJavaClass("android.accounts.AccountManager");
    4.         AndroidJavaObject jo_AccountManager = jc_AccountManager.CallStatic<AndroidJavaObject>("new", jo_Activity); //also tried with "get" instead of new
    5.         AndroidJavaObject jo_Accounts = jo_AccountManager.Call<AndroidJavaObject>
    6. // also tried jc_AccountManager.Call(("newChooseAccountIntent", null, null, new String[] { "com.google" }, null, null, null,null);
    7. ("newChooseAccountIntent", null, null, new String[] { "com.google" }, null, null, null,
    8. 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 <filename unknown>:0
    at UnityEngine.AndroidJNISafe.CallStaticObjectMethod (IntPtr clazz, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0
    at UnityEngine.A


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

    Thanks in advance,
    Michael
     
    Last edited: Nov 2, 2017
  2. Michael512

    Michael512

    Joined:
    Jan 26, 2015
    Posts:
    10
    Anyone ?

    Regards,
    Michael
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
  4. Michael512

    Michael512

    Joined:
    Jan 26, 2015
    Posts:
    10
    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.
     
  5. arslan_arshad

    arslan_arshad

    Joined:
    Jan 5, 2018
    Posts:
    1
    Hello, did you find any solution of this problem?