5

I'd like to be able to work on my project offline. So I disable offline mode, press the "sync project with gradle files" button, then enable offline mode and try to build. However, every time I do, I get the error:

FAILURE: Build failed with an exception.

*What went wrong:

Could not determine the dependencies of task ':app:compileDebugRenderscript'.
  > Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
      > Could not resolve androidx.fragment:fragment:[1.2.0].
        Required by
           project :app > androidx.fragment:fragment-ktx:1.2.0
               > No cached version listing for androidx.fragment:fragment:[1.2.0] available for offline mode.
               > No cached version listing for androidx.fragment:fragment:[1.2.0] available for offline mode. 

I've also tried invalidate cache/restart and then gradle sync before enabling offline mode, with the same result.

I'm running Android Studio 3.6.3

gradle-wrapper.properties contains the fololowing:

distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip

How can I make androidx.fragment:fragment available for offline mode?

duggulous
  • 2,427
  • 3
  • 23
  • 40
  • 1
    Hope you have gone through all the solutions mentioned here https://stackoverflow.com/questions/22607661/no-cached-version-available-for-offline-mode – AgentP Jun 03 '20 at 11:08
  • Does a full build instead of a Gradle Sync prepare the cache properly? – tynn Jun 04 '20 at 09:28

1 Answers1

0

There is most likely a dependency resolution problem. When multiple components use the same dependency but with a different version, Gradle goes online to figure out which version to use. That obviously doesn't work in offline mode.

I assume you're using a different fragment dependency (androidx.fragment:fragment) somewhere else, that is selected by default in online mode. Now in offline mode, since no such inspection is possible, Gradle looks for version 1.2.0 (needed by androidx.fragment:fragment-ktx:1.2.0) and can't find it because if was never downloaded/cached.

Better solution is to try making your dependencies refer to the same versions of conflicting sub-dependencies. You can inspect your dependency tree with ./gradlew app:dependencies and update dependencies that are clashing so they use the same versions (if possible).

The other solution is to enforce dependencies in Gradle. You can add something like this to your build.gradle file:

configurations.all {
    resolutionStrategy {
        if (project.gradle.startParameter.offline) {
            // x.y.z being the version that Gradle picks in the online mode.
            force 'androidx.fragment:fragment:x.y.z'
        }
    }
}

chef417
  • 494
  • 2
  • 7