方法testCompile在使用Gradle版本2.2.3的android studio项目中无法识别?

问题描述 投票:2回答:1

我无法弄清楚这个问题。我正在尝试根据android开发者页面推荐的内容使用junit开始为我的项目编写单元测试。在我的应用程序的顶级构建文件中,我有一个gradle文件,如下所示:

//顶级构建文件,您可以在其中添加所有子项目/模块共有的配置选项。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        testCompile 'junit:junit:4.12'
        testCompile "org.mockito:mockito-core:1.9.5"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

当我尝试执行Gradle项目同步时出现以下错误:

Error:(9, 0) Could not find method testCompile() for arguments [junit:junit:4.12] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
java android android-studio gradle
1个回答
3
投票

您正在为buildscript放置依赖项。通常,您应该将依赖项放在构建生命周期或插件开发所需的位置。

你可以通过将dependencies块移动到allprojects闭包来实际解决这个问题,例如:

// ...
allprojects {
    repositories {
        jcenter()
    }
    dependencies {
        testCompile 'junit:junit:4.12'
        testCompile "org.mockito:mockito-core:1.9.5"
    }
}

但请在评论中遵循Mark Keen的建议。将根共用依赖项放在根build.gradle文件中并不是最好的主意。

© www.soinside.com 2019 - 2024. All rights reserved.