# GrantPermissionRule

Summary:[Methods](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule#)  

# GrantPermissionRule

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


class
GrantPermissionRule
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object.html)`


``


implements

TestRule


`

|---|-----------------------------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||
| ↳ | android.support.test.rule.GrantPermissionRule |

<br />

*** ** * ** ***

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 losing control over the app under test.

The requested permissions will be granted for all test methods in the test class. Use[grant(String)](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule#grant(java.lang.String...))static factory method to request a variable number of permissions.

Usage:  

```
 @Rule
 public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule
     .grant(android.Manifest.permission.ACCESS_FINE_LOCATION);
 
```

<br />

Note: As per[the documentation](https://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE)this rule will automatically grant[READ_EXTERNAL_STORAGE](https://developer.android.com/reference/android/Manifest.permission.html#READ_EXTERNAL_STORAGE)when[WRITE_EXTERNAL_STORAGE](https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE)is requested.

**This API is currently in beta.**

## Summary

|                                                                                                                                                                                               ### Public methods                                                                                                                                                                                               ||
|------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` final Statement`                                                                                                     | ` `[apply](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule#apply(org.junit.runners.model.Statement, org.junit.runner.Description))`(Statement base, Description description) `                                                                   |
| ` static `[GrantPermissionRule](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule) | ` `[grant](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule#grant(java.lang.String...))`(`[String...](https://developer.android.com/reference/java/lang/String.html)` permissions) ` Static factory method that grants the requested permissions. |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule#)From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object.html)` ` |-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object.html)          | ` clone() `                                                                                | | ` boolean`                                                                          | ` equals(`[Object](https://developer.android.com/reference/java/lang/Object.html)` arg0) ` | | ` void`                                                                             | ` finalize() `                                                                             | | ` final `[Class](https://developer.android.com/reference/java/lang/Class.html)`<?>` | ` getClass() `                                                                             | | ` int`                                                                              | ` hashCode() `                                                                             | | ` final void`                                                                       | ` notify() `                                                                               | | ` final void`                                                                       | ` notifyAll() `                                                                            | | ` `[String](https://developer.android.com/reference/java/lang/String.html)          | ` toString() `                                                                             | | ` final void`                                                                       | ` wait(long arg0, int arg1) `                                                              | | ` final void`                                                                       | ` wait(long arg0) `                                                                        | | ` final void`                                                                       | ` wait() `                                                                                 | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule#)From interface` org.junit.rules.TestRule ` |-----------------------|---------------------------------------------| | ` abstract Statement` | ` apply(Statement arg0, Description arg1) ` | ||

## Public methods

### apply

```
Statement apply (Statement base, 
                Description description)
```

<br />

<br />

|             Parameters              ||
|---------------|----------------------|
| `base`        | `Statement` <br />   |
| `description` | `Description` <br /> |

|       Returns       ||
|-------------|--------|
| `Statement` | <br /> |

### grant

```
GrantPermissionRule grant (String... permissions)
```

Static factory method that grants the requested permissions.

Permissions will be granted before any methods annotated with`&#64;Before`but before any test method execution.

<br />

|                               Parameters                               ||
|---------------|---------------------------------------------------------|
| `permissions` | `String`: a variable list of Android permissions <br /> |

|                                                                                                              Returns                                                                                                              ||
|--------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|
| [GrantPermissionRule](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule) | [GrantPermissionRule](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule) <br /> |

**See also:**

- [Manifest.permission](https://developer.android.com/https://developer.android.com/reference/android/Manifest.permission)  
-

  ## Classes

  - [ActivityTestRule](https://developer.android.com/reference/android/support/test/rule/ActivityTestRule)
  - [DisableOnAndroidDebug](https://developer.android.com/reference/android/support/test/rule/DisableOnAndroidDebug)
  - [GrantPermissionRule](https://developer.android.com/reference/android/support/test/rule/GrantPermissionRule)
  - [ServiceTestRule](https://developer.android.com/reference/android/support/test/rule/ServiceTestRule)
  - [UiThreadTestRule](https://developer.android.com/reference/android/support/test/rule/UiThreadTestRule)