【问题标题】:Gradle 'Build Script error' occurs when I attempt to use testCompile in dependencies当我尝试在依赖项中使用 testCompile 时发生 Gradle“构建脚本错误”
【发布时间】:2014-08-11 21:22:04
【问题描述】:

我正在使用 Android Studio,在我的应用程序的依赖项中,我尝试添加一个 testCompile 依赖项,如下所示:http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html 当我同步我的文件时,我收到错误: 我不明白发生了什么,我根文件夹中的 gradle 构建文件设置为 classpath 'com.android.tools.build:gradle:0.12.+',这是最新版本。为什么它不能识别 testCompile?我不想将测试依赖项部署到生产环境...任何帮助将不胜感激。

编辑:这是项目构建文件

buildscript {
    repositories {
        jcenter()


    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
    }
}

这里是 src 构建文件:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 17
    buildToolsVersion "20.0.0"
    defaultConfig {
        applicationId "com.example.edu.myApp"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug{
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/scribe-1.3.5.jar')
    compile files('libs/json_simple-1.1.jar')
    compile 'com.google.android.gms:play-services:5.0.77'
    // Can't be higher than 19 if we want to support smaller android versions
    compile 'com.android.support:appcompat-v7:19.+'
    // You must install or update the Support Repository through the SDK manager to use this dependency.
    compile 'com.android.support:support-v4:19.+'
    // This Mockito includes all dependancies (great for us)
    testCompile "org.mockito:mockito-core:1.9.+"
    testCompile 'org.hamcrest:hamcrest-all:1.3'
    testCompile 'org.objenesis:objenesis:1.2'
}

【问题讨论】:

  • 请将您的构建文件添加到您的问题中。
  • 我没有在其中看到 testCompile 语句。
  • 只是因为我删除了它,我对最后3个依赖项进行了测试编译,我会编辑它。

标签: gradle android-studio android-gradle-plugin


【解决方案1】:

您应该使用androidTestCompile,而不是testCompile。如果这是由于通过“项目结构”对话框修改了依赖项范围,那么存在一个错误,即它使用错误的语句来设置依赖项。我已经为此提交了https://code.google.com/p/android/issues/detail?id=74771

【讨论】:

  • 这不是正确的答案。 切换到androidTestCompiletestCompile 非常不同。后者对于只需要 JVM 而不是 androidTestCompile 所针对的模拟器或设备的纯单元测试很有用。请参阅Louis Morda's answer,其中涉及更新 gradle。
  • @JayWick 我已将答案转至社区 wiki,以鼓励人们对其进行编辑并在必要时进行更正。
【解决方案2】:

一年后我偶然发现了这篇文章。我遇到了这个问题,因为我继承了一个旧项目,该项目在 Gradle 构建文件中使用了过时的 Gradle 构建工具设置。我通过更新 Gradle 构建工具解决了这个问题,显然你需要通过增加 Gradle 构建文件中的版本号来解决这个问题:

dependencies {
    classpath 'com.android.tools.build:gradle:1.X.X'
}

其中 X.X 是最新的 gradle 构建工具版本号。现在我使用的是 Android Studio v1.2,并且我的构建工具版本号设置为:

dependencies {
    classpath 'com.android.tools.build:gradle:1.2.2'
}

我在尝试实现单元测试时遇到了这个问题,这些单元测试现在显然已内置到 Android Studio 中。

希望这可以帮助遇到同样问题的人。

【讨论】:

  • 是的,这正是我的问题。谢谢。
【解决方案3】:

我也得到了这个“找不到方法'testcompile()'”的错误。问题是我的项目 build.gradle 文件中有 testCompile 'junit:junit:4.12' 而不是 app build.gradle 文件中的。我怎么知道它在错误的文件中?项目文件仅声明“注意:不要将您的应用程序依赖项放在这里;它们属于单独的模块 build.gradle 文件”。这么简单的答案难怪我找不到。

【讨论】:

【解决方案4】:

我有一个图书馆项目。我相信 Android 文档并将testCompile 声明放在我的顶级build.gradle 文件中。原来它实际上必须进入我的模块 build.gradle 文件中。

【讨论】:

    猜你喜欢
    • 2021-03-13
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 2016-05-30
    相关资源
    最近更新 更多