在我的应用程序中使用两个不同版本的 okhttp 库是否可以?

Is it OK to use two different versions of okhttp library in my app?

在我的应用程序中,我使用 latest version of the okhttp library,将其添加到我的应用程序的依赖项中:

compile 'com.squareup.okhttp3:okhttp:3.9.1'

但是,我现在正在向我的应用程序添加另一个第三方库(称为 Mintegrate),它需要 okhttp 的 v2.7.5:

compile 'com.squareup.okhttp:okhttp:2.7.5'

在我的应用的 build.gradle 中同时拥有这两个依赖项是否可以?

PS - 我曾尝试省略 v2.7.5,但当我这样做时,应用程序崩溃并出现此错误:

ClassNotFoundException: Didn't find class com.squareup.okhttp.OkHttpClient

您可以在 build.gradle 中使用 resolutionStrategy 强制依赖,例如

configurations.all {
    resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.9.1'
}

dependencies {
 .....
}

出于完全相同的原因(第 3 方库),我还在我们的应用程序中包含了两个版本,并且它可以正常工作。正如@Héctor 在他的评论中所说,包不同。参见 Jake's Wharton statement on that