[Fixed] ANR Error Updating com.google.android.gms:play-services-location to 21.0.1 – Android

by
Ali Hasan
android android-gradle-plugin firebase google-play-services

Quick Fix: Replace the latest version of Play Services Location (21.0.1) with a more stable version like 20.0.0 to resolve ANR errors and ensure better stability.

The Problem:

A user updated the dependency ‘com.google.android.gms:play-services-location’ from version 20.0.0 to 21.0.1 in an Android 33 project. This triggered an ANR error when the user opened a specific screen within the app. The issue appears to be linked to a text field for inputting a phone number, which previously requested focus and functioned correctly. Reverting the dependency back to version 20.0.0 resolved the issue.

The Solutions:

Solution 1: Downgrading the play-services-location dependency

Downgrading the play-services-location dependency from 21.0.1 to 20.0.0 resolved the ANR issue. This suggests that there might be a compatibility issue with the latest version of the library and your project setup. By reverting to the previous stable version, you can restore the stability and functionality of your app.

  1. In your project-level build.gradle file, locate the dependencies section where you have declared the play-services-location dependency.

  2. Change the version number from 21.0.1 to 20.0.0:

    dependencies {
        implementation 'com.google.android.gms:play-services-location:20.0.0'
    }
    
  3. Rebuild your project to apply the changes.

By downgrading the dependency, you can ensure that your app continues to function without encountering the ANR error. However, it’s worth keeping an eye on future releases of the play-services-location library to see if the issue is resolved in later versions.

Solution 2: Update Deprecated API

In the updated version of Play Services Location Library (21.0.1), the LocationServices.FusedLocationApi.getLastLocation() method has been deprecated. Using this deprecated API can lead to ANR (Application Not Responding) errors.

To resolve this issue, update your code to use the new API for requesting the last known location:

LocationServices.getFusedLocationProviderClient(context).getLastLocation();

This new API is not deprecated and should not cause ANR errors. Make sure to update your code to use the new API as soon as possible.

Q&A

What is causing the ANR error?

The ANR error is caused by updating the dependency "com.google.android.gms:play-services-location" to version 21.0.1.

What can be done to fix the issue?

The issue can be fixed by reverting the dependency back to version 20.0.0.

Is there any other way to fix the issue without reverting the dependency?

It is also possible to fix the issue by updating to the new API for requesting location.

Video Explanation:

The following video, titled "Failed to resolve com.google.firebase: firebase-core 10.0.1 - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... fix: Error: fix the version conflict (google-services plugin) (Android Studio). Failed to resolve com.google.firebase: firebase-core 10.0.1.