在gradle中添加依赖项

Łuk*_*zka 3 gradle gradlew

我知道为什么我的依赖不起作用.这是我的配置:

ext {
   junitVersion = "4.11"

   libs = [
           junit : dependencies.create("junit:junit:4.11")
   ]
}

configure(subprojects) { subproject ->
    dependencies {
        testCompile(libs.junit)
    }
}
Run Code Online (Sandbox Code Playgroud)

我有错误:

* What went wrong:
A problem occurred evaluating root project 'unit590'.
> Could not find method testCompile() for arguments [DefaultExternalModuleDependency{group='junit', name='junit', version='4.11', configuration='default'}] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@785c1069.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

Pet*_*ser 9

testCompile配置由申报java插件.因此,在您添加依赖项之前testCompile,您必须这样apply plugin: "java"subprojects.

PS:声明libs可以简化,如马特的答案所示.configure(subprojects) { ... }可以简化为subprojects { ... }.