Search code examples
androidandroid-manifestandroid-permissionsscoped-storage

Android Manifest attributes depending on SDK version


Android 11 introduces various privacy changes including scoped storage. This implies that on Android 11 or higher, the app no longer

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

But I still need to maintain the feature on Android 10 or lower. Is it possible to declare Manifest permissions and attributes depending on the SDK version?


Solution

  • See here :

    <uses-permission android:name="string"
        android:maxSdkVersion="integer" />
    

    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" />
    

    So your maxSdkVersion would be 28 (as apps on Android 10/ API level 29 or up don't need to request it). Your question mentions Android 11 but reading the documentation it seems its allowed from Android 10 and up.