无法在离线模式下构建。“没有 androidx.fragment:fragment:[1.2.0] 可用于离线模式的缓存版本列表”

dug*_*ous 5 android android-studio

我希望能够离线处理我的项目。所以我禁用离线模式,按“同步项目与 gradle 文件”按钮,然后启用离线模式并尝试构建。但是,每次我这样做时,都会收到错误消息:

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. 
Run Code Online (Sandbox Code Playgroud)

我还尝试在启用离线模式之前使缓存/重启无效,然后 gradle 同步,结果相同。

我正在运行 Android Studio 3.6.3

gradle-wrapper.properties 包含以下内容:

distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
Run Code Online (Sandbox Code Playgroud)

如何使 androidx.fragment:fragment 可用于离线模式?

小智 0

很可能存在依赖性解析问题。当多个组件使用相同的依赖项但版本不同时,Gradle 会上网找出要使用的版本。这显然在离线模式下不起作用。

我假设您在其他地方使用了不同的片段依赖项 ( androidx.fragment:fragment),该依赖项在在线模式下默认选择。现在在离线模式下,由于不可能进行此类检查,Gradle 会查找版本1.2.0( 所需的androidx.fragment:fragment-ktx:1.2.0),但找不到它,因为 if 从未下载/缓存。

更好的解决方案是尝试使您的依赖项引用冲突的子依赖项的相同版本。您可以检查依赖关系树并./gradlew app:dependencies更新冲突的依赖关系,以便它们使用相同的版本(如果可能)。

另一个解决方案是在 Gradle 中强制执行依赖关系。您可以将这样的内容添加到您的build.gradle文件中:

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'
        }
    }
}

Run Code Online (Sandbox Code Playgroud)