KB keyboard_arrow_right Android tips keyboard_arrow_right
2 - Do not use maxSdkVersion for permissions
2
Do not use maxSdkVersion for permissions
calendar_today
August 18
access_time
1min. read
remove_red_eye
250
thumb_up
1
comment
0
thumb_up
1
If you've automatically added a permission from Android Studio to your manifest the permission tag might contain maxSdkVersion which might make requestPermissions not work.

In a real word example imagine you've just typed the following code somewhere in your project:

Account[] accounts = AccountManager.get(getContext()).getAccounts();

Sure enough getting such sensitive info requires Manifest.permission.GET_ACCOUNTS and Android Studio will warn you about it and offer you to automatically add the permission to your manifest file.


However, depending on your Android Studio version if you go check the manifest file you might see something like:

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

It seems harmless, but in many case will make it impossible for the requestPermissions function to work properly and you might find that in @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { the grantResults array is empty.


The documentation mentions that whenever a permission is cancelled this grantResults array will be empty but it does not exactly say when such an event might occur.

Comments (0)