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

Adding espresso-contrib to androidTestImplementation creates dependancy / constraint errors

我正在学习有关 PluralSight 的旧教程:Enhancing the Android Application Experience。由于我使用的是 IDE 的较新版本,因此我 运行 遇到了一些问题。大多数我可以在 SO 修复,但我被困住了,没有足够的经验超出我所知道的。

在设置一些单元测试时,我不得不将 androidTestImplementation ('com.android.support.test.espresso:espresso-contrib:3.0.2' 添加到 gradle 文件中。自从

以来,讲师就避免了这个问题

The espresso-contrib library is actually relying on different versions of certain classes than we are using in our application. Cause remember that gradle takes care of resolving down chain dependencies

模块中的错误体验不是我在下面得到的。通过在 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'

我的应用程序 gradle:

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,所以我不认为改变会让事情变得更好。也许我使用了错误的意式浓缩咖啡?

Espresso test aren't running after adding espresso-contrib library in 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 Setup 周围闲逛,但在 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 .

什么意思

假设您有一些 class InternalImportantThing。而您的代码取决于 class 的 v1。然后添加一些依赖于 class.

的 v2 的库(espresso-contrib 在你的例子中)

问题是两个 class 都相同,比方说,"path"。例如。 com.myapp.InternalImportantThing。而且你不能将它们都放在同一个应用程序中,因为在这种情况下类加载器会变得疯狂:"Which class to take: com.myapp.InternalImportantThing or com.myapp.InternalImportantThing?"(是的,它们是相同的 - 这就是问题所在)。

这就是您必须解决冲突的原因。有不同的方法,我们稍后再讨论。

解析后会发生什么?

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

关于它的几个链接

Troubleshoot dependencies resolution

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

您可以找到最新版本 here 或使用来自 Android Studio 的助手。

唯一的提示是尽量不要从 AndroidX 包中导入任何东西,因为您不能同时拥有 AndroidX 和 non-AndroidX 依赖项。很容易找到它们:它们以 androidx 开头,您可以找到示例 here