googlesamples / easypermissions

Simplify Android M system permissions
https://firebaseopensource.com/projects/googlesamples/easypermissions/
Apache License 2.0
9.87k stars 1.45k forks source link

Crash when deny permission #184

Closed joepake closed 6 years ago

joepake commented 6 years ago

Basic Information

Device type: Crash on Emulator and some real devices OS version: 6.0.0 EasyPermissions version: 1.1.0

Describe the problem

private final static int RC_CAMERA_AND_STORAGE = 1009;

    @AfterPermissionGranted(RC_CAMERA_AND_STORAGE)
    private void methodRequiresTwoPermission() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(getContext(), perms)) {
        } else {
            // Do not have permissions, request them now
            //Crash here
            EasyPermissions.requestPermissions(this, getString(R.string.permission_camera_and_storage),
                    RC_CAMERA_AND_STORAGE, perms);
        }
    }

    @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);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> list) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> list) {
    }

Crash when the user denies the permission request.

logs

                                                             at pub.devrel.easypermissions.RationaleDialogFragmentCompat.showAllowingStateLoss(RationaleDialogFragmentCompat.java:47)
                                                             at pub.devrel.easypermissions.helper.BaseSupportPermissionsHelper.showRequestPermissionRationale(BaseSupportPermissionsHelper.java:29)
                                                             at pub.devrel.easypermissions.helper.PermissionHelper.requestPermissions(PermissionHelper.java:75)
                                                             at pub.devrel.easypermissions.EasyPermissions.requestPermissions(EasyPermissions.java:217)
                                                             at pub.devrel.easypermissions.EasyPermissions.requestPermissions(EasyPermissions.java:119)
                                                             at net.sample.SampleFragment.methodRequiresTwoPermission(SampleFragment.java:438)
samtstern commented 6 years ago

@ohmygodntt you say you're seeing this on an emulator, could you give me some steps to reproduce the crash?

joepake commented 6 years ago

@samtstern please check out my repository https://github.com/ohmygodntt/easypermissioncrash I had define 2 cases in https://github.com/ohmygodntt/easypermissioncrash/blob/master/app/build.gradle

Case 1: compileSdkVersion 26 targetSdkVersion 26 support libs verion 26.1.0

Request Permissions button -> Deny first permission, Allow the second permission Request Permissions button again -> Deny first permission, Deny the second permission -> Crash (Application is restarted)

Case 2: compileSdkVersion 25 targetSdkVersion 23 support libs verion 25.3.1

Request Permissions button -> Deny first permission, Deny the second permission Request Permissions button again -> Crash Process: com.test.easypermissionscrash, PID: 2918 java.lang.NoSuchMethodError: No virtual method isStateSaved()Z in class Landroid/support/v4/app/FragmentManager; or its super classes (declaration of 'android.support.v4.app.FragmentManager' appears in /data/app/com.test.easypermissionscrash-2/split_lib_dependencies_apk.apk) at pub.devrel.easypermissions.RationaleDialogFragmentCompat.showAllowingStateLoss(RationaleDialogFragmentCompat.java:47) at pub.devrel.easypermissions.helper.BaseSupportPermissionsHelper.showRequestPermissionRationale(BaseSupportPermissionsHelper.java:29) at pub.devrel.easypermissions.helper.PermissionHelper.requestPermissions(PermissionHelper.java:75) at pub.devrel.easypermissions.EasyPermissions.requestPermissions(EasyPermissions.java:217) at pub.devrel.easypermissions.EasyPermissions.requestPermissions(EasyPermissions.java:105) at com.test.easypermissionscrash.MainActivity.methodRequiresTwoPermission(MainActivity.java:25)

SUPERCILEX commented 6 years ago

@ohmygodntt Use the support library version 27.0.2 and the crash should go away.

joepake commented 6 years ago

@SUPERCILEX I changed like your comment but still crash. App is restarted compileSdkVersion 27 targetSdkVersion 27 support libs verion 27.0.2

Hlancan commented 6 years ago

I have the same problem when I deny one of the permissions

samtstern commented 6 years ago

@ohmygodntt thanks for taking the time to make a test case! I will check it out now.

samtstern commented 6 years ago

@ohmygodntt ok i tried your repo. To summarize:

Case 1

compileSdkVersion 26
targetSdkVersion 26
supportLibraryVersion 26.1.0

I was not able to produce any crash here.

Case 2

compileSdkVersion 25
targetSdkVersion 23
supportLibraryVersion 25.3.1

I was able to produce the crash as you said.

Case 3

compileSdkVersion 27
targetSdkVersion 26
supportLibraryVersion 27.0.2

I was not able to produce any crash here.

Before each test I did a clean install:

$ adb shell pm uninstall com.test.easypermissionscrash; ./gradlew :app:installDebug

So my conclusion is that @SUPERCILEX is right, you need to update to support library 27+. If you can reproduce a crash in that situation happy to try again.

samtstern commented 6 years ago

Ohhh wait! I think there's definitely an issue here: https://github.com/googlesamples/easypermissions/issues/187

samtstern commented 6 years ago

This should be fixed 1.1.1. I took "Case 2" and checked the dependency tree against 1.1.0 and 1.1.1-SNAPSHOT;

Before

debug
debugCompileClasspath - Dependencies for compilation
+--- com.android.support:appcompat-v7:25.3.1@aar
+--- pub.devrel:easypermissions:1.1.0@aar
+--- com.android.support:animated-vector-drawable:25.3.1@aar
+--- com.android.support:support-vector-drawable:25.3.1@aar
+--- com.android.support:support-v4:25.3.1@aar
+--- com.android.support:support-fragment:25.3.1@aar
+--- com.android.support:support-media-compat:25.3.1@aar
+--- com.android.support:support-core-utils:25.3.1@aar
+--- com.android.support:support-core-ui:25.3.1@aar
+--- com.android.support:support-compat:25.3.1@aar
\--- com.android.support:support-annotations:25.3.1@jar

After

> Task :app:androidDependencies
debug
debugCompileClasspath - Dependencies for compilation
+--- pub.devrel:easypermissions:1.1.1-SNAPSHOT@aar
+--- com.android.support:appcompat-v7:27.0.1@aar
+--- com.android.support:support-fragment:27.0.1@aar
+--- com.android.support:animated-vector-drawable:27.0.1@aar
+--- com.android.support:support-core-ui:27.0.1@aar
+--- com.android.support:support-core-utils:27.0.1@aar
+--- com.android.support:support-vector-drawable:27.0.1@aar
+--- com.android.support:support-compat:27.0.1@aar
+--- com.android.support:support-annotations:27.0.1@jar
+--- android.arch.lifecycle:runtime:1.0.0@aar
+--- android.arch.lifecycle:common:1.0.0@jar
\--- android.arch.core:common:1.0.0@jar

Now the app does not build in Case 2 ... which is at least progress from a runtime crash!

samtstern commented 6 years ago

Ok confirmed:

If you use easypermissions 1.1.1 and compileSdkVersion 27 you don't get this crash anymore.