Exploring Android 13: Downgradable Permission

M Farhan Majid
3 min readMar 22, 2022

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:

  1. For revoking ONE permission: revokeSelfPermissionOnKill(String).
  2. For revoking MULTIPLE permissions: revokeSelfPermissionsOnKill(Collection).

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:

In this demonstration. First I granted all permissions and then revoked them one by one. After that, I granted the permissions again and then revoked all them at once.

Where’s the Source Code?

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:

Change the compileSdk and 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 CAMERA and CALL_PHONE permissions.

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.

This is what activity_main.xml file should look like.

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 revokeSelfPermissionOnKill(String) and 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:

In this demonstration. First I granted all permissions and then revoked them one by one. After that, I granted the permissions again and then revoked all them at once.

As always, thanks for reading!

Want to learn more about Android 13?

Check out all of our articles from “Exploring Android 13” series here:

Exploring Android 13 (Tiramisu)

10 stories

--

--