将mockwebserver添加到android项目时发生依赖项冲突

n53p2ov0  于 2023-11-15  发布在  Android
关注(0)|答案(2)|浏览(126)

我有两个独立的android项目,我在其中实现了集成测试,我完成了对第一个项目的集成测试,我正在转移到我的第二个项目,但是我在做设置和添加mockwebserver依赖时遇到了一个问题。
顶级gradle文件(两个应用程序相同)

buildscript {
ext.kotlin_version = '1.3.21'
repositories {
    google()
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.3.2'
    classpath 'com.google.gms:google-services:4.2.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'io.fabric.tools:gradle:1.+'
}
}
allprojects {
    repositories {
        google()
        jcenter()
        maven { url "http://tokbox.bintray.com/maven" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

字符串
应用程序1的应用程序gradle文件(工作)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'https://jitpack.io' }
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    mavenCentral()
    jcenter()
}

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.myApps.appone"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "0.9"
        testInstrumentationRunner "com.myApps.CustomTestRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            applicationIdSuffix ".dev"
            versionNameSuffix '-DEBUG'
            minifyEnabled false              
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            def requested = details.requested
            if (requested.group == "com.android.support") {
                if (!requested.name.startsWith("multidex")) {
                    details.useVersion "26.+"
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(":domain")
    implementation project(":data")

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:28.0.0'

    androidTestImplementation("com.squareup.okhttp3:mockwebserver:3.11.0")
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
    androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'org.mockito:mockito-core:2.5.0'
    androidTestImplementation('com.android.support.test.espresso:espresso-contrib:2.0') {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }

    kaptAndroidTest 'com.google.dagger:dagger-compiler:2.16'

    kapt 'com.jakewharton:butterknife-compiler:8.8.1'
    implementation 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'
    implementation 'com.android.support.test.espresso:espresso-idling-resource:3.0.2'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.makeramen:roundedimageview:2.2.1'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    implementation 'com.google.android.gms:play-services-places:16.0.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'com.google.maps.android:android-maps-utils:0.5+'
    implementation 'com.google.maps:google-maps-services:0.1.20'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation "io.reactivex.rxjava2:rxandroid:2.1.0"
    implementation 'com.android.support:multidex:1.0.3'
    implementation "android.arch.lifecycle:extensions:1.1.1"
    implementation "android.arch.lifecycle:viewmodel:1.1.1"
    kapt "android.arch.lifecycle:compiler:1.1.1"
    kapt 'com.google.dagger:dagger-compiler:2.16'
    kapt "com.google.dagger:dagger-android-processor:2.16"

    //testImplementation
    testImplementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.11'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.0.6'
    testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
    testImplementation 'android.arch.core:core-testing:1.1.1'

    //Firebase
    implementation 'com.google.firebase:firebase-core:16.0.6'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'
    implementation 'com.google.firebase:firebase-invites:16.0.6'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'

}

apply plugin: 'com.google.gms.google-services'


应用程序2的应用程序gradle文件(错误)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: "kotlin-kapt"
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'https://jitpack.io' }
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    mavenCentral()
    jcenter()
}

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.myApps.apptwo"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 22
        versionName "2.0.3"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            versionNameSuffix '-DEBUG'
            minifyEnabled false               
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    kapt {
        generateStubs = true
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(":domain")
    implementation project(":data")

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'

    androidTestImplementation("com.squareup.okhttp3:mockwebserver:3.11.0")

    kaptAndroidTest 'com.google.dagger:dagger-compiler:2.16'

    implementation 'com.android.support:cardview-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    kapt 'com.jakewharton:butterknife-compiler:8.8.1'

    implementation 'com.squareup.picasso:picasso:2.71828'

    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation "io.reactivex.rxjava2:rxandroid:2.1.0"

    implementation 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'
    implementation 'com.google.android.gms:play-services-maps:16.0.0'
    implementation 'com.google.android.gms:play-services-places:16.0.0'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'com.google.maps.android:android-maps-utils:0.5+'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:multidex:1.0.3'
    def nav_version = "1.0.0-alpha06"
    implementation 'com.android.support:design:28.0.0'
    implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
    // use -ktx for Kotlin
    implementation "android.arch.navigation:navigation-ui-ktx:$nav_version" // use -ktx for Kotlin
    implementation "android.arch.lifecycle:extensions:1.1.1"
    implementation "android.arch.lifecycle:viewmodel:1.1.1"

    kapt "android.arch.lifecycle:compiler:1.1.1"
    kapt 'com.google.dagger:dagger-compiler:2.16'
    kapt "com.google.dagger:dagger-android-processor:2.16"

    //Firebase
    implementation 'com.google.firebase:firebase-core:16.0.6'
    implementation 'com.google.firebase:firebase-messaging:17.3.4'

    implementation('com.crashlytics.sdk.android:crashlytics:2.9.7@aar') {
        transitive = true
    }
}

