将 espresso-contrib 添加到 androidTestImplementation 会产生依赖/约束错误



我正在关注PluralSight上的一个旧教程:增强Android应用程序体验。由于我使用的是较新版本的IDE,因此遇到了一些问题。我可以在SO修复大多数问题,但是我被困住了,并且没有足够的经验来了解我所知道的。

在设置一些单元测试时,我不得不将androidTestImplementation ('com.android.support.test.espresso:espresso-contrib:3.0.2'添加到gradle文件中。教官回避了这一点,造成了一些问题,因为

espresso-contrib 库实际上依赖于某些类的不同版本,而不是我们在应用程序中使用的版本。原因请记住,gradle 负责解决下游链依赖关系

模块中的错误体验不是我在下面遇到的错误。通过在 TestCompile 中添加对库的显式调用来修复它们。我做同样的事情并不能解决问题。

Cannot find a version of 'com.android.support:appcompat-v7' that satisfies the version constraints: 
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Constraint path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:{strictly 24.2.1}' because of the following reason: debugRuntimeClasspath uses version 24.2.1
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Constraint path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:{strictly 24.2.1}' because of the following reason: debugRuntimeClasspath uses version 24.2.1
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Constraint path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:{strictly 24.2.1}' because of the following reason: debugRuntimeClasspath uses version 24.2.1
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Constraint path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:{strictly 24.2.1}' because of the following reason: debugRuntimeClasspath uses version 24.2.1
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Constraint path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:{strictly 24.2.1}' because of the following reason: debugRuntimeClasspath uses version 24.2.1
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Constraint path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:{strictly 24.2.1}' because of the following reason: debugRuntimeClasspath uses version 24.2.1
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:design:27.1.1' --> 'com.android.support:appcompat-v7:27.1.1'
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Constraint path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:{strictly 24.2.1}' because of the following reason: debugRuntimeClasspath uses version 24.2.1
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'
Constraint path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:{strictly 24.2.1}' because of the following reason: debugRuntimeClasspath uses version 24.2.1
Dependency path 'NoteKeeper:app:unspecified' --> 'com.android.support:appcompat-v7:24.2.1'

我的应用分级:

apply plugin: 'com.android.application'
android {
compileSdkVersion 24
defaultConfig {
applicationId "com.jwhh.jim.notekeeper"
minSdkVersion 24
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.android.support:appcompat-v7:24.2.1'
implementation 'com.android.support:design:24.2.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:24.2.1'
implementation 'com.android.support:cardview-v7:24.2.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation ('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'com.android.support:appcompat-v7:24.2.1'
androidTestImplementation 'com.android.support:design:24.2.1'
androidTestImplementation 'com.android.support:cardview-v7:24.2.1'
}

我在这里四处寻找解决方案,但没有找到任何合适的解决方案。

当我添加浓缩咖啡时的资源错误谈到了更改 SDK 版本。我所做的一切都是为了匹配24,所以我认为改变这一点不会让事情变得更好。也许我使用了错误的浓缩咖啡版本?

在 gradle 中添加浓缩咖啡库后,浓缩咖啡测试没有运行,这表明我需要添加一些排除项,我尝试如下,没有明显的区别。另外,不完全确定这应该做什么。

androidTestImplementation ('com.android.support.test.espresso:espresso-contrib:3.0.2', {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
})

我在浓缩咖啡设置中四处寻找,但迷路了,在espresso-contrib方面没有发现任何警告


在用户的建议下,我从androidTestImplementation中删除了额外的依赖项,但仍然出现错误

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:support-annotations:28.0.0'
implementation 'com.android.support:appcompat-v7:24.2.1'
implementation 'com.android.support:design:24.2.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:24.2.1'
implementation 'com.android.support:cardview-v7:24.2.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation ('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
}

Cannot find a version of 'com.android.support:appcompat-v7' that satisfies the version constraints:...

基本上,错误告诉您代码的不同部分(主代码和Android测试代码)取决于不同版本的支持库,在本例中为24.2.1和27.1.1。

什么意思?

假设您有一些类InternalImportantThing。您的代码依赖于该类的 v1。然后添加一些依赖于该类的 v2 的库(在您的情况下espresso-contrib)。

问题是这两个类具有相同的"路径"。 例如com.myapp.InternalImportantThing.而且您不能将它们同时放在同一个应用程序中,因为在这种情况下,类加载器会变得疯狂:"上哪个课:com.myapp.InternalImportantThing还是com.myapp.InternalImportantThing?(是的,它们是相同的 - 这就是问题所在)。

这就是为什么你必须解决冲突。有不同的方法可以做到这一点,我们稍后会讨论。

解决后会发生什么?

这要看情况。如果 v1 和 v2 具有相同的公共 API,并且仅在实现上有所不同,则一切可能运行良好。或者可能不会,如果这些方法对相同的输入产生不同的结果。但是,如果 v2 缺少 v1 的某些 API,并且 v1 没有来自 v2 的 smth - 那么您就有麻烦了。因为您需要选择一个,并且您的应用程序或依赖库可能会丢失它们所依赖的内容。

关于它的几个链接

Gradle 冲突解决

排查依赖项解析问题

后者很长,在您的情况下,您可以强制主应用程序使用最新版本的支持库(如implementation 'com.android.support:appcompat-v7:28.0.0')。这可能还需要您更新compileSdk/targetSdk,请注意警告和错误。

您可以在此处找到最新版本,也可以使用 Android Studio 中的帮助程序。

唯一的提示是尽量不要从AndroidX软件包中导入任何内容,因为您不能同时拥有AndroidX和非AndroidX选项。很容易找到它们:它们以androidx开头,您可以在此处找到示例

最新更新