Search code examples
androidandroid-permissionsandroid-espressoandroid-securityexceptionsystem-alert-window

Android Espresso java.lang.SecurityException: Permission android.permission.SYSTEM_ALERT_WINDOW is not a changeable permission type


I have an Espresso script that needs the SYSTEM_ALERT_WINDOW permission. I use the following code to grant the permission for all test scripts:

@Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE);

And I have a special AndroidManifest for test cases that includes the permissions.

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

This works fine on most devices from different brands and Android versions varying from Android 5.X up to Android 8.x (about 15+ devices). However on my Huawei P8 Lite with Android 6.0 I get the following error:

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.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$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:1890)

When I check the logcat the following additional errors are shown:

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)

I also tried changing the 'display over other apps' permission manually on the device for the test-app and the to-be-tested-app. But this also didn't change anything.

When I remove the SYSTEM_ALERT_WINDOW permission from the GrantPermissionRule all other tests run fine. I don't see why this would fail on some devices.

I used the following websites for research but none of them were useful:


Solution

  • I did the following to solve this issue. First I added the following line to the Espresso AndroidManifest

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    

    Instead of adding the permission rule to all my testcases I added it to the specific test case.

    @Rule public GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant(Manifest.permission.SYSTEM_ALERT_WINDOW);
    

    And I ignore the test if the device cannot draw overlays.

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