38

I'm using this to get permission:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(context, Manifest.permission.GET_ACCOUNTS)) {

    } else {
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.GET_ACCOUNTS}, PERMISSIONS_REQUEST_GET_ACCOUNTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

But the pop up dialog for permission asks user for access Contacts!?!?

In pre 6.0 in Play Store with

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

request is named Identity and explains I need it to get device account.

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42
SpyZip
  • 5,511
  • 4
  • 32
  • 53
  • What is your question? – Kuffs Oct 14 '15 at 12:15
  • 2
    Important sidemark: The documentation of [GET_ACCOUNTS](https://developer.android.com/intl/ru/reference/android/Manifest.permission.html#GET_ACCOUNTS) says that you can remove the permission, if you use it only for your own account. – oli Mar 04 '16 at 10:21
  • 1
    you don't need to request the GET_ACCOUNTS permission on 6.0+ to make the parse push to work. It is declared in the Manifest so it can support Android 4.04 and lower. – jiawen May 14 '16 at 16:55

6 Answers6

37

That is because of Permission Groups. Basically, permissions are placed under different groups and all permissions from that group would be granted if one of them is granted.

Eg. Under "Contacts" , there is write/read contacts and get accounts, so when you ask for any of those, the popup asks for Contacts permissions.

Read through: Everything every Android Developer must know about new Android's Runtime Permission


EDIT 1

Just thought i'l add the related(not to get accounts but permissions and groups) Oreo update info:
source: https://developer.android.com/about/versions/oreo/android-8.0-changes.html#rmp

Prior to Android 8.0 (API level 26), if an app requested a permission at runtime and the permission was granted, the system also incorrectly granted the app the rest of the permissions that belonged to the same permission group, and that were registered in the manifest.

For apps targeting Android 8.0, this behavior has been corrected. The app is granted only the permissions it has explicitly requested. However, once the user grants a permission to the app, all subsequent requests for permissions in that permission group are automatically granted.


Pararth
  • 8,114
  • 4
  • 34
  • 51
  • write/read storage is NOT under contacts it is under android.permission-group.Storage. Under contacts ther is read/write contacts ofc – Karol Żygłowicz Feb 11 '16 at 09:38
  • Correct... Corrected...not storage.. contacts – Pararth Feb 11 '16 at 09:42
  • 10
    This is the sort of thing that makes me question Google. Contacts is an extremely important permission that protects access to the private info about hundreds of people. GET_ACCOUNTS is almost meaningless in comparison. Now I have to stop using GET_ACCOUNTS in my apps. – Tom Feb 11 '16 at 15:21
  • 2
    Yeah..i think Google/Android got stuck in between not giving all Permissions before installation and not asking too many permissions individually at runtime. Then it came out to be somewhere in the middle – Pararth Feb 11 '16 at 15:28
19

GET_ACCOUNTS was moved into the CONTACTS permission group in Android 6.0. While the API has us provide permissions, the user (for Android 6.0 at least) is prompted for permission groups. Hence, the user will be given the same prompt for GET_ACCOUNTS as the user would get for READ_CONTACTS or WRITE_CONTACTS.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 9
    This is too bad. I've noticed people also complain about this here: https://code.google.com/p/android/issues/detail?id=189766 . I use it in order to add payload data into the in-app-billing mechanism. – android developer Nov 07 '15 at 14:32
  • 6
    Fortunately there is a workaround for those who use the permission only for their own account : https://code.google.com/p/android/issues/detail?id=189766#c8 – tbruyelle Nov 10 '15 at 14:28
  • @tbruyelle , do you know if "their own account" means accounts created by the app requesting the GET_ACCOUNTS permission or if it means the users accounts, which are only accounts owned by the user of the app? – ShawnFeatherly Jul 31 '16 at 19:53
  • This means there is no need to use GET_ACCOUNTS in the manifest anymore and use READ_CONTACTS alone, from API 23, right? Meaning use something like: ` ` , right? – android developer Feb 28 '21 at 08:04
  • @androiddeveloper: Only for a couple of releases. IIRC, they later added the requirement that you call `requestPermsisions()` for the specific permissions, not just of one of the group. The user still grants by the group, but if you fail to request the specific permission, you still do not get it. – CommonsWare Feb 28 '21 at 11:40
  • @CommonsWare Well I tested with what I wrote, and requested only READ_CONTACTS during runtime, and on Android API 30 (Android 11) it still let me get the accounts list. I think it's not needed if you have READ_CONTACTS. – android developer Feb 28 '21 at 12:40
  • @androiddeveloper: Perhaps I am misremembering. – CommonsWare Feb 28 '21 at 12:41
  • @CommonsWare I think it might be related to the fact that various queries on the address book will get you various account-type values anyway. – android developer Feb 28 '21 at 13:29
9

Fortunately this will change in Android N

http://developer.android.com/preview/behavior-changes.html#perm

The GET_ACCOUNTS permission is now deprecated. The system ignores this permission for apps that target Android N.

ibmkhd
  • 799
  • 2
  • 14
  • 25
  • 2
    Can you double-check your link please, because I can't find the cited text there. Hopefully Google haven't changed their minds about deprecating it... – snark Jun 09 '16 at 19:39
  • 1
    Im 100% sure it was there before the new design of https://developer.android.com. – ibmkhd Jun 13 '16 at 11:49
  • 1
    Same here, the text changed! Actually would be nice to know, if this information is still right. – Simon Fakir Oct 19 '16 at 10:33
  • Yes correct.Even I felt same.In my android app my targetSdkVersion is 24 and I dont get any security exception while I use GET_ACCOUNTS – Anshul Agarwal Jan 11 '17 at 06:58
  • Doesn't seem deprecated. It is possible to use it, but only in some cases: https://developer.android.com/about/versions/oreo/android-8.0-changes.html#aaad – android developer Dec 31 '17 at 11:56
2

In Marshmallow all dangerous permissions belong to permission groups.

The permission android.permission.GET_ACCOUNTS belongs to CONTACTS group

You can find more information about dangerous permission and their groups here:
https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
1

I got your question wrong first. On this page http://developer.android.com/guide/topics/security/permissions.html#perm-groups, your can see that GET_ACCOUNTS refers to the permission group contacts. Because of that your are prompted for contact permission.

Thomas R.
  • 7,988
  • 3
  • 30
  • 39
0

If your using the GET_ACCOUNTS permission to ask the user to select a particular account type on the device(Google in my case), you can use the AccountPicker class which doesn't require any special permissions

Intent intent = AccountPicker.newChooseAccountIntent(null, null,
                    new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE},
                    false, null, null, null, null);
try {
    startActivityForResult(intent, REQUEST_ACCOUNT_PICKER);
} catch (ActivityNotFoundException e) {
    // This device may not have Google Play Services installed.
}

You'll need Google Play services auth in your gradle dependencies

implementation com.google.android.gms:play-services-auth:16.0.1

This avoids the Contacts permission popup for me

Jer
  • 610
  • 5
  • 9