1. 程式人生 > >Android studio新建工程報錯:Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict wit

Android studio新建工程報錯:Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict wit

報錯:

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

截圖:

 解決方法:

1.點選Build - Rebuild project,錯誤消除,但是重啟AS,再次開啟專案的時候還會出現該錯誤,雖然不影響,但是看著難受。

2.錯誤大致意思為,依賴衝突,可以看到,依賴項裡面,annotation有兩個,一個26.1.0另一個為27.1.1

3.解決方式一:在app - build.gradle  dependencies{}節點下增加如下內容:

    androidTestCompile('com.android.support:support-annotations:26.1.0') {
        force = true
    }

4.解決方式二:在app - build.gradle  Android{}節點下增加如下內容:

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

完整例子:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "club.pogaizai.del2"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
//方式一
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
    }
}

dependencies {
//方式二
    androidTestCompile('com.android.support:support-annotations:26.1.0') {
        force = true
    }
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}