This article was translated from Japanese by Claude Code.
As the title suggests. The main content is information documented in the PermissionsDispatcher GitHub README.
For more details about PermissionsDispatcher, please see below.
Permission-related implementations can get complicated without a library, but with the PermissionsDispatcher library, when executing processes that require permission approval, you can display a request for permission approval if the specified permission is not granted, and specify functions to execute when the request is denied. By adding annotations to the functions you want to execute, you can specify when and which permissions to request.
For details, please refer to the README above.
PermissionsDispatcher KTX#
As mentioned above, PermissionsDispatcher is a very convenient library.
While regular PermissionsDispatcher is sufficient for most permission-related implementations, when I was working on updating to Target SDK 31 at work the other day, I found that the KTX version of the library could be used even more flexibly, so I’ll introduce the use case.
The KTX version README and such are here. The ktx module exists within the PermissionsDispatcher repository.
New Permissions Added in Android 12#
From here, I’ll describe what I wanted to do: “switching the list of permissions to request based on OS version.”
In Android 12, new Bluetooth-related permissions were introduced: BLUETOOTH_SCAN, BLUETOOTH_ADVERTISE, and BLUETOOTH_CONNECT.
For example, if an app has calling functionality, users can use Bluetooth-connected devices to hear the other person’s voice. In Android 12, permission approval is required for this.
On devices running Android OS 12, you need to separately request approval for these permissions (as needed for the app).
Switching Permissions to Request Based on OS Version#
With the non-KTX PermissionsDispatcher library, you can pass a list of permissions you want users to approve as annotation arguments. However, since the annotation argument type is Java’s String[], it’s difficult to separately define and pass different lists for each OS using if statements.
On the other hand, with KTX, as an example, you can request approval for different permission lists based on OS version with code like this:
constructPermissionsRequest(
permissions = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
arrayOf(
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT
...
)
} else {
arrayOf(
...
)
},
onPermissionDenied = { ... },
onNeverAskAgain = { ... },
requiresPermission = { ... }
)Other Benefits#
Slightly off-topic, but permissionsdispatcher-ktx doesn’t use annotations like PermissionsDispatcher does. Therefore, Annotation Processing doesn’t run, which seems to be a build-time friendly design.
Summary#
- Using PermissionsDispatcher KTX allows you to switch permissions to request based on conditions
- PermissionsDispatcher KTX doesn’t require Annotation Processing, which seems to be a build-time friendly design
That’s all!