title: Auto grant permissions for Espresso UI Testing
body: <p>I'm trying to avoid having to manually grant permissions on every single UI test built with Espresso. </p>
  
  <p>I have tried:</p>
  
  <pre><code> @Rule
      public GrantPermissionRule permissionRule = GrantPermissionRule.grant(
              android.Manifest.permission.INTERNET,
              android.Manifest.permission.READ_PHONE_STATE,
              android.Manifest.permission.ACCESS_NETWORK_STATE,
              android.Manifest.permission.ACCESS_FINE_LOCATION.....)
  </code></pre>
  
  <p>and</p>
  
  <pre><code> @Before
      public void grantPhonePermission()
          {
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M)
                  {
                      getInstrumentation().getUiAutomation().executeShellCommand(
                              "pm grant " + getTargetContext().getPackageName()
                                      + " android.permission.ACCESS_FINE_LOCATION");
                  }
          }
  </code></pre>
  
  <p>And here's most of my gradle file :</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
  compileSdkVersion 24
  buildToolsVersion '26.0.2'
  defaultConfig {
      minSdkVersion 14
      targetSdkVersion 23
      testInstrumentationRunner 
  'android.support.test.runner.AndroidJUnitRunner'
  }
  buildTypes {
      release {
          minifyEnabled false
          proguardFiles getDefaultProguardFile('proguard-android.txt'), 
  'proguard-rules.txt'
          debuggable false
          jniDebuggable false
          buildConfigField "boolean", "UseProduction", "true"
      }
      qa {
          initWith debug
          debuggable true
          applicationIdSuffix ".qa" 
          buildConfigField "boolean", "UseProduction", "false"
      }
      debug {
          debuggable true
          testCoverageEnabled = false
          buildConfigField "boolean", "UseProduction", "true"
      }
  
  }
  
  flavorDimensions "standard"
  
  productFlavors {
      standard
              {
                  buildConfigField "boolean", "ChinaBuild", "false"
                  buildConfigField "boolean", "YTOBuild", "false"
              }
     ....
  }
  compileOptions {
      sourceCompatibility JavaVersion.VERSION_1_7
      targetCompatibility JavaVersion.VERSION_1_7
  }
  sourceSets {
      main {
          java.srcDirs = ['src/main/java']
      }
  }
  
  
  dependencies {
  implementation 'com.android.support:support-v4:24.2.1'
  implementation 'com.android.support:appcompat-v7:24.2.1'
  implementation 'com.android.support:design:24.2.1'
  implementation 'com.android.support:recyclerview-v7:24.2.1'
  implementation 'com.android.support:percent:24.2.1'
  implementation 'com.google.firebase:firebase-messaging:10.0.1'
  implementation 'com.android.support.constraint:constraint-layout:1.0.2'
  androidTestImplementation 'com.android.support.test.espresso:espresso- 
  core:3.0.2'
  androidTestImplementation 'junit:junit:4.12'
  androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
  androidTestImplementation 'org.mockito:mockito-core:2.+'
  androidTestImplementation 'com.android.support.test:runner:1.0.2'
  androidTestImplementation 'com.android.support.test:rules:1.0.2'
  }
  </code></pre>
  
  <p>None of it works.
  As you can see I'm targeting API 23 so <code>GrantPermissionRule</code> should work. Any ideas? I've been stuck on this for 3 days. :(</p>
  
  <p>It looks like my post is mostly code, I should add more details... But not sure what more needs to be said.</p>