我有两个独立的 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依赖项,我看到的是,在应用程序1中,正在运行的应用程序,gradle解决了冲突,并对所有子依赖项使用okhttp:3.11.0,而在应用程序2中,gradle无法解决冲突冲突。
我设法解决此冲突的唯一方法是降级mockwebserver的依赖声明,更改
androidTestImplementation("com.squareup.okhttp3:mockwebserver:3.11.0")
到
androidTestImplementation("com.squareup.okhttp3:mockwebserver:3.10.0")
这里有什么问题吗?为什么它在一个应用程序中有效,而在另一个应用程序中无效?
MockWebServer 和 Retrofit 的 gradle 版本都使用了 okhhtp。您需要匹配兼容版本。
它最有可能在一个应用程序中工作但在另一个应用程序中不起作用的原因可能是缓存了有效版本,或者是不同的包(例如 okio)需要有效版本。
您需要降级mockwebserver或更新改造
这是因为与 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'
希望对您有帮助。