我无法弄清楚这个问题。我正在尝试根据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
您正在为buildscript
放置依赖项。通常,您应该将依赖项放在构建生命周期或插件开发所需的位置。
你可以通过将dependencies
块移动到allprojects
闭包来实际解决这个问题,例如:
// ...
allprojects {
repositories {
jcenter()
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
}
但请在评论中遵循Mark Keen的建议。将根共用依赖项放在根build.gradle
文件中并不是最好的主意。