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) static factory method to request a variable number of permissions.
Usage:
@Rule
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule
.grant(android.Manifest.permission.ACCESS_FINE_LOCATION);
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# GrantPermissionRule\n\nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \\| [\\[Expand All\\]](#) \n\nGrantPermissionRule\n===================\n\n| This package is part of the Android [support library](/topic/libraries/support-library) which is no longer maintained. The support library has been superseded by [AndroidX](/jetpack/androidx) which is part of [Jetpack](/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider [migrating](/jetpack/androidx/migrate) existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library [class\n| mappings](/jetpack/androidx/migrate/class-mappings).\n\n\n`\npublic\n\n\nclass\nGrantPermissionRule\n`\n\n\n`\n\nextends `[Object](https://developer.android.com/reference/java/lang/Object.html)`\n\n\n`\n\n`\n\n\nimplements\n\nTestRule\n\n\n`\n\n|---|-----------------------------------------------|\n| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||\n| ↳ | android.support.test.rule.GrantPermissionRule |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nThe `GrantPermissionRule` Rule allows granting of runtime permissions on Android M (API 23)\nand above. Use this `Rule` when a test requires a runtime permission to do its work.\n\nWhen applied to a test class this Rule attempts to grant all requested runtime permissions.\nThe requested permissions will then be granted on the device and will take immediate effect.\nPermissions can only be requested on Android M (API 23) or above and will be ignored on all other\nAPI levels. Once a permission is granted it will apply for all tests running in the current\nInstrumentation. There is no way of revoking a permission after it was granted. Attempting to do\nso will crash the Instrumentation process.\n\nNote, this Rule is usually used to grant runtime permissions to avoid the permission dialog\nfrom showing up and blocking the App's Ui. This is especially helpful for Ui-Testing to avoid\nlosing control over the app under test.\n\nThe requested permissions will be granted for all test methods in the test class. Use [grant(String)](/reference/android/support/test/rule/GrantPermissionRule#grant(java.lang.String...)) static factory method to request a variable number of permissions.\n\nUsage:\n\n```\n\n @Rule\n public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule\n .grant(android.Manifest.permission.ACCESS_FINE_LOCATION);\n \n```\n\n\nNote: 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\n[WRITE_EXTERNAL_STORAGE](https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE) is requested.\n\n**This API is currently in beta.**\n\nSummary\n-------\n\n| ### Public methods ||\n|-------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` final Statement` | ` `[apply](/reference/android/support/test/rule/GrantPermissionRule#apply(org.junit.runners.model.Statement, org.junit.runner.Description))`(Statement base, Description description) ` |\n| ` static `[GrantPermissionRule](/reference/android/support/test/rule/GrantPermissionRule) | ` `[grant](/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. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| 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)`\u003c?\u003e` | ` 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() ` | ||\n| From interface ` org.junit.rules.TestRule ` |-----------------------|---------------------------------------------| | ` abstract Statement` | ` apply(Statement arg0, Description arg1) ` | ||\n\nPublic methods\n--------------\n\n### apply\n\n```\nStatement apply (Statement base, \n Description description)\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Parameters ||\n|---------------|----------------------|\n| `base` | `Statement` \u003cbr /\u003e |\n| `description` | `Description` \u003cbr /\u003e |\n\n| Returns ||\n|-------------|--------|\n| `Statement` | \u003cbr /\u003e |\n\n### grant\n\n```\nGrantPermissionRule grant (String... permissions)\n```\n\nStatic factory method that grants the requested permissions.\n\nPermissions will be granted before any methods annotated with `@Before` but before\nany test method execution.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|---------------|---------------------------------------------------------|\n| `permissions` | `String`: a variable list of Android permissions \u003cbr /\u003e |\n\n| Returns ||\n|---------------------------------------------------------------------------------|----------------------------------------------------------------------------------------|\n| [GrantPermissionRule](/reference/android/support/test/rule/GrantPermissionRule) | [GrantPermissionRule](/reference/android/support/test/rule/GrantPermissionRule) \u003cbr /\u003e |\n\n**See also:**\n\n- [Manifest.permission](/https://developer.android.com/reference/android/Manifest.permission) \n-\n\n Classes\n -------\n\n - [ActivityTestRule](/reference/android/support/test/rule/ActivityTestRule)\n - [DisableOnAndroidDebug](/reference/android/support/test/rule/DisableOnAndroidDebug)\n - [GrantPermissionRule](/reference/android/support/test/rule/GrantPermissionRule)\n - [ServiceTestRule](/reference/android/support/test/rule/ServiceTestRule)\n - [UiThreadTestRule](/reference/android/support/test/rule/UiThreadTestRule)"]]