5

I'm getting the following crash on certain devices API 6.0+:

Fatal Exception: java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
       at android.os.Parcel.readException(Parcel.java:1693)
       at android.os.Parcel.readException(Parcel.java:1646)
       at com.google.android.gms.internal.zzed.zza(Unknown Source)
       at com.google.android.gms.internal.zzcda.zzdw(Unknown Source)
       at com.google.android.gms.internal.zzcdd.zzvQ(Unknown Source)
       at com.google.android.gms.internal.zzcdj.zzvQ(Unknown Source)
       at com.google.android.gms.location.zzf.zza(Unknown Source)
       at com.google.android.gms.internal.zzbaq.zza(Unknown Source)
       at com.google.android.gms.internal.zzbdd.zzb(Unknown Source)
       at com.google.android.gms.internal.zzbdd.zzqq(Unknown Source)
       at com.google.android.gms.internal.zzbdd.onConnected(Unknown Source)
       at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source)
       at com.google.android.gms.common.internal.zzn.zzj(Unknown Source)
       at com.google.android.gms.common.internal.zze.zzrj(Unknown Source)
       at com.google.android.gms.common.internal.zzi.zzrk(Unknown Source)
       at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.os.HandlerThread.run(HandlerThread.java:61)

Note that I am asking the user for location permission before making any location related operation. But for some reason I can't reproduce, some devices seem to be reporting having location permission when I use:

boolean hasPermission = ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;

Because of that, I need to be able to catch the SecurityException and ignore it when it happens. Or perhaps figure out a way to tell the user to enable location permissions manually.

The crash above started to happen when I started to use the new APIs from the Play Services to request location using the FusedLocationProviderClient. It crashes when I try to do the following without location permissions grated, for example:

LocationServices.getFusedLocationProviderClient(this).getLocationAvailability().addOnFailureListener(myListener);

The failure listener does not send the crash to myListener unfortunately. Surrounding the above with try..catch doesn't work either. I.e., this doesn't work and the app crashes anyway throwing SecurityException:

try {
    LocationServices.getFusedLocationProviderClient(this)
        .getLocationAvailability()
        .addOnFailureListener(myListener);
} catch(Exception e) {
    Log.e(e);
}

This problem seems to be related to the following issues:

Any ideas?

ADev
  • 5,259
  • 2
  • 16
  • 29
  • What are you trying to catch in the try/catch? – Daniel J Jul 10 '17 at 14:08
  • please check your menifest some time its happen to forgot give permission in menifest . – Darshan Kachhadiya Jul 10 '17 at 14:13
  • I have the permission in the manifest. The app works for me on all the devices I try. I'm seeing the above issue only on crashlytics. – ADev Jul 10 '17 at 14:38
  • @DanielJunyszek I edited the question with an example of what I'm trying to catch. – ADev Jul 10 '17 at 14:38
  • Try catching throwable instead of exception – Daniel J Jul 10 '17 at 15:21
  • `SecurityException` is an `Exception` (https://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.html) so that won't help unfortunately. – ADev Jul 10 '17 at 15:34
  • Have you tried on Android 8.0 devices? I just noticed the same crash in one of my apps on crashlytics and it seems limited to Android 8.0 devices. – ZeroStatic Aug 24 '17 at 07:03
  • It crashes on a subset of Android 6+ devices. I haven't tried recently because for now I reverted back to using the old APIs for getting location. – ADev Aug 24 '17 at 07:39
  • I have the exact same issue on some devices since using FusedLocationProviderClient. Do you know if the problem is fixed in some newer Play Services version? – BladeCoder Feb 20 '18 at 01:50

1 Answers1

-1

Have you considered the fact, that some users revoke the permission after your app was launched? You should always check the permissions every time before calling any methods in FusedLocationProviderClient.

whlk
  • 15,487
  • 13
  • 66
  • 96
  • Yes, I check if the permission is present every time on `onResume()` of the activity. I highlighted this in my question: "I am asking the user for location permission before making any location related operation" – ADev Feb 07 '18 at 15:24