Skip to content

Most visited

Recently visited

navigation

GrantPermissionRule

public class GrantPermissionRule
extends Object implements TestRule

java.lang.Object
   ↳ android.support.test.rule.GrantPermissionRule


The GrantPermissionRule Rule allows granting of runtime permissions on Android M (API 23) and above. Use this Rule when a test requires a runtime permission to do its work.

When applied to a test class this Rule attempts to grant all requested runtime permissions. The requested permissions will then be granted on the device and will take immediate effect. Permissions can only be requested on Android M (API 23) or above and will be ignored on all other API levels. Once a permission is granted it will apply for all tests running in the current Instrumentation. There is no way of revoking a permission after it was granted. Attempting to do so will crash the Instrumentation process.

Note, this Rule is usually used to grant runtime permissions to avoid the permission dialog from showing up and blocking the App's Ui. This is especially helpful for Ui-Testing to avoid loosing control over the app under test.

The requested permissions will be granted for all test methods in the test class. Use grant(String) static factory method to request a variable number of permissions. Usage: \@Rule public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule .grant(android.Manifest.permission.ACCESS_FINE_LOCATION);

This API is currently in beta.

Summary

Public methods

final Statement apply(Statement base, Description description)
static GrantPermissionRule grant(String... permissions)

Static factory method that grants the requested permissions.

Protected methods

void setPermissionRequester(PermissionRequester permissionRequester)

Inherited methods

From class java.lang.Object
From interface org.junit.rules.TestRule

Public methods

apply

Statement apply (Statement base,
                Description description)

Parameters
base Statement

description Description

Returns
Statement

grant

GrantPermissionRule grant (String... permissions)

Static factory method that grants the requested permissions.

Permissions will be granted before any methods annotated with @Before but before any test method execution.

Parameters
permissions String: a variable list of Android permissions

Returns
GrantPermissionRule GrantPermissionRule

See also:

Protected methods

setPermissionRequester

void setPermissionRequester (PermissionRequester permissionRequester)

Parameters
permissionRequester PermissionRequester

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.