16

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?

amp
  • 11,754
  • 18
  • 77
  • 133
  • 1
    Off the cuff, it seems like a bug in that particular device. Try using `adb shell` to grant the permission and see if that works. AFAIK, that's all `GetPermissionRule` is using. – CommonsWare Dec 23 '18 at 13:32

7 Answers7

11

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

usb_debugging_security_settings

amp
  • 11,754
  • 18
  • 77
  • 133
  • 6
    Unfortunately I do not have such an option in my developer settings. Any other solution. I have the same problem in 2 devices as well as in the emulator. BTW: It seems to be related to the WRITE_EXTERNAL_STORAGE permission. It does not complain if I only request READ_EXTERNAL_STORAGE or ACCESS_FINE_LOCATION as an example. – user2808624 Jan 31 '20 at 09:39
  • 3
    Don't think it's related to a specific permission, maybe a set of them. For me it's happening trying to grant `ACCESS_BACKGROUND_LOCATION`, works fine for `ACCESS_FINE_LOCATION`. I don't see the "Security Settings" in my developer options either. – Marino Apr 14 '20 at 14:17
  • 1
    @Marino that happens if your device is < AndroidQ. A permission you don't need can't be granted to you. That can only work in >=AndroidQ when background restrictions were introduced. – Otieno Rowland Nov 12 '22 at 10:30
9

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

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20
3

I faced this error when I tried to grant the POST_NOTIFICATIONS permission on a device with Android lower than 13. So check if the devices API level is high or low enough for the permissions you grant.

Fah
  • 199
  • 9
1

in my case, the READ_EXTERNAL_STORAGE permission is no longer granted since API level 32. changed to an emulator/device that has an API level of 31 or lower fixed the issue

hicham raf
  • 11
  • 1
0

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

Achraf Amil
  • 1,275
  • 16
  • 20
0

Following code fixed my issue, my test now are passing in all supported versions of my application.


Kotlin, the example provided also needs camera, you can remove it if you don't need it.
// Android 13 requires more specific permission to read media
// https://stackoverflow.com/questions/53903976
private val requiredMediaPermissions = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {

    Manifest.permission.READ_MEDIA_IMAGES
} else {

    Manifest.permission.READ_EXTERNAL_STORAGE
}
Akhha8
  • 418
  • 3
  • 10
0

In my case I don't declare WRITE_EXTERNAL_STORAGE in manifest and it was provided from some library, but in the new version of that library they set maxSdkVersion

So I just add that permission to my manifest and ignore maxSdkVersion

<uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        tools:remove="android:maxSdkVersion"
        tools:ignore="ScopedStorage" />
Deni Erdyneev
  • 1,779
  • 18
  • 26