构建第一个默认应用程序时构建错误“任务执行失败”:app:preDebugAndroidTestBuild'“

时间:2018-05-01 20:13:11

标签: android-studio android-gradle build.gradle

尝试按照此处的教程进行操作: https://developer.android.com/training/basics/firstapp/creating-project

但是在我创建一个活动并单击“完成”后,它会给出一个Gradle构建错误:

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

与项目':app'中的依赖'com.android.support:support-annotations'冲突。 app(26.1.0)和测试app(27.1.1)的已解决版本有所不同。有关详细信息,请参阅https://d.android.com/r/tools/test-apk-dependency-conflicts.html

查看其他SO解决方案(这里的第二个答案:Resolved versions for app (22.0.0) and test app (21.0.3) differ),它说要将其添加到build.gradle脚本的依赖项中:

androidTestCompile 'com.android.support:support-annotations:xx.x.x'

以上解决方案也是它在Google Samples上展示的内容(https://github.com/googlesamples/android-testing/blob/ba14ef9e925fa17621bf86abe5336dcb9d53e466/runner/AndroidJunitRunnerSample/app/build.gradle#L36

所以这是我的build.gradle(模块:app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.ahmedayman.ember"
        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 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.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'
    androidTestCompile 'com.android.support:support-annotations:26.1.0'
}

但是当我尝试同步项目时,我仍然遇到同样的错误。知道如何解决这个问题吗?

4 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,我在build.gradle(模块:应用程序)中做了一些更改,它对我有用。我换了这个 androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

于: androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

它对我有用。

答案 1 :(得分:0)

我在build.gradle(Module:app)中将appcompat-v7的实现版本更改为27.1.1,它解决了我的问题。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.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'
}

答案 2 :(得分:0)

修改

答案在这里:All com.android.support libraries must use the exact same version specification

提到android studio 3.0已将compile替换为实现,因此我使用implementation 'com.android.support:design:27.1.1'代替compile(将在下面提到)。

原始答案

错误消息提供了指向要查看的网站的链接。我查看了与此网址相关联的网站和网站:https://developer.android.com/studio/build/gradle-tips#configure-project-wide-properties

提到的用于配置项目范围属性的URL。它说要在我的build.gradle项目文件中添加它:

// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
    // The following are only a few examples of the types of properties you can define.
    compileSdkVersion = 26
    // You can also use this to specify versions for dependencies. Having consistent
    // versions between modules can avoid behavior conflicts.
    supportLibVersion = "27.1.1"
    ...
}

并在我的build.gradle应用文件中添加:

android {
  // Use the following syntax to access properties you define at the project level:
  // rootProject.ext.property_name
  compileSdkVersion rootProject.ext.compileSdkVersion
  ...
}
...
dependencies {
    compile "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    ...
}

这解决了这个问题。

答案 3 :(得分:0)

在存储库中将jcenter()替换为mavenCentral()