I’m having trouble with the Google Drive API in my Android app. It worked fine with targetSdkVersion 21
, but after upgrading to targetSdkVersion 23
, I’m getting a strange error.
The app crashes when executing request.execute()
with this exception:
Caused by: java.lang.IllegalArgumentException: the name must not be empty: null
at android.accounts.Account.<init>(Account.java:48)
at com.google.android.gms.auth.zzd.getToken(Unknown Source)
at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
I’m sure I’m passing a valid account name to GoogleAccountCredential
. Here’s a snippet:
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
Utils.getGoogleAccountCredential().setSelectedAccountName(accountName);
}
The weird part is that it works fine on Android 5.1.1 (Nexus 4) but fails on Android 6.0.1 (Nexus 5). I’ve tried both library versions 1.18.0-rc and 1.21.0 with the same result.
Could this be related to Android 6’s runtime permissions? Any ideas on how to fix this?
hey harry47, i had that issue too. its cuz of android 6 permissions stuff. u gotta ask for GET_ACCOUNTS permission at runtime now. check if u got it first, then request if ya dont. like this:
if (checkSelfPermission(GET_ACCOUNTS) != GRANTED) {
requestPermissions(new String{GET_ACCOUNTS}, CODE);
} else {
// do ur google drive stuff here
}
that shud fix it for ya
This issue is likely related to the changes in how Android 6.0+ handles permissions. I’ve dealt with similar problems in my own apps. The key is to implement runtime permission requests for GET_ACCOUNTS.
Before accessing the account information, you should check if the permission is granted. If not, request it from the user. Here’s a quick example:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.GET_ACCOUNTS},
REQUEST_CODE_GET_ACCOUNTS);
} else {
// Permission granted, proceed with account access
}
Remember to handle the permission result in onRequestPermissionsResult(). Only try to access the account after confirming the permission is granted. This approach should resolve your ‘name must not be empty’ error on newer Android versions.
I encountered a similar issue when updating my app to target Android 6.0+. The problem is indeed related to runtime permissions in newer Android versions.
In Android 6.0 and above, you need to explicitly request permission to access accounts. The GET_ACCOUNTS permission is now considered dangerous and requires user consent. To resolve this, you should check at runtime whether the GET_ACCOUNTS permission has been granted. If not, request the permission from the user before attempting to access their Google account information.
Here’s a basic approach:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.GET_ACCOUNTS},
MY_PERMISSIONS_REQUEST_GET_ACCOUNTS);
} else {
// Permission already granted, proceed with Google Drive API operations
}
Make sure to implement onRequestPermissionsResult to handle the user’s response, and only proceed with the API call after confirming that the permission is granted. This should resolve the ‘name must not be empty’ error on Android 6.0+.