Top Related Projects
Android library that simplifies the process of requesting permissions at runtime.
Android runtime permissions powered by RxJava2
Simplify Android M system permissions
Easy check permission library for Android Marshmallow
A declarative API to handle Android runtime permissions.
Quick Overview
PermissionsDispatcher is a code generation library for Android that simplifies the process of requesting runtime permissions. It uses annotations to generate boilerplate code for handling permission requests, making it easier for developers to implement proper permission handling in their Android applications.
Pros
- Reduces boilerplate code for permission handling
- Generates compile-time safe code
- Supports both Kotlin and Java
- Integrates well with Android's permission system
Cons
- Requires annotation processing, which may increase build times
- Limited flexibility for complex permission scenarios
- Learning curve for developers new to annotation-based code generation
- May add unnecessary code for simple permission requests
Code Examples
- Basic permission request:
@RuntimePermissions
class MainActivity : AppCompatActivity() {
@NeedsPermission(Manifest.permission.CAMERA)
fun showCamera() {
// Camera permission granted, open camera
}
@OnPermissionDenied(Manifest.permission.CAMERA)
fun onCameraDenied() {
// Handle permission denied
}
@OnNeverAskAgain(Manifest.permission.CAMERA)
fun onCameraNeverAskAgain() {
// Handle never ask again
}
}
- Multiple permissions request:
@RuntimePermissions
class MainActivity : AppCompatActivity() {
@NeedsPermission(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
fun takePhotoAndSave() {
// Both permissions granted, take photo and save
}
}
- Handling permission rationale:
@RuntimePermissions
class MainActivity : AppCompatActivity() {
@NeedsPermission(Manifest.permission.ACCESS_FINE_LOCATION)
fun getLocation() {
// Location permission granted, get location
}
@OnShowRationale(Manifest.permission.ACCESS_FINE_LOCATION)
fun showRationaleForLocation(request: PermissionRequest) {
AlertDialog.Builder(this)
.setMessage("Location permission is needed for core functionality")
.setPositiveButton("OK") { _, _ -> request.proceed() }
.setNegativeButton("Cancel") { _, _ -> request.cancel() }
.show()
}
}
Getting Started
- Add the dependencies to your
build.gradle
file:
plugins {
id 'kotlin-kapt'
}
dependencies {
implementation 'org.permissionsdispatcher:permissionsdispatcher:4.9.0'
kapt 'org.permissionsdispatcher:permissionsdispatcher-processor:4.9.0'
}
- Annotate your Activity or Fragment with
@RuntimePermissions
:
@RuntimePermissions
class MainActivity : AppCompatActivity() {
// ...
}
- Use
@NeedsPermission
annotation for methods that require permissions:
@NeedsPermission(Manifest.permission.CAMERA)
fun showCamera() {
// Camera permission granted, open camera
}
- Call the generated method in your code:
MainActivityPermissionsDispatcher.showCameraWithPermissionCheck(this)
- Override
onRequestPermissionsResult
and delegate to PermissionsDispatcher:
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
onRequestPermissionsResult(requestCode, grantResults)
}
Competitor Comparisons
Android library that simplifies the process of requesting permissions at runtime.
Pros of Dexter
- Simpler API with a more fluent interface
- Supports checking multiple permissions at once
- Provides built-in rationale handling
Cons of Dexter
- Less actively maintained (last update in 2019)
- Fewer configuration options compared to PermissionsDispatcher
- Limited support for newer Android versions and features
Code Comparison
Dexter:
Dexter.withContext(this)
.withPermission(Manifest.permission.CAMERA)
.withListener(object : PermissionListener {
override fun onPermissionGranted(response: PermissionGrantedResponse) { /* ... */ }
override fun onPermissionDenied(response: PermissionDeniedResponse) { /* ... */ }
override fun onPermissionRationaleShouldBeShown(permission: PermissionRequest, token: PermissionToken) { /* ... */ }
}).check()
PermissionsDispatcher:
@RuntimePermissions
class MainActivity : AppCompatActivity() {
@NeedsPermission(Manifest.permission.CAMERA)
fun showCamera() { /* ... */ }
@OnShowRationale(Manifest.permission.CAMERA)
fun showRationaleForCamera(request: PermissionRequest) { /* ... */ }
@OnPermissionDenied(Manifest.permission.CAMERA)
fun onCameraDenied() { /* ... */ }
}
Both libraries aim to simplify Android runtime permissions, but they differ in approach. Dexter offers a more straightforward API with built-in rationale handling, while PermissionsDispatcher uses annotation processing for a more declarative style. PermissionsDispatcher is more actively maintained and offers more configuration options, making it potentially better suited for complex permission scenarios and long-term projects.
Android runtime permissions powered by RxJava2
Pros of RxPermissions
- Integrates seamlessly with RxJava, allowing for reactive programming patterns
- Lightweight and simple API, requiring less boilerplate code
- Supports chaining multiple permission requests easily
Cons of RxPermissions
- Requires knowledge of RxJava, which may have a steeper learning curve
- Less feature-rich compared to PermissionsDispatcher (e.g., no built-in rationale handling)
- May not be suitable for projects not already using RxJava
Code Comparison
RxPermissions:
RxPermissions(this)
.request(Manifest.permission.CAMERA)
.subscribe { granted ->
if (granted) {
// Permission granted
}
}
PermissionsDispatcher:
@RuntimePermissions
class MainActivity : AppCompatActivity() {
@NeedsPermission(Manifest.permission.CAMERA)
fun showCamera() {
// Camera permission granted
}
}
Summary
RxPermissions is ideal for projects already using RxJava and preferring a reactive approach. It offers a concise API but requires RxJava knowledge. PermissionsDispatcher provides a more feature-rich solution with annotation-based processing, making it easier to handle complex permission scenarios but with more generated code. Choose based on your project's needs and team's expertise.
Simplify Android M system permissions
Pros of EasyPermissions
- Simpler API with less boilerplate code
- Easier integration for basic permission handling scenarios
- Supports both Activity and Fragment
Cons of EasyPermissions
- Less flexibility for complex permission scenarios
- Limited customization options compared to PermissionsDispatcher
- Fewer features for handling edge cases
Code Comparison
EasyPermissions:
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
private fun methodRequiresTwoPermission() {
val perms = arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
if (EasyPermissions.hasPermissions(this, *perms)) {
// Already have permission, do the thing
} else {
// Do not have permissions, request them now
EasyPermissions.requestPermissions(this, getString(R.string.camera_and_location_rationale),
RC_CAMERA_AND_LOCATION, *perms)
}
}
PermissionsDispatcher:
@RuntimePermissions
class MainActivity : AppCompatActivity() {
@NeedsPermission(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
fun showCamera() {
// Camera and Location permission is granted
}
@OnShowRationale(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
fun showRationaleForCamera(request: PermissionRequest) {
// Show rationale and request permission
}
}
Both libraries aim to simplify Android runtime permissions, but PermissionsDispatcher offers more granular control at the cost of increased complexity, while EasyPermissions provides a more straightforward approach for simpler use cases.
Easy check permission library for Android Marshmallow
Pros of TedPermission
- Simpler API and easier to use for basic permission requests
- Supports custom rationale messages for permission requests
- Includes a pre-built UI for permission requests
Cons of TedPermission
- Less flexibility for complex permission scenarios
- Not as actively maintained as PermissionsDispatcher
- Fewer configuration options for fine-tuning permission behavior
Code Comparison
TedPermission:
TedPermission.create()
.setPermissions(Manifest.permission.CAMERA)
.check()
PermissionsDispatcher:
@RuntimePermissions
class MainActivity : AppCompatActivity() {
@NeedsPermission(Manifest.permission.CAMERA)
fun showCamera() {
// Camera-related code
}
}
TedPermission offers a more concise API for simple permission requests, while PermissionsDispatcher uses annotations for a more declarative approach. PermissionsDispatcher generates code at compile-time, which can lead to better performance but requires an additional annotation processing step.
Both libraries aim to simplify Android runtime permissions, but they cater to different use cases. TedPermission is more suitable for quick implementations, while PermissionsDispatcher offers more control and is better suited for complex permission scenarios in larger applications.
A declarative API to handle Android runtime permissions.
Pros of PermissionsDispatcher
- Simplifies runtime permissions handling in Android apps
- Uses annotation processing to generate boilerplate code
- Supports both Java and Kotlin
Cons of PermissionsDispatcher
- Requires additional setup and configuration
- May increase build time due to annotation processing
- Limited flexibility for complex permission scenarios
Code Comparison
PermissionsDispatcher:
@RuntimePermissions
class MainActivity : AppCompatActivity() {
@NeedsPermission(Manifest.permission.CAMERA)
fun showCamera() {
// Camera-related code
}
}
Standard Android approach:
class MainActivity : AppCompatActivity() {
private fun showCamera() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
// Camera-related code
} else {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CAMERA), CAMERA_PERMISSION_CODE)
}
}
}
PermissionsDispatcher simplifies permission handling by generating the necessary code, reducing boilerplate and potential errors. However, it requires additional setup and may increase build time. The standard Android approach offers more flexibility but requires more manual code writing and error handling.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PermissionsDispatcher
- Fully Kotlin/Java support
- Special permissions support
- 100% reflection-free
PermissionsDispatcher provides a simple annotation-based API to handle runtime permissions.
This library lifts the burden that comes with writing a bunch of check statements whether a permission has been granted or not from you, in order to keep your code clean and safe.
Usage
Here's a minimum example, in which you register a MainActivity
which requires Manifest.permission.CAMERA
.
0. Prepare AndroidManifest
Add the following line to AndroidManifest.xml
:
<uses-permission android:name="android.permission.CAMERA" />
1. Attach annotations
PermissionsDispatcher introduces only a few annotations, keeping its general API concise:
NOTE: Annotated methods must not be
private
.
Annotation | Required | Description |
---|---|---|
@RuntimePermissions | â | Register an Activity or Fragment to handle permissions |
@NeedsPermission | â | Annotate a method which performs the action that requires one or more permissions |
@OnShowRationale | Annotate a method which explains why the permissions are needed. It passes in a PermissionRequest object which can be used to continue or abort the current permission request upon user input. If you don't specify any argument for the method compiler will generate process${NeedsPermissionMethodName}ProcessRequest and cancel${NeedsPermissionMethodName}ProcessRequest . You can use those methods in place of PermissionRequest (ex: with DialogFragment ) | |
@OnPermissionDenied | Annotate a method which is invoked if the user doesn't grant the permissions | |
@OnNeverAskAgain | Annotate a method which is invoked if the user chose to have the device "never ask again" about a permission |
@RuntimePermissions
class MainActivity : AppCompatActivity(), View.OnClickListener {
@NeedsPermission(Manifest.permission.CAMERA)
fun showCamera() {
supportFragmentManager.beginTransaction()
.replace(R.id.sample_content_fragment, CameraPreviewFragment.newInstance())
.addToBackStack("camera")
.commitAllowingStateLoss()
}
@OnShowRationale(Manifest.permission.CAMERA)
fun showRationaleForCamera(request: PermissionRequest) {
showRationaleDialog(R.string.permission_camera_rationale, request)
}
@OnPermissionDenied(Manifest.permission.CAMERA)
fun onCameraDenied() {
Toast.makeText(this, R.string.permission_camera_denied, Toast.LENGTH_SHORT).show()
}
@OnNeverAskAgain(Manifest.permission.CAMERA)
fun onCameraNeverAskAgain() {
Toast.makeText(this, R.string.permission_camera_never_askagain, Toast.LENGTH_SHORT).show()
}
}
2. Delegate to generated functions
Now generated functions become much more concise and intuitive than Java version!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById(R.id.button_camera).setOnClickListener {
// NOTE: delegate the permission handling to generated function
showCameraWithPermissionCheck()
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
// NOTE: delegate the permission handling to generated function
onRequestPermissionsResult(requestCode, grantResults)
}
Check out the sample for more details.
Other features/plugins
Installation
NOTE:
- If you're using jCenter we've moved on to MavenCentral, see migration guide.
- 4.x only supports Jetpack. If you still use appcompat 3.x is the way to go.
To add PermissionsDispatcher to your project, include the following in your app module build.gradle
file:
dependencies {
implementation "com.github.permissions-dispatcher:permissionsdispatcher:${latest.version}"
annotationProcessor "com.github.permissions-dispatcher:permissionsdispatcher-processor:${latest.version}"
}
With Kotlin:
apply plugin: 'kotlin-kapt'
dependencies {
implementation "com.github.permissions-dispatcher:permissionsdispatcher:${latest.version}"
kapt "com.github.permissions-dispatcher:permissionsdispatcher-processor:${latest.version}"
}
License
Copyright 2016 Shintaro Katafuchi, Marcel Schnelle, Yoshinori Isogai
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Top Related Projects
Android library that simplifies the process of requesting permissions at runtime.
Android runtime permissions powered by RxJava2
Simplify Android M system permissions
Easy check permission library for Android Marshmallow
A declarative API to handle Android runtime permissions.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot