Jetpack Compose Permissions: A Quick and Easy Guide
RockAndNull
Posted on August 4, 2023
Permissions and Android have quite a long history. At first, you only needed to request permissions in the Android Manifest and they were granted at install time. Then they changed them to be requested on runtime. Then they fine-tuned the permissions and different permissions might be required for different Android versions.
Overall, these were good changes for the benefit of the user from malicious apps but added a burden to the developer. Multiple methods were developed, both from Google and from the community, to deal with the additional burden. But most of these methods became obsolete with the introduction and adoption of Jetpack Compose.
Thankfully, the Accompanist project, which is something like a testbed for Google to introduce and test the new additions to Compose has a super useful helper library that makes requesting permission a breeze.
First, add the dependency for Jetpack Compose Permissions to your project and you are ready to go. Below is a snippet on how to create a common component to wrap around the content that needs specific permissions. If those permissions are not granted, a special UI is displayed that informs the user and has a button to request permissions. As soon as the permissions are granted, the actual content is displayed.
@OptIn(ExperimentalPermissionsApi::class) // 1.
@Composable
fun MissingPermissionsComponent(
content: @Composable () -> Unit, // 2.
) {
var permissions = listOf( // 3.
Manifest.permission.ACCESS_FINE_LOCATION,
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // 4.
permissions = permissions.plus(
listOf(
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT,
),
)
}
val permissionsState = rememberMultiplePermissionsState( // 5.
permissions = permissions,
)
if (permissionsState.allPermissionsGranted) { // 6.
content()
} else {
Button(
onClick = {
permissionsState.launchMultiplePermissionRequest() // 7.
}
) {
Text(text = "Request permissions")
}
}
}
- You need to explicitly add this annotation to declare that you understand that this API is a work in progress and might change in the future. Hopefully, this won't require too many changes in its final form, but be aware that you might have to change it a bit.
- This is your Compose
@Component
that will be displayed when the permissions are granted. - The list of permissions to request. Note that you can request multiple permissions and they will be requested from the user one by one.
- Since the permissions were fine-tuned in the recent versions of Android, you might need to request different permissions for different versions of Android. Just construct the appropriate list of permissions.
- This is provided by the Jetpack Compose Permissions library and handles everything for you. From keeping the state on whether permissions were requested, to requesting permissions.
- This provides whether the declared permissions are granted or not, to display the appropriate
@Composable
. - This calls the permission-requesting logic. Note that the library will handle the permissions granted case for you, and will trigger a recomposition to display the appropriate
@Composable
.
Hopefully, this was a short and to-the-point intro on how to quickly handle permissions in Compose.
Happy coding!
Posted on August 4, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.