如何排除从其他子项目中引入的依赖项?

use*_*607 20 dependencies gradle

这不是重复,因为那些其他解决方案不起作用.

我有一个子项目:

:公地:小部件

gradle.build(子项目)类似于:

configurations {providedCompile}

dependencies {
  compile project(":commons:other-widget")
...other dependencies...
}
Run Code Online (Sandbox Code Playgroud)

如果显示依赖项:

+--- project :commons:some-other-project
    +--- project :commons:exclude-me-project (*)
    \--- org.apache.cxf:cxf-rt-frontend-jaxrs: -> 3.0.3 (*)
Run Code Online (Sandbox Code Playgroud)

什么行不通:

任何通常的语法.我试过了我能想到的每一个变化.甚至去寻找API但无法找到我需要的东西.

在这个项目的依赖项部分:...

compile project(":commons:some-other-project") {
 exclude (":commons:exclude-me-project")
}
Run Code Online (Sandbox Code Playgroud)

结果:

Could not find method exclude() for arguments [:commons:some-other-project] on project 
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

compile ( project (':commons:some-other-project') ) {
  transitive = false
}
Run Code Online (Sandbox Code Playgroud)

结果:它不删除":commons:some-other-project"的依赖项,而是删除":commons:some-other-project".

我有一个庞大而复杂的项目要转换.我前面有很多这样的工作.将项目作为依赖项,如何从中排除项目?

Sta*_*lav 38

exclude对于依赖项有一点点另一种语法,所以尝试提供模块名称,它等于exclude-me-project名称,如:

compile(project(":commons:some-other-project")) {
    exclude module: "exclude-me-project"
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以排除commons项目的所有传递依赖项,但它将删除some-other-project项目的所有deps ,包括exclude-me-project:

compile(project(":commons:some-other-project")) {
    transitive = false
}
Run Code Online (Sandbox Code Playgroud)

  • @ user447607只是不要忘记将`project`放入`(`和`)`,因为它在答案中完成了.不是'编译项目(":commons:some-other-project")`,而是`compile(project(":commons:some-other-project"))`.我已经测试过了,但是还有一些项目结构 (6认同)

小智 6

对于新的 gradle 语法,您可以执行以下操作:

implementation (project(path: ':my_library_v1.0.0')) {
    exclude (group: 'com.google.code.gson', module: 'gson')
}
Run Code Online (Sandbox Code Playgroud)