android studio 中Error:Execution failed for task ':app:preDebugAndroidTestBuild'. 的解決辦法

  使用AndroidStudio工具建立新的app應用時,根據工具提示進行多個next以後,發現編譯報錯,報錯以下:html

  

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.

 

  通過網上查閱,發現提供有不少方法,好比 build->Rebuid-project,該方法只是當前啓動有用,可是若是關閉androidstudio工具從新打開以後又會從新報錯。android

  該問題主要是由於在新建的應用中默認依賴了com.android.support:support-annotations包與app版本衝突了,app裏的版本是26.1.0,可是Test app的版本里是27.1.1。可在External Libraries文件夾下面找到該包,發現確實衝突。app

  最後通過查閱,發現兩種方法能夠解決該問題,思路都是同樣的,就是將27.1.1版本號強制轉換爲26.1.0,轉換方法以下:工具

 (1)在依賴中強制轉換,在app模塊下的build.gradle中dependencies下,添加依賴轉換,主要是下面兩行代碼gradle

    

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

 

   所有代碼爲:ui

    

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "baseapp.li.com.baseapp"
        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'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    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'
    androidTestImplementation('com.android.support:support-annotations:26.1.0') {
        force = true
    }
}

 

 (2)在配置中強制轉換,在app模塊下的build.gralde中進行配置,其中主要增長了兩行代碼spa

      

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

 

     所有代碼以下:code

apply plugin: 'com.android.application'
 
configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
}
 
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "wzt.mytest"
        minSdkVersion 21
        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'
        }
    }
}
 
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    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'
}

 

 

以上兩種方法都可解決包名衝突問題。下面這個代碼code背景不知道怎麼去掉了,允許我調皮一下,應該無大礙。htm

相關文章
相關標籤/搜索