Android 与依赖冲突';support:supportannotations';项目中

Android 与依赖冲突';support:supportannotations';项目中,android,android-support-library,Android,Android Support Library,过了一会儿,我又回到了安卓工作室。 当我创建一个新项目时,我遇到了这个问题 错误:任务“:app:preDebugAndroidTestBuild”的执行失败 与依赖项“com.android.support”冲突:支持注释“在项目中”:应用程序。应用程序(26.1.0)和测试应用程序(27.1.1)的解析版本不同。有关详细信息,请参阅 我找到了许多解决这个问题的办法,但没有一个奏效。 我的依赖项中包含以下内容: dependencies { implementation fileTr

过了一会儿,我又回到了安卓工作室。 当我创建一个新项目时,我遇到了这个问题

错误:任务“:app:preDebugAndroidTestBuild”的执行失败

与依赖项“com.android.support”冲突:支持注释“在项目中”:应用程序。应用程序(26.1.0)和测试应用程序(27.1.1)的解析版本不同。有关详细信息,请参阅

我找到了许多解决这个问题的办法,但没有一个奏效。

我的依赖项中包含以下内容:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'

    androidTestCompile 'com.android.support:support-annotations:27.1.1'
}

对于appCompat和support Annotation,您需要使用相同版本的support library。因此,将依赖项更改为:

implementation 'com.android.support:appcompat-v7:26.1.0'
androidTestCompile 'com.android.support:support-annotations:27.1.1'

然后,尝试从浓缩咖啡中排除现有的支持注释,包括:

androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

我得到了与support annotations模块相同的版本冲突。 不同之处在于我试图减少测试库使用的版本

dependencies {
def withoutSupportAnnotations = {exclude group: 'com.android.support', module: 'support-annotations'}
androidTestImplementation 'com.android.support.test:runner:1.0.2', withoutSupportAnnotations
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2', withoutSupportAnnotations}
这个可重用变量帮助了我。
另外测试:runner和espresso使用相同版本的注释(我的例子是27.2.1)

排除的语法对我不起作用。我找到了另一个,请参见下面的答案
dependencies {
def withoutSupportAnnotations = {exclude group: 'com.android.support', module: 'support-annotations'}
androidTestImplementation 'com.android.support.test:runner:1.0.2', withoutSupportAnnotations
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2', withoutSupportAnnotations}