apply plugin: 'com.google.gms.google-services'


抛出的错误是这样的:

Cannot find a version of 'com.squareup.okhttp3:okhttp' that satisfies the version constraints: 
   Dependency path 'apptwo:app:unspecified' --> 'com.squareup.okhttp3:mockwebserver:3.11.0' --> 'com.squareup.okhttp3:okhttp:3.11.0'
   Dependency path 'apptwo:app:unspecified' --> 'com.squareup.picasso:picasso:2.71828' --> 'com.squareup.okhttp3:okhttp:3.10.0'
   Dependency path 'apptwo:app:unspecified' --> 'com.squareup.retrofit2:retrofit:2.4.0' --> 'com.squareup.okhttp3:okhttp:3.10.0'
   Dependency path 'apptwo:app:unspecified' --> 'apptwo:data:unspecified' --> 'com.squareup.okhttp3:logging-interceptor:3.8.1' --> 'com.squareup.okhttp3:okhttp:3.8.1'
   Dependency path 'apptwo:app:unspecified' --> 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' --> 'com.squareup.retrofit2:retrofit:2.4.0' --> 'com.squareup.okhttp3:okhttp:3.10.0'
   Constraint path 'apptwo:app:unspecified' --> 'com.squareup.okhttp3:okhttp' strictly '3.10.0' because of the following reason: apptwoDevelopmentDebugRuntimeClasspath uses version 3.10.0
   Constraint path 'apptwo:app:unspecified' --> 'com.squareup.okhttp3:okhttp' strictly '3.10.0' because of the following reason: apptwoDevelopmentDebugRuntimeClasspath uses version 3.10.0
   Constraint path 'apptwo:app:unspecified' --> 'com.squareup.okhttp3:okhttp' strictly '3.10.0' because of the following reason: apptwoDevelopmentDebugRuntimeClasspath uses version 3.10.0


我运行了./gradlew dependencies,我看到的是,在app 1中,gradle解决了冲突,并为所有子依赖使用okhttp:3.11.0,而在app 2中,gradle无法解决冲突。
我设法解决这个冲突的唯一方法是降级mockwebserver的依赖声明,

androidTestImplementation("com.squareup.okhttp3:mockwebserver:3.11.0")


androidTestImplementation("com.squareup.okhttp3:mockwebserver:3.10.0")


这里的问题是什么?为什么它在一个应用程序中工作,而在另一个应用程序中不工作?

zengzsys

zengzsys1#

MockWebServer和Retrofit的gradle版本都使用okhhtp。您需要匹配兼容的版本。
为什么它很可能在一个应用程序中工作,而不是其他的原因可能是一个有效的版本要么被缓存,要么是需要一个不同的包,如okio。
您将需要降级mockwebserver或更新改造

vngu2lb8

vngu2lb82#

这是因为与OkHttp发生冲突。要解决它,请在模块的build.gradle中添加OkHttp-bom,如下所示:

implementation(platform("com.squareup.okhttp3:okhttp-bom:4.12.0"))

字符串
然后添加OkHttp依赖不带版本号

implementation 'com.squareup.okhttp3:okhttp'


然后添加MockWebServer依赖项:

implementation 'com.squareup.okhttp3:mockwebserver:4.9.0'


我希望这会有所帮助。

相关问题