Jetpack Compose

Runtime Permissions In Jetpack Compose

How to properly handle android runtime permissions in jetpack compose

Oussama Cherfaoui
4 min readMay 24, 2023
A screenshot of the example app with “Permissions handling” as a title

Asking your users for necessary permissions is an important part of developing any Android app. In this article, we will explore how to handle requesting runtime permissions in an Android app using Jetpack Compose. We will create a user-friendly approach to requesting permissions and handling the user interactions as you can see in the video bellow.

An animated gif that demostrate how the example app function.

Add Permissions to the Manifest File

The android manifest file describes the essential information about your app to the Android build tools, the Android operating system, and Google Play. Adding permissions to the manifest file is necessary, this is how the android operating system knows that you will have access to such a permission.

In this example we’re requesting these permissions, that all need to be granted by the user:

  1. ACCESS_COARSE_LOCATION : this permission gives the app access to the approximate location of the device.
  2. READ_CALENDAR: this permission gives the app access to read the user’s calendar events and information.
  3. READ_CONTACTS: this permission gives the app access to read the user’s contacts.
  4. RECORD_AUDIO: this permission gives the app access to use the microphone.

Define the Permissions Enum

In the permissions enum you will define the needed permissions for your app. By defining an enum, you can centralize the definition and the management of the necessary permissions and their associated properties such as the title and description, making it easier to reference them throughout your app.

In this example we’re defining an enum for the four permissions that we added to the manifest file, with these properties:

  1. permission: this is the appropriate permission name from the android package.
  2. title: a title for the requested permission.
  3. description: a brief description for the user about why you need this permission.
  4. permanentlyDeniedDescription: this description is shown when the user permanently denied granting the permission, this means that the permission need to be granted from the app settings.
  5. the function permissionTextProvider(isPermanentDenied: Boolean): will return the appropriate description based on the permission status (denied or permanently denied).
  6. and the last function getNeededPermission(permission: String): will take the permission name as it’s in the android package and return a permission enum, we will see it use later.

Create the Alert Dialog

When the user refuses to grant a permission, the alert dialog will be shown to provide a clear explanation to the user of why the permission is needed and how it will be used.

This alert dialog composable function will take these properties as a parameter:

  1. neededPermission: this is the needed permission enum that we created before.
  2. isPermissionDeclined: this will be true when the user permanently declined the permission and it need to be granted in the app setting.
  3. onDismiss: this lambda function will be called when the user dismiss the dialog.
  4. onOkClick: this will be called when the user click the “ok “ button to grant the permission.
  5. onGoToAppSettingsClick: this will be called when the user click the “go to app settings” to go to the app settings and grant the permission.

Create the Permissions Composable

Now we will create a simple UI of two buttons one for requesting one permission (we’re requesting the microphone as an example), and the other one for requesting multiple permissions.

  1. The PermissionDialog variable is a mutable state list that keeps track of the permissions that have been declined by the user and need to be shown in alert dialog.
  2. The microphonePermissionLauncher and multiplePermissionLauncher variables use the rememberLauncherForActivityResult function to launch permission requests and handle the results. The onResult callback is used to add declined permissions to the PermissionDialog list.
  3. The goToAppSetting function is an extension function that launches the app settings screen to allow the user to grant a declined permission manually.

Call the Permissions Composable in the Main Activity

And finally we will simply call the permissions composable in the main activity.

Conclusion

In conclusion, the code provided in this article demonstrates how to handle requesting runtime permissions in Jetpack Compose, while providing a good user-friendly way to request permissions and handle user interactions. The full code can be found in my GitHub repository bellow.

--

--