Gradle 格拉德尔';生成脚本错误';在依赖项中尝试使用testCompile时发生

Gradle 格拉德尔';生成脚本错误';在依赖项中尝试使用testCompile时发生,gradle,android-studio,android-gradle-plugin,Gradle,Android Studio,Android Gradle Plugin,我正在使用Android Studio,在我的应用程序依赖项中,我试图添加testCompile依赖项,如下所示: 当我同步我的文件时,我得到错误: 我不明白发生了什么,我根文件夹中的gradle构建文件被设置为classpath'com.android.tools.build:gradle:0.12.+',这是最新版本。为什么它不能识别testCompile?我不想将测试依赖项部署到生产。。。任何帮助都将不胜感激 编辑:这是项目构建文件 buildscript { repositori

我正在使用Android Studio,在我的应用程序依赖项中,我试图添加testCompile依赖项,如下所示: 当我同步我的文件时,我得到错误: 我不明白发生了什么,我根文件夹中的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'
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.X.X'
}

您应该使用
androidTestCompile
,而不是
testCompile
。如果这是由于通过“项目结构”对话框修改依赖项范围造成的,则会出现一个错误,即它使用错误的语句来设置依赖项。我已经申请了这篇文章。

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

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'
}
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中


希望这能帮助有同样问题的人。

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

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

请将您的构建文件添加到您的问题中。我没有看到testCompile语句。这只是因为我删除了它,我对最后3个依赖项进行了测试编译,我将对其进行编辑。这不是正确的答案。切换到
androidTestCompile
testCompile
非常不同。后者对于只需要JVM的纯单元测试非常有用,而不是用于
androidTestCompile
的模拟器或设备。看看哪一个需要更新gradle。@JayWick我把答案翻到社区维基上,鼓励人们编辑它,并在必要时更正它。是的,这正是我的问题。谢谢。你可以参考《谢谢》的解决方案。这些文档甚至在应用程序的顶级build.gradle文件中声明了
,这是毫无帮助的。