92

When adding permissions to my manifest file, the below xml works.

 <permission android:name="android.permission.ACCESS_FINE_LOCATION" />

However, this xml doesn't work.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Which one am I supposed to be using? If it's the first one, why wouldn't it work? How can I fix it?

Also, I am getting an Android 6.0 runtime permissions related exception:

java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.

When I try to add the permission to a String array in order to check the permission, Android Studio tells me it can't resolve Manifest.permission in the below code:

new String[]{Manifest.permission.ACCESS_FINE_LOCATION}

Why would it be doing this? How can I fix it?

sonictt1
  • 2,906
  • 1
  • 15
  • 18
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110

13 Answers13

242

For the first part, you should be using <uses-permission> according the the Android Devlopers site. Try making sure you declare your permissions directly under the <manifest> tag, not in your <application> tag. It's hard to know what your problem is without seeing your entire manifest file. Check out the link I posted above for more info on how to declare permissions in your manifest.

In regards to your runtime permissions problem:

With uses-permissions Cannot resolve that..

new String[]{Manifest.permission.ACCESS_FINE_LOCATION}

Why?

Make sure you're using android.Manifest instead of my.app.package.Manifest. A lot of times Android Studio will default to the latter instead of the former.

So, your new line of code would look like:

new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION};

Edit: I reformatted my answer.

Edit 2: Be wary of importing android.Manifest. It can cause issues if you're also importing my.app.package.Manifest. Other than that import android.Manifest is another valid way to resolve this issue.

sonictt1
  • 2,906
  • 1
  • 15
  • 18
  • 4
    Using andorid.Manifest.permission.X instead of Manifest.permission.X solved this for me, thanks. – hnilsen Nov 20 '17 at 07:13
  • I am new in android so i am not able to understand where i have to put "new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION};" this line in the android studio. – anshuman burmman Jan 09 '20 at 18:33
110

change this

Manifest.permission.ACCESS_FINE_LOCATION

into this

android.Manifest.permission.ACCESS_FINE_LOCATION

fcicer
  • 1,192
  • 1
  • 7
  • 11
30

Try this! Since ACCESS_FINE_LOCATION available in following package so Add:

import android.Manifest;

­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

Sanket Parchande
  • 894
  • 9
  • 14
8

I had a similar problem where used;

ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED 

where it could not resolve symbol READ_CONTACTS.

However on using;

import android.Manifest;

It started to recognize READ_CONTACT

bennygenel
  • 23,896
  • 6
  • 65
  • 78
Madhur Bhatnagar
  • 191
  • 1
  • 2
  • 5
6

If you have already the uses.permissions setup correctly in your manifest file, as already mentioned by hnilsen, just replace your line:

 Manifest.permission.ACCESS_FINE_LOCATION

by this one:

 android.Manifest.permission.ACCESS_FINE_LOCATION

This can solve your problem.

Marcelo Gumiero
  • 1,849
  • 2
  • 14
  • 14
6

Add this import android.Manifest; enter image description here

3

if you are working on dynamic permissions and any permission like ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION giving error "cannot resolve method PERMISSION_NAME" in this case write you code with permission name and then rebuild your project this will regenerate the manifest(Manifest.permission) file

anoop ghildiyal
  • 821
  • 10
  • 18
0
 if (Build.VERSION.SDK_INT >= 23) {

            if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

                mapView.setMyLocationEnabled(true); 
            }
        }
        else
        {
            mapView.setMyLocationEnabled(true);

        }
toto_tata
  • 14,526
  • 27
  • 108
  • 198
0

Try this before running, make sure you have permission to access..

try {
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
} catch (SecurityException e) {
   dialogGPS(this.getContext()); // lets the user know there is a problem with the gps
}
Iam ByeBlogs
  • 715
  • 1
  • 6
  • 15
0

Change

new String[]{Manifest.permission.ACCESS_FINE_LOCATION}

To this

new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}

Your problem will be resolved.

Hassnain Jamil
  • 1,651
  • 17
  • 21
  • This seems to be just a repeat of [this existing answer](https://stackoverflow.com/a/38083495). – Pang Jan 25 '18 at 10:06
0
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}

change it to

android.manifest.permission.ACCESS_FINE_LOCATION
Pang
  • 9,564
  • 146
  • 81
  • 122
-1

When you press Alt+Enter on Manifest, Android Studio suggests a few options to import. You should choose the one import android.Manifest; instead of import <your.package.name>.Manifest;.

林果皞
  • 7,539
  • 3
  • 55
  • 70
johnnyman
  • 129
  • 2
  • 11
-2

Try this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_SELECT_PICTURE);
        return;
    }
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Iam ByeBlogs
  • 715
  • 1
  • 6
  • 15