Gradle构建中的Mockwebserver抛出错误

7

我的Gradle文件中的这个条目:

androidTestCompile ('com.squareup.okhttp:mockwebserver:2.7.0')

出现错误:

Warning:Conflict with dependency 'com.squareup.okio:okio'. Resolved versions for app (1.8.0) and test app (1.6.0) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

我尝试注释掉gradle文件中不同的编译条目,以找出哪个条目与com.squareup.okio:okio发生冲突,但我无法确定具体是哪一个。

更新: 通过运行命令:gradlew.bat app:dependencies > c:\tmp\output.txt,我成功地获取了依赖关系。

+--- com.squareup.retrofit2:retrofit:2.0.0 -> 2.1.0
|    \--- com.squareup.okhttp3:okhttp:3.3.0
|         \--- com.squareup.okio:okio:1.8.0


--- com.squareup.okhttp:mockwebserver:2.7.0
|    +--- com.squareup.okhttp:okhttp:2.7.0
|    |    \--- com.squareup.okio:okio:1.6.0

如您所见,Retrofit 2.0 使用的是 OkHttp3,而 OkHttp3 又使用了 Okio:1.8.0。另一方面,MockWebServer:2.7.0 使用的是 OkHttp:2.7.0,它使用了 Okio:1.6.0。那么我该如何解决呢?

以下是我的 Gradle 文件中“dependencies”部分的条目:

compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:24.2.1'


        //retrofit
        compile 'com.squareup.retrofit2:retrofit:2.0.0'
        compile 'com.squareup.retrofit2:converter-gson:2.+'
        compile 'com.squareup.retrofit2:adapter-rxjava:2.+'
        compile 'com.squareup.retrofit2:retrofit-mock:2.+'



        //recycler view
        compile 'com.android.support:recyclerview-v7:+'

        //picasso image caching
        compile 'com.squareup.picasso:picasso:2.5.2'


        //jackson parser
        compile (
            [group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.4.1']
        )

        //Dagger
        compile 'com.google.dagger:dagger:2.7'
        apt 'com.google.dagger:dagger-compiler:2.7'

        //constraint based layouts
        compile 'com.android.support:design:24.1.1'
        compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'

        //for chrome debugging
        compile 'com.facebook.stetho:stetho:1.4.1'
        compile 'com.facebook.stetho:stetho-okhttp3:1.4.1' //for retrofit

        //RxJava
        compile 'io.reactivex:rxandroid:1.2.1'
        // Because RxAndroid releases are few and far between, it is recommended you also
        // explicitly depend on RxJava's latest version for bug fixes and new features.
        compile 'io.reactivex:rxjava:1.1.6'


        //--- For Testing ---
        //robolectric:
        testCompile "org.robolectric:robolectric:3.2.2"

        //mockito
        testCompile "org.mockito:mockito-core:2.+"
        testCompile('org.hamcrest:hamcrest-core:1.3')
        testCompile('org.hamcrest:hamcrest-library:1.3')

        testCompile 'junit:junit:4.12'

        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })

        // Espresso-web for WebView support
        androidTestCompile( 'com.android.support.test.espresso:espresso-web:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })

        androidTestCompile( 'com.android.support.test:runner:0.5', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        androidTestCompile( 'com.android.support.test:rules:0.5', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })

        testCompile ('org.powermock:powermock-api-mockito:1.6.2') {
            exclude module: 'hamcrest-core'
            exclude module: 'objenesis'
        }

        //mockwebserver
        //testCompile 'com.squareup.okhttp3:mockwebserver:3.3.0'
        androidTestCompile ('com.squareup.okhttp:mockwebserver:2.7.0')
        androidTestCompile 'com.squareup.spoon:spoon-client:1.2.0'

1
你可以通过在Windows终端中使用gradlew.bat app:dependencies获取依赖树来解决问题。在Mac上使用./gradlew - Raghunandan
@Raghunandan - 感谢您的提示,既然现在看到了依赖关系,您有什么修复的建议吗? - Mike6679
4个回答

4
我使用以下方法解决了问题:
Retrofit version 2.3.0 -> com.squareup.retrofit2:retrofit:2.3.0

MockWebServer version 3.8.0 -> com.squareup.okhttp3:mockwebserver:3.8.0

0
当运行仪器测试时,主 APK 和测试 APK 共享相同的类路径。如果主 APK 和测试 APK 使用不同版本的相同库(例如 Guava),Gradle 构建将失败。如果 Gradle 没有捕获到这一点,您的应用程序在测试和正常运行期间可能会表现出不同的行为(包括在某些情况下崩溃)。
参考 - https://sites.google.com/a/android.com/tools/tech-docs/new-build-system/user-guide#TOC-Resolving-conflicts-between-main-and-test-APK 这意味着每个配置中的依赖项,即compileandroidTestCompiletestCompile,应该是相同版本的。
解决冲突
更新父依赖项(retrofitmockwebserver),使它们在所有配置中共享相同的子依赖项(okhttpokio)。
或者

在配置文件中明确添加依赖项(compileandroidTestCompiletestCompile),并选择最低版本。

在您的情况下,添加androidTestCompile('com.squareup.okio:okio:1.6.0')应该解决冲突。
现在,每个需要okioandroidTest依赖项都将使用最新版本。

注意
当同一配置中存在版本冲突时,自动使用可用的最新版本。但是,跨配置的版本冲突不适用于此规则,因此会出现错误。

例如 - 在我的编译配置中,每当发生冲突时,okhttp3依赖项都会被替换为最新版本3.9.0

enter image description here


0

这两个版本的RetrofitMockWebServer的依赖版本是相同的:

com.squareup.retrofit2:retrofit:2.2.0 
com.squareup.okhttp3:mockwebserver:3.6.0   

与其处理依赖冲突,我建议使用这两个版本。


0

尝试从androidTestCompile中排除okio

androidTestCompile ('com.squareup.okhttp:mockwebserver:2.7.0') {
    exclude module: 'com.squareup.okio'
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接