Exploring Android 13: Downgradable Permission
Android 13 (Tiramisu)’s new APIs and features are ready to be tried out by developers. One of them is downgradable permission: now your app will be able to revoke runtime permissions that were previously granted by the user.
Essentially, we now have 2 new APIs:
- For revoking ONE permission:
- For revoking MULTIPLE permissions:
In this article, we will create a simple application that uses these 2 new APIs. Here’s how it will look like in the end:
Where’s the Source Code?
android-13-samples/downgradable-permission at master · hanmajid/android-13-samples
Sample projects for Android 13 (Developer Preview 2) - android-13-samples/downgradable-permission at master ·…
Step by Step Explanation
Follow the steps provided below to make this application.
1. As of the writing of this article, Android 13 can only be used with the preview release of Android Studio. Therefore, you must go to this page, and download the Canary Build of Android Studio.
2. Once installed, you need to install the Android 13 SDK. You can do this by clicking Tools > SDK Manager. In the SDK Platforms tab, select Android 13. In the SDK Tools tab, select Android SDK Build-Tools 33. Click OK to install the SDK.
3. Once SDK is installed, open the Android Studio. Create new project with Empty Activity option.
4. First off, you need to change the target and compile SDK version of this app. Open
app/build.gradle and you would see something like this:
targetSdk to use the Android 13 SDK like shown in the code snippet below. Don’t forget to click Sync Now after.
5. Next, we need to declare some runtime permissions in our
AndroidManifest.xml file. These permissions will be requested on runtime in our app. In this article, we are going to use
6. Next, we are going to update our layout,
activity_main.xml file. The image below shows how it will look like in the end. Here, we have 4 buttons: one for requesting all permissions (Camera and Call Phone), one for revoking Camera permission, one for revoking Call Phone permission, and one for revoking all permissions simultaneously. There are also TextViews displaying whether the permission is already granted or not by the user.
Copy-paste the code snippet below to create the above layout:
7. And finally, we are going to update our
MainActivity.kt file. You can go ahead and copy-paste the code below. But the most important thing to notice is the usage of
revokeSelfPermissionsOnKill(Collection) methods for revoking runtime permissions that are already granted by the user. Take note that the revoked permissions MUST BE GRANTED FIRST! Otherwise, the methods will throw an Exception.
Also note that the permissions won’t be revoked while the app is still in use, but only when the system decides that it’s safe to do so. In this application, we call
exitApp() so that our app is closed and it’s safe for the system to revoke the permissions immediately.
8. That’s it! Now you can run your app and try revoking the permissions yourself like this:
As always, thanks for reading!
Want to learn more about Android 13?
Check out all of our articles from “Exploring Android 13” series here: