Mockwebserver in gradle build throwing error

痴心易碎 提交于 2020-01-02 03:33:06

问题


This entry in my gradle file :

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

is throwing error:

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.

I tried commenting out different compile entries in my gradle file to find out which one was conflicting but I just can't find which one uses com.squareup.okio:okio.

UPDATE: I was able to get the dependencies by running: 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

So as you can see, retrofit 2.0 uses okhttp3 which uses okio:1.8.0. On the other hand mockwebserver:2.7.0 uses okhttp:2.7.0 which uses okio:1.6.0. So how can I resolve this?

Here are the entries in "dependencies" section of my gradle file:

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:


I solved by using

Retrofit version 2.3.0 -> com.squareup.retrofit2:retrofit:2.3.0

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



回答2:


try exclude okio from androidTestCompile

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



回答3:


When instrumentation tests are run, both the main APK and test APK share the same classpath. Gradle build will fail if the main APK and the test APK use the same library (e.g. Guava) but in different versions. If gradle didn't catch that, your app could behave differently during tests and during normal run (including crashing in one of the cases).

ref - https://sites.google.com/a/android.com/tools/tech-docs/new-build-system/user-guide#TOC-Resolving-conflicts-between-main-and-test-APK

Which means dependencies in each configuration i.e compile, androidTestCompile & testCompile should be of the same versions.

Resolving the conflict

Update parent dependencies(retrofit & mockwebserver) such that they all share the same child dependencies(okhttp & okio) across configurations.

OR

Explicitly add the dependency in the configuration(compile, androidTestCompile or testCompile) that has the least version.

In your case adding androidTestCompile('com.squareup.okio:okio:1.6.0') should resolve the conflict.
Now each androidTest dependency that needs okio will use the latest version.

Note
Whenever there are version conflicts within the same configuration, the latest available version of the dependency is automatically used. This is not the case for version conflicts across configurations, hence the error.

example - Here in my compile configuration okhttp3 dependencies are replaced with the latest version of 3.9.0 whenever there is a conflict.




回答4:


The dependency versions for these two versions of Retrofit and MockWebServer line up:

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

Rather than deal with dependency conflicts, I'd recommend using these two versions.



来源:https://stackoverflow.com/questions/42218723/mockwebserver-in-gradle-build-throwing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!