Android M - GoogleAccountCredential setSelectedAccount not working - name must not be null

On Android M-Preview, GoogleAccountCredential.setSelectedAccount does not seem to work.

When debugging, I noticed that after calling this method, the selectedAccount and accountName object are still zero.

During debugging, you can see that my accountName variable accountName not empty or null, I call .setSelectedAccountName() , but as you can see in the debug window, the field in GoogleAccountCredential is still null.

Debug Window Code Window debugging debug values

I think this could be due to some permissions? On my manifest, I declare the following permissions:

 <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.USE_CREDENTIALS" /> <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" /> 

I know that you get GET_ACCOUNTS permission for free on M (according to https://developer.android.com/preview/features/runtime-permissions.html#normal ), but the other two permissions are "unknown" for M-Preview. So maybe this is so?

+5
source share
2 answers

android.permission.GET_ACCOUNTS has securityLevel: it is dangerous and is now part of the contact permissions group, which means that you must request it at runtime using the new Activity.requestPermissions()

Only then can you interact with accounts created by other applications on your device.

+4
source

I had the same problem.

Use setSelectedAccount () instead of setSelectedAccountName ()

  mCredential = GoogleAccountCredential.usingOAuth2( getApplicationContext(), Arrays.asList(SCOPES)) .setBackOff(new ExponentialBackOff()); // to set accountName manually instead of prompting user to select it mCredential.setSelectedAccount(new Account(" xyz@gmail.com ", "com.android.example")); 

set up the account you want to set up as email, and you can specify the name of your package as a second parameter.

0
source

All Articles