Android Espresso java.lang.SecurityException: 权限 android.permission.SYSTEM_ALERT_WINDOW 不是可更改的权限类型
Android Espresso java.lang.SecurityException: Permission android.permission.SYSTEM_ALERT_WINDOW is not a changeable permission type
我有一个 Espresso 脚本需要 SYSTEM_ALERT_WINDOW
权限。我使用以下代码授予所有测试脚本的权限:
@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
我有一个特殊的 Android包含权限的测试用例清单。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
这适用于来自不同品牌和 Android 版本的大多数设备,从 Android 5.X 到 Android 8.x(大约 15+ 设备).但是,在我的 Android 6.0 华为 P8 Lite 上,出现以下错误:
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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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:1890)
当我检查 logcat 时,显示了以下附加错误:
I/GrantPermissionCallable: Permission: android.permission.WRITE_EXTERNAL_STORAGE is already granted!
I/GrantPermissionCallable: Permission: android.permission.CAMERA is already granted!
I/UiAutomationShellCmd: Requesting permission: pm grant [PACKAGE_NAME_MY_APP] android.permission.SYSTEM_ALERT_WINDOW
E/GrantPermissionCallable: Permission: android.permission.SYSTEM_ALERT_WINDOW cannot be granted!
java.lang.SecurityException: Permission android.permission.SYSTEM_ALERT_WINDOW is not a changeable permission type
at com.android.server.pm.PackageManagerService.enforceDeclaredAsUsedAndRuntimeOrDevelopmentPermission(PackageManagerService.java:3665)
at com.android.server.pm.PackageManagerService.grantRuntimePermission(PackageManagerService.java:3698)
at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:398)
at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:2807)
at com.android.server.pm.HwPackageManagerService.onTransact(HwPackageManagerService.java:506)
at android.os.Binder.execTransact(Binder.java:453)
我还尝试在设备上手动更改测试应用程序和待测试应用程序的 'display over other apps' 权限。但这也没有改变任何东西。
当我从 GrantPermissionRule 中删除 SYSTEM_ALERT_WINDOW 权限时,所有其他测试 运行 正常。
我不明白为什么这会在某些设备上失败。
我使用以下网站进行研究,但其中 none 个有用:
我做了以下事情来解决这个问题。首先,我将以下行添加到 Espresso AndroidManifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
我没有将权限规则添加到我的所有测试用例中,而是将其添加到特定的测试用例中。
@Rule public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW);
如果设备无法绘制叠加层,我将忽略测试。
@Before
public void shouldPerformTest()
{
boolean shouldPerformTest = true;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
shouldPerformTest = Settings.canDrawOverlays(getContext());
}
Assume.assumeTrue(shouldPerformTest);
}
我有一个 Espresso 脚本需要 SYSTEM_ALERT_WINDOW
权限。我使用以下代码授予所有测试脚本的权限:
@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
我有一个特殊的 Android包含权限的测试用例清单。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
这适用于来自不同品牌和 Android 版本的大多数设备,从 Android 5.X 到 Android 8.x(大约 15+ 设备).但是,在我的 Android 6.0 华为 P8 Lite 上,出现以下错误:
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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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:1890)
当我检查 logcat 时,显示了以下附加错误:
I/GrantPermissionCallable: Permission: android.permission.WRITE_EXTERNAL_STORAGE is already granted!
I/GrantPermissionCallable: Permission: android.permission.CAMERA is already granted!
I/UiAutomationShellCmd: Requesting permission: pm grant [PACKAGE_NAME_MY_APP] android.permission.SYSTEM_ALERT_WINDOW
E/GrantPermissionCallable: Permission: android.permission.SYSTEM_ALERT_WINDOW cannot be granted!
java.lang.SecurityException: Permission android.permission.SYSTEM_ALERT_WINDOW is not a changeable permission type
at com.android.server.pm.PackageManagerService.enforceDeclaredAsUsedAndRuntimeOrDevelopmentPermission(PackageManagerService.java:3665)
at com.android.server.pm.PackageManagerService.grantRuntimePermission(PackageManagerService.java:3698)
at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:398)
at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:2807)
at com.android.server.pm.HwPackageManagerService.onTransact(HwPackageManagerService.java:506)
at android.os.Binder.execTransact(Binder.java:453)
我还尝试在设备上手动更改测试应用程序和待测试应用程序的 'display over other apps' 权限。但这也没有改变任何东西。
当我从 GrantPermissionRule 中删除 SYSTEM_ALERT_WINDOW 权限时,所有其他测试 运行 正常。 我不明白为什么这会在某些设备上失败。
我使用以下网站进行研究,但其中 none 个有用:
我做了以下事情来解决这个问题。首先,我将以下行添加到 Espresso AndroidManifest
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
我没有将权限规则添加到我的所有测试用例中,而是将其添加到特定的测试用例中。
@Rule public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW);
如果设备无法绘制叠加层,我将忽略测试。
@Before
public void shouldPerformTest()
{
boolean shouldPerformTest = true;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
shouldPerformTest = Settings.canDrawOverlays(getContext());
}
Assume.assumeTrue(shouldPerformTest);
}