10

I want to some permissions only for older devices for compatibility. So I did some research and found this here:

android:maxSdkVersion
The highest API level at which this permission should be granted to your app. Setting this attribute is useful if the permission your app requires is no longer needed beginning at a certain API level.

For example, beginning with Android 4.4 (API level 19), it's no longer necessary for your app to request the WRITE_EXTERNAL_STORAGE permission when your app wants to write to its own application-specific directories on external storage (the directories provided by getExternalFilesDir()). However, the permission is required for API level 18 and lower. So you can declare that this permission is needed only up to API level 18 with a declaration such as this:

<uses-permission
     android:name="android.permission.WRITE_EXTERNAL_STORAGE"
     android:maxSdkVersion="18" />

I tried to implemented that, but it did not work. So I build up this simple test:

<uses-permission
    android:name="android.permission.WRITE_SMS"
    android:maxSdkVersion="3" />

As far I understand this permission should been only required for devices with the API levels 1-3, but I get this permission request even on a API 18 device. Did I something wrong or is this feature broken?

rekire
  • 47,260
  • 30
  • 167
  • 264
  • 2
    Add permission to manifest. No worry about the API level. If in newer version that permission doesn't require, that will be ignored. – Pankaj Kumar Dec 02 '13 at 12:20
  • android:maxSdkVersion="3" , Does this permission is requested in your device? – Viswanath Lekshmanan Dec 02 '13 at 12:22
  • And the code given into docs is for `WRITE_EXTERNAL_STORAGE` not for 'WRITE_SMS'. – Pankaj Kumar Dec 02 '13 at 12:23
  • @PankajKumar Okay my code does not use it but the user may think that I do evil things with it, so I really want only to use a minimal set of permissions. Well yes it is it is just an example, it should work for other permissions too isn't it? – rekire Dec 02 '13 at 12:24
  • @Arju I target API 7-19, but this does not really matter. I just want that the permission is not required in new devices. – rekire Dec 02 '13 at 12:26
  • Yes that should work (as they said into doc). But not sure that it works with all permissions or for only those permissions who desn't need to be added into newer API. – Pankaj Kumar Dec 02 '13 at 12:26
  • @PankajKumar that is really an interesting point. If you find a reference of that I would be happy to accept that as answer. – rekire Dec 02 '13 at 12:27
  • Ok. Will try to find the solution :). If you will find, please add as an answer :) – Pankaj Kumar Dec 02 '13 at 12:31
  • @PankajKumar I checked it with a API19 device this attibute seems to be a API 19 feature. – rekire Dec 02 '13 at 12:32
  • ohh.. might be the reason it was not doing any thing on lower versions – Pankaj Kumar Dec 02 '13 at 12:34
  • 1
    [Reference](https://android.googlesource.com/platform/frameworks/base/+/fb0676a902dd1195e3e35790d137bc1b90e390ba). Looks like Android 4.4 was still not named as KK at that time :) – ozbek Dec 02 '13 at 13:28
  • @shoerat we have a winner ;) I would accept that as answer. – rekire Dec 02 '13 at 13:31

2 Answers2

9

maxSdkVersion attribute was added in API level 19 (docs should have mentioned that). Ref.:

Introduce maxSdkVersion for <uses-permission>

This way an application can automatically sunset its permission requests when running on later versions of the OS where those permissions are no longer relevant, but may be alarming to the user. A canonical example is WRITE_EXTERNAL_STORAGE, which as of KLP becomes unnecessary for an app to use the external storage volume solely for its own large-data needs, without the need for actual file-system sharing among multiple apps.

Bug 9761041

Change-Id: I60130af3a108fe4a750c356038a1c8cb897e9c8b (missing, can be a Google internal change)

Looks like Android 4.4 was still not named as KK at that time :)

ozbek
  • 20,955
  • 5
  • 61
  • 84
  • 2
    It seems there is no `minSdkVersion` .. strange. For example `android.permission.READ_EXTERNAL_STORAGE` is only required since 4.4 to read files, e.g. the 'open with' data filter scheme. – Barry Staes May 08 '14 at 12:04
  • 1
    Please be aware that while maxSdkVersion works fine within Android OS, Play Store doesn't recognize it at all and will keep reporting all permissions marked with this flag as new! It will also request user confirmation of this "new" permissions on every subsequent updates containing that flag! This has been reported to Google 2 years ago, no fix to date! – 3c71 Oct 09 '15 at 06:03
  • @3c71, thanks. Could you please post the link to that bug report? Maybe someone would want to star/up-vote it to help accelerate the fix. – ozbek Oct 09 '15 at 06:07
  • There are no bug tracking for Play Store, nevertheless I reported it, but as you will see, Android dev team doesn't care much about it! There are actually 3 reports: https://code.google.com/p/android/issues/detail?id=63895, https://code.google.com/p/android/issues/detail?id=183899 and https://code.google.com/p/android/issues/detail?id=63898 – 3c71 Oct 10 '15 at 07:15
  • I posted this issue on Android M preview issue tracker, faking an OS issue so I could explain the Play Store bug, and it was acknowledge and later spammed lol: https://code.google.com/p/android-developer-preview/issues/detail?id=3010 – 3c71 Oct 10 '15 at 07:22
  • Also contacted Ellie Powers on G+ as mentioned in issue 183899, she redirected me to Dev Console help center. If you want to use this flag, I invite you to contact the Google Play team as well. You can find the answer I received from Google Play support team here, nearly a month ago: http://www.3c71.com/android/?q=node/2618. Play Store app updates a couple of times since, but no fix! – 3c71 Oct 10 '15 at 07:25
  • Thanks @3c71. Looks like [issue 63895](https://code.google.com/p/android/issues/detail?id=63895) is the most suitable one to star. – ozbek Oct 10 '15 at 07:28
  • 1
    Yes, sorry for not updating this post. The issue has been resolved in Play Store 6.0.5, available on January 1st 2016 or sooner. I've successfully published apps with this particular flag without any further problems! – 3c71 May 21 '16 at 22:15
0

On my HTC with Android 5.0.2 external storaged failed when having the permissions and restrictions: android.permission.WRITE_EXTERNAL_STORAGE and maxSdkVersion = 18. So I needed to change maxSdkVersion to 22.

kotlinski
  • 331
  • 2
  • 6
  • I have this issue too on this exact device and version , but for some reason, accessing the external storage via a POC doesn't fail. It must be something in the app itself that causes it, but I can't find what. Can you please try this app on your device: https://code.google.com/p/android/issues/detail?id=231869 – android developer Jan 10 '17 at 08:23
  • Hi, Thank you and sorry for late answer. But I don't have the time right now :-/ – kotlinski Jan 19 '17 at 09:20
  • Can you please consider starring the reported issue? – android developer Jan 19 '17 at 09:26
  • Hehe, I can star it... But I'm not even sure what I'm starring.. Not really having the time to remeber or figureing out what my main concern was... :-P – kotlinski Jan 19 '17 at 09:46
  • It's about this issue you've found. I got the exact issue on the same device. – android developer Jan 19 '17 at 11:07