android GrantPermissionRule无法授予权限

nfzehxib  于 2023-01-11  发布在  Android
关注(0)|答案(4)|浏览(306)

我正在编写一个测试类,并尝试使用规则GrantPermissionRule授予权限:

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

但是在测试执行开始时,我得到了以下错误:

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)

这是logcat输出(仅错误):

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!

但这似乎只发生在我的Redmi 4与Android 6. 0. 1。但在三星Galaxy Tab S与6. 0. 1它的工作,以及在一些模拟器...
我错过了什么?

rhfm7lfc

rhfm7lfc1#

找到了解决方案。我必须在开发者选项菜单中授予权限:

wmomyfyw

wmomyfyw2#

上面回答的选项在我的OPPO设备中不可用。
但是有一个名为“禁用权限监视”的选项可以执行相同的功能。
要解决此问题,请打开“开发人员选项”下的“禁用权限功能”选项

w8f9ii69

w8f9ii693#

在我的示例中,由于API级别为32,因此不再授予READ_EXTERNAL_STORAGE权限。更改为API级别为31或更低的模拟器/设备修复了此问题

vlf7wbxs

vlf7wbxs4#

不管怎样:对我来说,我只是忘记了在应用程序的清单中指定权限。

相关问题