使用“implementation”时发生 Gradle 依赖冲突

tir*_*r38 2 gradle

我有一个包含两个 gradle 模块的项目:libapp。我刚刚更改了libbuild.gradle 以停止公开依赖项(即我从api->移动implementation)。该app模块并不直接依赖于 OkHttp 日志拦截器,因此我认为最好不要公开它。

lib模块的build.gradle:

dependencies {
    // api 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    ...
}
Run Code Online (Sandbox Code Playgroud)

app模块的build.gradle:

dependencies {
    implementation project(':lib')
    implementation group: 'com.zendesk', name: 'support-providers', version: '2.0.0'
    ...
}
Run Code Online (Sandbox Code Playgroud)

但是我现在看到一个编译问题:

与项目“:app”中的依赖项“com.squareup.okhttp3:logging-interceptor”冲突。运行时类路径 (3.10.0) 和编译类路径 (3.8.1) 的解析版本不同

如果我查看项目结构,我会看到以下内容:

+--- project :lib
...
+--- com.zendesk:support-providers:2.0.0
|    +--- com.zendesk:core:1.0.0
|    |    +--- com.zendesk:java-common:1.13
|    |    +--- com.google.dagger:dagger:2.12 -> 2.15 (*)
|    |    +--- com.squareup.retrofit2:retrofit:2.3.0
|    |    |    \--- com.squareup.okhttp3:okhttp:3.8.0 -> 3.8.1
|    |    |         \--- com.squareup.okio:okio:1.13.0
|    |    +--- com.squareup.retrofit2:converter-gson:2.3.0
|    |    |    +--- com.squareup.retrofit2:retrofit:2.3.0 (*)
|    |    |    \--- com.google.code.gson:gson:2.7
|    |    +--- com.squareup.okhttp3:logging-interceptor:3.8.1 // <----- SEE HERE
|    |    |    \--- com.squareup.okhttp3:okhttp:3.8.1 (*) 
|    |    +--- com.squareup.okhttp3:okhttp:3.8.1 (*)
|    |    +--- com.android.support:support-annotations:27.0.2 -
Run Code Online (Sandbox Code Playgroud)

lib没有透露任何依赖项(显然),并且app依赖于 Zendesk sdk,而 Zendesk sdk 又依赖于不同版本的 OkHttp 日志拦截器。

我只看到两种方法来解决这个问题:

  1. api -> implementation在模块中恢复lib,从而将日志拦截器暴露给app模块
  2. 声明对日志记录拦截器的顶级依赖项并设置为 3.10 以强制 Zendesk 使用最新版本:

app构建.gradle:

dependencies {
    implementation project(':lib')
    implementation group: 'com.zendesk', name: 'support-providers', version: '2.0.0'

    // used just to force zendesk to use 3.10
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0' 
    ...
}
Run Code Online (Sandbox Code Playgroud)

这些看起来都不干净。IMO 该app模块不应该了解 OkHttp 日志拦截器的任何信息。还有其他选择吗?

如果 Zendesk 更新他们的库以用于implementationOkHttp 依赖项,这会解决问题吗?Gradle 是否会允许两个依赖项使用同一传递依赖项的不同版本,只要它们不向整个项目公开?

Mar*_*ler 5

对于 Gradle,这有点不同......

人们可以强制执行该版本3.10.0

dependencies {
    implementation group: 'com.zendesk', name: 'support-providers', version: '2.0.0'
}

configurations.all() {
    resolutionStrategy.force "com.squareup.okhttp3:logging-interceptor:3.10.0"
}
Run Code Online (Sandbox Code Playgroud)

或者只是排除版本3.8.1(除了所需的版本之外什么都没有留下3.10.0):

dependencies {
    implementation ('com.zendesk:support-providers:2.0.0") {
        exclude "com.squareup.okhttp3:logging-interceptor:3.8.1"
    }
}
Run Code Online (Sandbox Code Playgroud)