1. 程式人生 > >AS:Error:Execution failed for task ':app:preDebugAndroidTestBuild'.

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{…….}的下面新增。
然後同步一下,解決問題。