Android Studio - remove Security Exception warning
Asked Answered
D

3

56

I'm getting the user's location through

Location location = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);

This line of code is inside a method and before calling this method I do check for Android run time permissions. Only if the permission is available from the user then I call this method. Code is working perfectly.

The problem is that Android Studio still shows an error on this line not recognising that I've already checked before calling this function.

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`

Now how do I remove this warning? I've already checked for permissions and don't want to check again just to remove this warning. I've tried adding @SuppressWarnings() but don't know the exact String to pass into this. @SuppressWarnings({"all"}) works but it is obviously not recommended.

How do I remove this warning?

EDIT 1 : This is my exact code -

private void checkPermissions() {
    if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED)
        getLocation();  //Method called if I have permission
}

private void getLocation() {
    //Android studio shows warning at this line.
    Location location = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
}

But if I put the permission check inside getLocation() method then the warning disappears. @SuppressWarnings({"MissingPermission"}) did not work.

EDIT 2: I've discovered that the only way to suppress the warning are -

Adding this comment on top of that particular piece of code -

//noinspection ResourceType

or adding this -

@SuppressWarnings({"ResourceType"})
Dodecahedron answered 1/2, 2016 at 7:11 Comment(2)
If my edited answer bellow works, please mark it as accepted.Alfalfa
Put @SuppressWarnings("MissingPermission") above your function implementationNotarize
A
124

You are looking for (updated to improve clarity):

@Override
@SuppressWarnings({"MissingPermission"})
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (LOCATION_REQUEST_CODE == requestCode) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);


       }
}

EDITED:

The correct lint rule is MissingPermission, but there seems to be a bug that misplace it to some people.

So, please try @SuppressWarnings({"ResourceType"}), too.

Alfalfa answered 4/2, 2016 at 2:31 Comment(7)
Are you sure? I updated my answer to best clarification. This code is parsed and compiling on my Android Studio (2.0-Preview 7) without any warning, from gradle or lint.Alfalfa
You have the permission declared on your manifest file too, right?Alfalfa
I have everything correct. Code works perfect. Just want to remove the warning. Check my edited question.Dodecahedron
Is exactly what I'm saying. The line @SuppressWarnings({"MissingPermission"}) is removing the warning to me.Alfalfa
@Jyotman Singh, @SuppressWarnings({"ResourceType"}) works properly?Alfalfa
{"ResourceType"} is working but {"MissingPermission"} is not. -0-aPullulate
The incorrect annotation name working was due to a Google bug. I think both work now.Sindee
E
6

If you are looking to ignore this lint warning, you can either do this inline:

//noinspection MissingPermission
Location lastLocation = FusedLocationApi.getLastLocation(locationClient);

or at method level:

@SuppressWarnings({"MissingPermission"})
public void toggleGPS(boolean enableGPS) {
  ...
}
Egor answered 20/12, 2016 at 12:33 Comment(0)
J
1

you can use Android Studio Inspection To Generate @SuppressWarnings({"MissingPermission"}) For Your Method Or Class

android studio missing permission check

Jewell answered 21/9, 2017 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.