[expo-location] checkLocationSettings in resolveUserSettingsForRequest failing due to ClassCastException

Please provide the following:

  1. SDK Version: 49.0.10
  2. Platforms(Android/iOS/web/all): Android
  3. Add the appropriate “Tag” based on what Expo library you have a question on.

npmPackages:
expo: 49.0.10 => 49.0.10
react: 18.2.0 => 18.2.0
react-dom: 18.2.0 => 18.2.0
react-native: 0.72.5 => 0.72.5
Expo Workflow: managed

Summary

I am using expo-location in my app and have seen a small number of users have repetitive crashes logged in crashlytics with the following stack trace:

Fatal Exception: java.lang.ClassCastException: com.google.android.gms.common.api.ApiException cannot be cast to com.google.android.gms.common.api.ResolvableApiException
       at expo.modules.location.LocationModule.lambda$resolveUserSettingsForRequest$16$expo-modules-location-LocationModule(LocationModule.java:668)
       at expo.modules.location.LocationModule$$ExternalSyntheticLambda12.onFailure(:4)
       at com.google.android.gms.tasks.zzk.run(com.google.android.gms:play-services-tasks@@18.0.2:1)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8751)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

I have not been able to replicate this issue.

Potential cause

The code it references at expo-location LocationModule.java:668 is very similar to that in this issue: https://issuetracker.google.com/issues/255798723

Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

    task.addOnSuccessListener(locationSettingsResponse -> {
      // All location settings requirements are satisfied.
      executePendingRequests(Activity.RESULT_OK);
    });

    task.addOnFailureListener(e -> {
      int statusCode = ((ApiException) e).getStatusCode();

      if (statusCode == CommonStatusCodes.RESOLUTION_REQUIRED) {
        // Location settings are not satisfied, but this can be fixed by showing the user a dialog.
        // Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().

        try {
          ResolvableApiException resolvable = (ResolvableApiException) e;

          mUIManager.registerActivityEventListener(LocationModule.this);
          resolvable.startResolutionForResult(activity, CHECK_SETTINGS_REQUEST_CODE);
        } catch (IntentSender.SendIntentException sendEx) {
          // Ignore the error.
          executePendingRequests(Activity.RESULT_CANCELED);
        }
      } else {// Location settings are not satisfied. However, we have no way to fix the settings so we won't show the dialog.
        executePendingRequests(Activity.RESULT_CANCELED);
      }
  • What was the expected result?
    if exception.statusCode = LocationSettingsStatusCodes.RESOLUTION_REQUIRED
    will be cast exception as ResolvableApiException

  • What was the actual result?
    exception is not ResolvableApiException

Dependency versions

  • “expo”: “49.0.10”
  • “expo-location”: “16.1.0”

Expo-location has an explicit dependency on ‘com.google.android.gms:play-services-location:20.0.0’ and so I would expect not to see this problem, as my understanding is that the bug was introduced in “play-services-location:21.0.0”

Have I missed something?
Is it possible for users’ devices to be running “play-services-location:21.0.0” despite the explicit dependency, and that cause the issue?

I am cross-posting this to Google Issue Tracker: https://issuetracker.google.com/issues/303285489

Hi did you manage to find a solution? I am experiencing the same problem unfortunately!!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.