googlesamples /easypermissions
Simplify Android M system permissions
★ 9,856⑂ 1,460👁 1⑃ 118● 241▤ 12
+0
Daily
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 haveTop issues
#278java.lang.NullPointerException12 comments · 2019-03-21#218Rationale callback when not using Fragment or Activity10 comments · 2018-05-24#221AAPT: error: resource style/EasyPermissions.Transparent (aka com.app.internal.debug:style/EasyPermissions.Transparent) not found7 comments · 2018-06-04#301Support asking for permissions again after returning from permissions settings6 comments · 2020-03-10#266Crashing when showing rationale dialog6 comments · 2018-12-21#349Request: Make easypermissions library target to android api 334 comments · 2022-08-25#330OnActivityResult Deprecated in latest Android X AppCompatActivity4 comments · 2021-06-08#311java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference4 comments · 2020-08-13
Latest release
v3.0.0 3.0.0 2019-01-23Version 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