Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle offline sync issue

I'm facing an issue using gradle in offline mode: I'm running my project fine when offline is uncheked, then, I need to work offline (because I'm taking the train), and, without touching any gradle file, this error is thrown:

 > Could not resolve com.google.android.gms:play-services-basement:[15.0.1,16.0.0).
 Required by:
     project :app > com.google.android.gms:play-services-ads:15.0.1
     project :app > com.google.android.gms:play-services-ads-lite:15.0.1
     project :app > com.google.android.gms:play-services-gass:15.0.1
  > No cached version listing for com.google.android.gms:play-services-basement:[15.0.1,16.0.0) available for offline mode.

Why this dependency was not cached during the last online sync? how can I do to be able to build this project without any internet connection?

like image 807
Thibault Francois Avatar asked Mar 08 '26 17:03

Thibault Francois


1 Answers

The problem might be related to conflicting versions of com.google.android.gms:play-services-basement dependency implicitly required by more than one higher-level dependencies came from your build.gradle.

The steps below describe how to diagnose the same issue but with com.google.android.gms:play-services-ads-identifier (used in examples below) dependency.

"app" is used as an example, so replace the app with actual name of your app_module.

Prerequisites (initial problematic case):

My build.gradle has the following:

...
implementation 'com.google.android.gms:play-services-ads:15.0.1'
implementation 'com.google.android.gms:play-services-analytics:17.0.0'
...

running Gradle sync works fine when Offline work is unselected. But as soon as I turn ON Offline work, the following error pops up during gradle sync:

:app@debug/compileClasspath': Could not resolve com.google.android.gms:play-services-ads-identifier:[15.0.1,16.0.0).
Disable offline mode and sync project
Show Details
Affected Modules: app
...

Steps to diagnose:

  1. Print all the places where play-services-ads-identifier dependency is referenced from
./gradlew :app:dependencyInsight --configuration releaseRuntimeClasspath --dependency "com.google.android.gms:play-services-ads-identifier"

In my case among all of the output I see the following lines:

...
com.google.android.gms:play-services-ads-identifier:17.0.0
+--- com.google.android.gms:play-services-analytics-impl:17.0.0
|    +--- com.google.android.gms:play-services-analytics:17.0.0 (requested com.google.android.gms:play-services-analytics-impl:[17.0.0])
|    |
...
...
com.google.android.gms:play-services-ads-identifier:[15.0.1,16.0.0) -> 17.0.0
\--- com.google.android.gms:play-services-ads:15.0.1
     +--- releaseRuntimeClasspath
...

The line

com.google.android.gms:play-services-ads-identifier:[15.0.1,16.0.0) -> 17.0.0

means that there was a conflict and the version of play-services-ads-identifier was resolved to 17.0.0 while initially 15.0.1 was requested.

  1. The next and most important things to look at are where play-services-ads-identifier was referenced from. This is specified in the output the next line after the name of the dependency, in my specific case those sources are
    1. com.google.android.gms:play-services-analytics-impl:17.0.0
    2. com.google.android.gms:play-services-ads:15.0.1

Since I know that those 2 dependencies are my top-level dependencies (i. e. are explicitly specified in build.gradle). Now I have the following options to solve the problem.

Solution 1
I can avoid versions conflict e. g. by upgrading com.google.android.gms:play-services-ads:15.0.1 to com.google.android.gms:play-services-ads:17.0.0 in build.gradle in the app module. Doing this allows Gradle to sync successfully when Offline work is selected.

Solution 2
Keep dependency but exclude conflicting modules. I. e. change

implementation 'com.google.android.gms:play-services-ads:15.0.1'

to

implementation ('com.google.android.gms:play-services-ads:15.0.1') {
    exclude group: 'com.google.android.gms', module: 'play-services-ads-identifier'
}

After doing this, I'll face few more sync errors since few more underlaying dependencies have conflicts. As a result, I'll end up with the following:

implementation ('com.google.android.gms:play-services-ads:15.0.1') {
    exclude group: 'com.google.android.gms', module: 'play-services-ads-identifier'
    exclude group: 'com.google.android.gms', module: 'play-services-base'
    exclude group: 'com.google.android.gms', module: 'play-services-basement'
}

This makes Gradle work offline as well.

NOTE: In your case the source dependencies that cause the conflict may be found deeper in a tree-like output of the dependencyInsight command showed above.


Environment I'm using:
Android Studio v3.5.2 on macOS, Gradle plugin v3.5.2, Gradle v5.6.4

like image 120
vladZams Avatar answered Mar 11 '26 07:03

vladZams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!