title: Android Google Maps setMyLocationEnabled method error related to gradle file
body: <p>I am using Android Studio 1.5.1. I have a map fragment in my activity and I am trying to use the setMyLocationEnabled method in the activity code in order to display the user's current location on the map. I included all the necessary permissions code that is required in the activity file which is required if the user's phone is Android 6.0 Marshmallow.  Yet, I am getting the following error with a squiggly red line under "setMyLocationEnabled" even before I build the project:</p>
  
  <pre><code>call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException`
  </code></pre>
  
  <p>I can still build the project and run it on the device running Android 5.0.1, but the app crashes with an error message saying "sorry google play services has stopped working".</p>
  
  <p>The compileSdkVersion variable is set to 23 in the build.gradle file, but If I change the compileSdkVersion to a lower like 15, it causes all sorts of errors when I try to build the project. However, if I change targetsdkVersion from 23 to something like 18, I don't get that squiggly line and the app runs fine on the device without crashing.</p>
  
  <p>I am confused, I understand that I need to include the extra permissions code in the activity file just in case the user's phone is running marshmallow, but as you can see things aren't going too well. I want to target sdkversion 23 so that I can use features of Android 6.0 while supporting users that have a lower Android version on their phones. Any help would be greatly appreciated. </p>
  
  <p>Thanks.</p>
  
  <p><strong>MapsActivity.java</strong></p>
  
  <pre><code>   package dev.com.transitone;
  
  import android.Manifest;
  import android.content.pm.PackageManager;
  import android.support.design.widget.NavigationView;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.app.FragmentActivity;
  import android.os.Bundle;
  import android.support.v4.content.ContextCompat;
  import android.support.v4.view.GravityCompat;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.ActionBarDrawerToggle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.Toast;
  
  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.OnMapReadyCallback;
  import com.google.android.gms.maps.SupportMapFragment;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.MarkerOptions;
  
  import java.io.IOException;
  import java.util.logging.Logger;
  
  public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, NavigationView.OnNavigationItemSelectedListener {
  
      private GoogleMap mMap;
      private static final int LOCATION_REQUEST_CODE = 101;
      private String TAG = "MapDemo";
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_maps);
  
          requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
                  LOCATION_REQUEST_CODE);
  
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
  
  
          // Obtain the SupportMapFragment and get notified when the map is ready to be used.
          SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                  .findFragmentById(R.id.map);
          mapFragment.getMapAsync(this);
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          drawer.setDrawerListener(toggle);
          toggle.syncState();
  
          NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
          navigationView.setNavigationItemSelectedListener(this);
  
  
      }
  
      @Override
      public void onBackPressed() {
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          if (drawer.isDrawerOpen(GravityCompat.START)) {
              drawer.closeDrawer(GravityCompat.START);
          } else {
              super.onBackPressed();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      protected void requestPermission(String permissionType, int
              requestCode) {
          int permission = ContextCompat.checkSelfPermission(this,
                  permissionType);
          if (permission != PackageManager.PERMISSION_GRANTED) {
              ActivityCompat.requestPermissions(this,
                      new String[]{permissionType}, requestCode
              );
          }
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode,
                                             String permissions[], int[]
                                                     grantResults) {
          switch (requestCode) {
              case LOCATION_REQUEST_CODE: {
                  if (grantResults.length == 0
                          || grantResults[0] != PackageManager.PERMISSION_GRANTED)
                  {
                      Toast.makeText(this, "Unable to show location - permission required", Toast.LENGTH_LONG).show();
                  }
                  return;
              }
          }
      }
      /**
       * Manipulates the map once available.
       * This callback is triggered when the map is ready to be used.
       * This is where we can add markers or lines, add listeners or move the camera. In this case,
       * we just add a marker near Sydney, Australia.
       * If Google Play services is not installed on the device, the user will be prompted to install
       * it inside the SupportMapFragment. This method will only be triggered once the user has
       * installed Google Play services and returned to the app.
       */
      @Override
      public void onMapReady(GoogleMap googleMap) {
          mMap = googleMap;
  
          if (mMap != null) {
              mMap.setMyLocationEnabled(true);
          }
  
          // Add a marker in Sydney and move the camera
          /*LatLng sydney = new LatLng(-34, 151);
          mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
          mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
      }
  
  
      @SuppressWarnings("StatementWithEmptyBody")
      @Override
      public boolean onNavigationItemSelected(MenuItem item) {
          // Handle navigation view item clicks here.
          int id = item.getItemId();
  
          if (id == R.id.nav_camera) {
              // Handle the camera action
          } else if (id == R.id.nav_gallery) {
  
          } else if (id == R.id.nav_slideshow) {
  
          } else if (id == R.id.nav_manage) {
  
          } else if (id == R.id.nav_share) {
  
          } else if (id == R.id.nav_send) {
  
          }
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          drawer.closeDrawer(GravityCompat.START);
          return true;
      }
  
  
  }
  </code></pre>
  
  <p><strong>Manifest.xml</strong></p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="dev.com.transitone"&gt;
  
      &lt;!--
           The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
           Google Maps Android API v2, but you must specify either coarse or fine
           location permissions for the 'MyLocation' functionality.
      --&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
      &lt;!--&lt;application--&gt;
          &lt;!--android:allowBackup="true"--&gt;
          &lt;!--android:icon="@mipmap/ic_launcher"--&gt;
          &lt;!--android:label="@string/app_name"--&gt;
          &lt;!--android:supportsRtl="true"--&gt;
          &lt;!--android:theme="@style/AppTheme"&gt;--&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme.NoActionBar"&gt;
          &lt;activity
              android:name=".MapsActivity"
              android:label="@string/title_activity_maps"
              &gt;
              &lt;intent-filter
                  android:label="@string/app_name"&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name"
              android:theme="@style/AppTheme.NoActionBar"
              &gt;
  
          &lt;/activity&gt;
          &lt;!--
               The API key for Google Maps-based APIs is defined as a string resource.
               (See the file "res/values/google_maps_api.xml").
               Note that the API key is linked to the encryption key used to sign the APK.
               You need a different API key for each encryption key, including the release key that is used to
               sign the APK for publishing.
               You can define the keys for the debug and release targets in src/debug/ and src/release/.
          --&gt;
          &lt;meta-data
              android:name="com.google.android.geo.API_KEY"
              android:value="@string/google_maps_key" /&gt;
  
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>build.gradle</strong></p>
  
  <pre><code>    apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 23
      buildToolsVersion "23.0.3"
  
      defaultConfig {
          applicationId "dev.com.transitone"
          minSdkVersion 15
          targetSdkVersion 23
          versionCode 1
          versionName "1.0"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      testCompile 'junit:junit:4.12'
      compile 'com.android.support:appcompat-v7:23.2.1'
      compile 'com.android.support:design:23.2.1'
      compile 'com.google.android.gms:play-services:8.4.0'
  }
  </code></pre>