googlesamples /easypermissions

Simplify Android M system permissions

★ 9,856⑂ 1,460👁 1⑃ 118● 241▤ 12
下载:v3.0.0v3.0.0 ↓3.0.02019-01-23
+0
日增长
9,856
stars
1,460
forks
1
watchers
118
prs
241
issues
12
releases

README

EasyPermissions [![Build Status][1]][2] [![Android Weekly][3]][4]

EasyPermissions is a wrapper library to simplify basic system permissions logic when targeting
Android M or higher.

Note: If your app is written in Kotlin consider the easypermissions-ktx
library which adds Kotlin extensions to the core EasyPermissions library.

 Installation

EasyPermissions is installed by adding the following dependency to your build.gradle file:

groovy
dependencies {
    // For developers using AndroidX in their applications
    implementation 'pub.devrel:easypermissions:3.0.0'
 
    // For developers using the Android Support Library
    implementation 'pub.devrel:easypermissions:2.0.1'
}


 Usage

 Basic

To begin using EasyPermissions, have your Activity (or Fragment) override the onRequestPermissionsResult method:

java
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
}


 Request Permissions

The example below shows how to request permissions for a method that requires both
CAMERA and ACCESS_FINE_LOCATION permissions. There are a few things to note:

   Using EasyPermissionshasPermissions(...) to check if the app already has the
    required permissions. This method can take any number of permissions as its final
    argument.
   Requesting permissions with EasyPermissionsrequestPermissions. This method
    will request the system permissions and show the rationale string provided if
    necessary. The request code provided should be unique to this request, and the method
    can take any number of permissions as its final argument.
   Use of the AfterPermissionGranted annotation. This is optional, but provided for
    convenience. If all of the permissions in a given request are granted, all methods
    annotated with the proper request code will be executed(be sure to have an unique request code). The annotated method needs to be void and without input parameters (instead, you can use onSaveInstanceState in order to keep the state of your suppressed parameters). This is to simplify the common
    flow of needing to run the requesting method after all of its permissions have been granted.
    This can also be achieved by adding logic on the onPermissionsGranted callback.

java
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
private void methodRequiresTwoPermission() {
    String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
    if (EasyPermissions.hasPermissions(this, perms)) {
        // Already have

Top issues

Latest release

v3.0.0 3.0.0 2019-01-23
Version 3.0.0 contains a breaking change:

   Move from using Android Support Libraries (com.android.support.) to AndroidX libraries (androidx.).  If you are using the Android Support Libraries without using Jetifier this is a breaking change for you.

The newly updated transitive dependencies are:

xml
 
    
      androidx.appcompat
      appcompat
      1.0.2
    
    
      androidx.core
      core
      1.0.1
    
    
      androidx.fragment
      fragment
      1.0.0

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON