当我尝试在依赖项中使用testCompile时,会发生Gradle"构建脚本错误"

jQw*_*rdy 11 gradle android-studio android-gradle-plugin

我正在使用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()
    }
}
Run Code Online (Sandbox Code Playgroud)

这是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'
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*rta 17

你应该使用androidTestCompile,而不是testCompile.如果这是由于通过"项目结构"对话框修改了依赖关系范围,则会出现一个错误,即使用错误的语句来设置依赖关系.我已为此提交了https://code.google.com/p/android/issues/detail?id=74771.

  • **这不是正确的答案.**切换到`androidTestCompile`与`testCompile`非常不同.后者对纯单元测试很有用,它只需要JVM而不是`androidTestCompile`用于的模拟器或设备.请参阅[Louis Morda的回答](http://stackoverflow.com/a/30039571/80428),其中涉及更新gradle. (7认同)

Lou*_*rda 9

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

dependencies {
    classpath 'com.android.tools.build:gradle:1.X.X'
}
Run Code Online (Sandbox Code Playgroud)

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

dependencies {
    classpath 'com.android.tools.build:gradle:1.2.2'
}
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助有同样问题的人.

  • 是的,这正是我的问题.谢谢. (2认同)