AS:Error:Execution failed for task ':app:preDebugAndroidTestBuild'.

在新建AS项目时,由于gradle的版本冲突原因,经常会出现如下的错误

Error:Execution failed for task ':app:preDebugAndroidTestBuild'.
> Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (26.0.0-beta1) and test app (27.1.1) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.

错误的原因:名为app的module里,com.android.support:support-annotations这个依赖冲突了,app里的版本是26.0.0,但是Test app的版本里是27.1.1。

在网上找了好多种解决方案,将各种方案分享如下:

解决方案一:

直接进行:build->Rebuid-project 操作后,但重新build后问题还是没有 解决。

解决方案二:
在app的build.gradle中添加如下代码:

androidTestImplementation('com.android.support:support-annotations:26.0.0') {
    force = true
}

添加后的结构如下所示:

dependencies {
    androidTestImplementation('com.android.support:support-annotations:26.0.0') {
        force = true
    }
    ...
}

然后执行同步操作,发现还是没有解决问题。

解决方案三:
在app的build.gradle中添加如下代码:

configurations.all{
    resolutionStrategy.force'com.android.support:support-annotations:27.1.1'
}

该代码在dependencies{…….}的下面添加。
然后同步一下,解决问题。

猜你喜欢

转载自blog.csdn.net/xk7298/article/details/82817066