# GrantPermissionRule

Artifact: [androidx.test:rules](https://developer.android.com/jetpack/androidx/releases/test) [View Source](https://cs.android.com/search?q=file:androidx/test/rule/GrantPermissionRule.java+class:androidx.test.rule.GrantPermissionRule)

*** ** * ** ***


```
class GrantPermissionRule : TestRule
```

<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 `https://developer.android.com/reference/kotlin/androidx/test/rule/GrantPermissionRule#grant(java.lang.String...)` static factory method to request a variable number of permissions.

Usage:

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

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

See [Request App Permissions](https://developer.android.com/training/permissions/requesting) for more details on runtime permissions.

For tests running on Android SDKs \>= API 28, use `https://developer.android.com/reference/android/app/UiAutomation.html#grantRuntimePermission(java.lang.String, java.lang.String)` instead.

## Summary

| ### Public functions |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html!` | `https://developer.android.com/reference/kotlin/androidx/test/rule/GrantPermissionRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description)(base: https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html!, description: https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html!)` |
| `java-static https://developer.android.com/reference/kotlin/androidx/test/rule/GrantPermissionRule!` | `https://developer.android.com/reference/kotlin/androidx/test/rule/GrantPermissionRule#grant(java.lang.String...)(permissions: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>!)` Static factory method that grants the requested permissions. |

## Public functions

### apply

```
fun apply(base: Statement!, description: Description!): Statement!
```

### grant

```
java-static fun grant(permissions: Array<String!>!): GrantPermissionRule!
```

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.

| Parameters |
|---|---|
| `permissions: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!>!` | a variable list of Android permissions |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/test/rule/GrantPermissionRule!` | `https://developer.android.com/reference/kotlin/androidx/test/rule/GrantPermissionRule` |

| See also |
|---|---|
| `https://developer.android.com/reference/android/Manifest.permission.html` |   |