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

  使用AndroidStudio工具创建新的app应用时,根据工具提示进行多个next之后,发现编译报错,报错如下:

  

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工具重新打开之后又会重新报错。

  该问题主要是因为在新建的应用中默认依赖了com.android.support:support-annotations包与app版本冲突了,app里的版本是26.1.0,但是Test app的版本里是27.1.1。可在External Libraries文件夹下面找到该包,发现确实冲突。

  最后经过查阅,发现两种方法可以解决该问题,思路都是一样的,就是将27.1.1版本号强制转换为26.1.0,转换方法如下:

 (1)在依赖中强制转换,在app模块下的build.gradle中dependencies下,添加依赖转换,主要是下面两行代码

    

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

   全部代码为:

    

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中进行配置,其中主要增加了两行代码

      

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

     全部代码如下:

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背景不知道怎么去掉了,容许我调皮一下,应该无大碍。

 

猜你喜欢

转载自www.cnblogs.com/fei-android/p/10364265.html