GrantPermissionRule Failed to grant permissions

Viewed 6439

I am writing a test class and I am trying to grant the permissions using the rule GrantPermissionRule:

@Rule
public GrantPermissionRule permissionsRule = GrantPermissionRule.grant(
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE);

But I get the following error at the beginning of the test execution:

junit.framework.AssertionFailedError: Failed to grant permissions, see logcat for details
    at junit.framework.Assert.fail(Assert.java:50)
    at android.support.test.runner.permission.PermissionRequester.requestPermissions(PermissionRequester.java:110)
    at android.support.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:128)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
    at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886)

And this is the logcat output (Errors only):

12-23 13:03:21.196 13242-13242/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@data@com.google.android.gms@app_chimera@m@00000088@DynamiteLoader.apk@classes.dex: Permission denied
12-23 13:03:21.721 13306-13306/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@data@com.google.android.gms@app_chimera@m@0000008a@DynamiteModulesC.apk@classes.dex: Permission denied
12-23 13:03:22.361 13328-13328/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@data@com.google.android.gms@app_chimera@m@00000086@AdsDynamite.apk@classes.dex: Permission denied
12-23 13:03:26.303 13436-13467/my.app.package E/GrantPermissionCallable: Permission: android.permission.READ_EXTERNAL_STORAGE cannot be granted!

But this seems to happen only in my Redmi 4 with Android 6.0.1. But in a Samsung Galaxy Tab S with 6.0.1 it works, as well in some emulators...

What am I missing?

3 Answers

Found the solution. I had to grant permission in the developer options menu:

usb_debugging_security_settings

The above-answered option wasn't available in my OPPO device.

But there is an option called "Disable Permissions Monitoring" which does the same functionality.

To fix this issue, Turn ON the option of "Disbale Permission Functionality" under the Developer Options

For what it's worth: For me I just forgot specifying the permission in the app's manifest.

Related