[英]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:我在离线模式下使用 gradle 遇到了一个问题:当离线未选中时,我的项目运行良好,然后,我需要离线工作(因为我正在坐火车),并且在不接触任何 gradle 文件的情况下,这抛出错误:
> 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?
我该怎么做才能在没有任何互联网连接的情况下构建这个项目?
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 .该问题可能与
com.google.android.gms:play-services-basement
依赖项的冲突版本有关,这些依赖项是来自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.下面的步骤描述了如何使用
com.google.android.gms:play-services-ads-identifier
(在下面的示例中使用)依赖项来诊断相同的问题。
"app" is used as an example, so replace the app with actual name of your app_module. “app”用作示例,因此将应用程序替换为您的 app_module 的实际名称。
Prerequisites (initial problematic case):先决条件(最初有问题的情况):
My build.gradle has the following:我的build.gradle有以下内容:
...
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.取消选择离线工作时,运行 Gradle 同步工作正常。 But as soon as I turn ON Offline work , the following error pops up during gradle sync:
但是一旦我打开Offline work ,在 gradle 同步过程中就会弹出以下错误:
: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:诊断步骤:
./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.意味着存在冲突,并且play-services-ads-identifier 的版本已解析为 17.0.0,而最初请求的是 15.0.1。
Since I know that those 2 dependencies are my top-level dependencies (ie are explicitly specified in build.gradle ).因为我知道这两个依赖项是我的顶级依赖项(即在build.gradle中明确指定)。 Now I have the following options to solve the problem.
现在我有以下选项来解决这个问题。
Solution 1解决方案1
I can avoid versions conflict eg 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.我可以避免版本冲突,例如通过在应用程序的build.gradle中将
com.google.android.gms:play-services-ads:15.0.1
升级到com.google.android.gms:play-services-ads:17.0.0
模块。 Doing this allows Gradle to sync successfully when Offline work is selected.这样做可以让 Gradle 在选择离线工作时成功同步。
Solution 2解决方案2
Keep dependency but exclude conflicting modules.保持依赖但排除冲突的模块。 I. e.
IE。 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.这使得 Gradle 也可以离线工作。
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.注意:在您的情况下,可能会在上面显示的dependencyInsight命令的树状输出中更深地找到导致冲突的源依赖项。
Environment I'm using:我使用的环境:
Android Studio v3.5.2 on macOS, Gradle plugin v3.5.2, Gradle v5.6.4 macOS 上的 Android Studio v3.5.2、Gradle 插件 v3.5.2、Gradle v5.6.4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.