Member-only story

How to Request Android Runtime Permissions using Jetpack Compose?

A simple app example and proper way to request Android runtime permission workflow using Accompanist Permissions library for Jetpack Compose

--

There are 2 types of permissions:

  • Install-time permissions
  • Runtime permissions

An example of the most common install-time permissions is internet permissions. The Android OS system automatically grants your app the permission when the app is installed. You just need to declare the permission in the AndroidManifest.xml.

Unlike runtime permissions, in addition to declaring the permissions in the AndroidManifest.xml, you also need to manually request the permissions in your app. There are some gotchas on requesting runtime permissions, which will be discussed later.

Why Accompanist Permissions?

After playing around with different Android permission implementations in Jetpack Compose, I prefer to use Accompanist permissions library over rememberLauncherForActivityResult() for the following reasons:

  • Permissions state checking is slightly easier using rememberPermissionState() or rememberMultiplePermissionsState() instead of waiting for callback results from the activity result launcher.
  • Accessing to shouldShowRequestPermissionRationale() in composable function is not that straight forward. You probably want to override the shouldShowRequestPermissionRationale() in Activity and do something like this, and figure out how this can be accessed from your composable function.
  class MainActivity : ComponentActivity() {
/*...*/
override fun shouldShowRequestPermissionRationale(
permission: String) : Boolean
{
return if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
super.shouldShowRequestPermissionRationale(permission)
} else {
false
}
}
}

In Accompanist permission, you can just access the PermissionStatus.shouldShowRationale variable from PermissionState.

Request Runtime Permission Gotchas

--

--

No responses yet