gradle 怎么配置依赖对所有的子模块都生效?

2020-05-09 12:32:48 +08:00
 rizon

研究了好半天也不会。 我在根模块添加:

subprojects{
 dependencies{
  testCompile group: 'junit', name: 'junit', version: '4.12'
 }
}

这个 gradle 执行会报错

A problem occurred evaluating root project 'operation'.
> Could not find method testCompile() for arguments [{group=junit, name=junit, version=4.12}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
2170 次点击
所在节点    程序员
6 条回复
ldcloli
2020-05-09 12:35:50 +08:00
子项目中有不支持 testCompile 字段的模块?
rizon
2020-05-09 13:12:33 +08:00
@ldcloli #1 我只能每个子项目分别配置 testCompile 才行
Flywith24
2020-05-09 13:54:43 +08:00
使用 buildSrc,添加公共依赖和配置

https://juejin.im/post/5e22c2ce6fb9a02ff67d41c3

这个 demo 是 kotlin 的,也可以用 groovy 的
zoharSoul
2020-05-09 14:15:39 +08:00
子模块肯定有依赖关系吧.
放在 base 那个子模块里就可以了啊
norcm
2020-05-09 16:03:07 +08:00
compile 和 testCompile 是属于 java 或者 java-library 这个插件的,所以你得申明使用 java 插件:
subprojects{
apply plugin: "java-library"
dependencies{
testCompile group: 'junit', name: 'junit', version: '4.12'
}
}
rizon
2020-05-12 14:18:34 +08:00
@norcm #5 多谢 加了一个 apply plugin: 'java' 之后就可以了。gradle 是真麻烦的。。看不明白

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/669949

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX