Top Android Runtime Permissions Libraries 2018


Top Android Runtime Permission Libraries in 2018.

This is a top list tutorial. We want to explore some of the top android runtime permission library examples.

In android devices, permission has to be requested by the programmer before using or accessing some device resources. This is a security feature in that it allows the application to respect user's privacy.

The System will react based on the requested feature. It may grant the permission automatically or might prompt the user to approve the request. To read about permissions and runtime permissions in more detail view here.

Developers have developed several open source libraries that allow us work with runtime permissions easily. Listed below are some of the top libraries in random order.

(a). PermissionsDispatcher

PermissionsDispatcher is a Simple annotation-based API to handle runtime permissions.

Here are some of the features that make PermissionsDispatcher cool:

  • Fully Kotlin support
  • Special Permissions support
  • Xiaomi(API level >= 23) support
  • 100% reflection-free

PermissionsDispatcher 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.

How to Install Permissions Dispatcher

In your app level build.gradle you add the following:

${latest.version} as of the time of writing this was 4.0.0. You can find it here.

dependencies {
  implementation "com.github.hotchemi:permissionsdispatcher:${latest.version}"
  annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:${latest.version}"
}

NOTE: 4.x is still alpha and it only supports Jetpack. If you use appcompat 3.x which is almost stable is the way to go. The above instruction was for version 3.x which was stable at the time of writing.

How to Use PermissionsDispatcher

First in your android manifest add the permission:

<uses-permission android:name="android.permission.CAMERA" />

Then:

@RuntimePermissions
public class MainActivity extends AppCompatActivity {

    @NeedsPermission(Manifest.permission.CAMERA)
    void showCamera() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.sample_content_fragment, CameraPreviewFragment.newInstance())
                .addToBackStack("camera")
                .commitAllowingStateLoss();
    }

    @OnShowRationale(Manifest.permission.CAMERA)
    void showRationaleForCamera(final PermissionRequest request) {
        new AlertDialog.Builder(this)
            .setMessage(R.string.permission_camera_rationale)
            .setPositiveButton(R.string.button_allow, (dialog, button) -> request.proceed())
            .setNegativeButton(R.string.button_deny, (dialog, button) -> request.cancel())
            .show();
    }

    @OnPermissionDenied(Manifest.permission.CAMERA)
    void showDeniedForCamera() {
        Toast.makeText(this, R.string.permission_camera_denied, Toast.LENGTH_SHORT).show();
    }

    @OnNeverAskAgain(Manifest.permission.CAMERA)
    void showNeverAskForCamera() {
        Toast.makeText(this, R.string.permission_camera_neverask, Toast.LENGTH_SHORT).show();
    }
}
PermissionsDispatcher Resources
No. Location Link
1. GitHub HomePage

(b). Nammu

Nammu is a Permission helper for Android M - background check, monitoring and more. Nammu allows you speed up your work with new Runtime Permissions introduced in Android 6.0 Marshmallow. This lib allows you to monitor permissions, check them in background and as well ask for a permission in easy way (callback).

Read full Nammu tutorial here.

Runtime Permissions are like old-loved permissions that were asked during installation but this time they are more dynamic (should be ask only when they are needed) and can be revoked by user at any time.

Nammu allows you to:

  1. Monitor permissions To keep track of access to particular permissions, all you need is init Nammu Nammu.init(Context); (pass Application Context, not Activity Context) and then call permissionCompare(PermissionListener) to compare lists of granted permissions with previous method call.

  2. Easily ask/request for permissions Nammu removes a bit of boiler plate to keep request id, and thus simplify your code inside Activity class. Just callNammu.askForPermission(Activity, PermissionString , PermissionCallback) which offers a nice callback with either success or fail method. To use this the only thing you need to do is add this in your activity:
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    Nammu.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
How to Install Nammu

In you project level build.gradle:

repositories {
    maven {
        url "https://jitpack.io"
    }
}

In your app level build.gradle:

dependencies {
    compile 'com.github.tajchert:nammu:1.2.0'
}
Nammu Resources
No. Location Link
1. GitHub HomePage
2. Camposha Tutorial

(c). RxPermissions

RxPermissions is an Android runtime permissions powered by RxJava2. This library allows the usage of RxJava with the new Android M permission model.

This is also another popular and great library like the ones we've listed here.

RxPermissions Requirements

To use this library your minSdkVersion must be >= 11.

How to Install RxPermissions
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.tbruyelle:rxpermissions:0.10.2'
}
How to use RxPermissions

First you need to create a RxPermissions instance :

final RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity or Fragment instance

NOTE: new RxPermissions(this) the this parameter can be an Activity or a Fragment. If you are using RxPermissions inside of a fragment you should pass the fragment instance(new RxPermissions(this)) as constructor parameter rather than new RxPermissions(fragment.getActivity()) or you could face a java.lang.IllegalStateException: FragmentManager is already executing transactions.

Then: for example : you can request the CAMERA permission (with Retrolambda for brevity, but not required)

// Must be done during an initialization phase like onCreate
rxPermissions
    .request(Manifest.permission.CAMERA)
    .subscribe(granted -> {
        if (granted) { // Always true pre-M
           // I can control the camera now
        } else {
           // Oups permission denied
        }
    });

If multiple permissions at the same time, the result is combined :

rxPermissions
    .request(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(granted -> {
        if (granted) {
           // All requested permissions are granted
        } else {
           // At least one permission is denied
        }
    });   

You can also observe a detailed result with requestEach or ensureEach :

rxPermissions
    .requestEach(Manifest.permission.CAMERA,
             Manifest.permission.READ_PHONE_STATE)
    .subscribe(permission -> { // will emit 2 Permission objects
        if (permission.granted) {
           // `permission.name` is granted !
        } else if (permission.shouldShowRequestPermissionRationale) {
           // Denied permission without ask never again
        } else {
           // Denied permission with ask never again
           // Need to go to the settings
        }
    });
RxPermissions Resources
No. Location Link
1. GitHub HomePage

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post