Android Gradle 不会根据要求排除模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25993974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:54:25  来源:igfitidea点击:

Gradle won't exclude a module as requested

androidandroid-gradle-pluginbuild.gradle

提问by aez

I can't exclude a guava module in a build.gradle file using "exclude".

我无法使用“排除”排除 build.gradle 文件中的番石榴模块。

With this dependency block:

有了这个依赖块:

dependencies {
    ...
    compile(group: 'com.google.api-client', name: 'google-api-client', version: '1.19.0') {
        exclude(group: 'com.google.guava', module: 'guava-jdk5') // !!! Doesn't work!!!
       ...
    }
...
}

I get the dependency tree below. Note that guava-jdk5 is not excluded.

我得到下面的依赖树。请注意,不排除 guava-jdk5。

+--- com.google.api-client:google-api-client:1.19.0
|    +--- com.google.oauth-client:google-oauth-client:1.19.0
|    |    +--- com.google.http-client:google-http-client:1.19.0
|    |    |    ...
|    |    \--- com.google.code.findbugs:jsr305:1.3.9
|    ...
|    \--- com.google.guava:guava-jdk5:13.0

...

...

Notice that the last line still includes the guava module, it has not been excluded. Why? How to exclude it?

请注意,最后一行仍然包含 guava 模块,它没有被排除。为什么?如何排除它?

采纳答案by aez

The problem was that another compile object was also dependent on the first level dependency that had the guava-jdk5 as a transitive dependency, so this other object was bringing in the unwanted module.

问题是另一个编译对象也依赖于第一级依赖,将 guava-jdk5 作为传递依赖,所以这个另一个对象引入了不需要的模块。

I was able to finally see this using

我终于能够看到这个使用

./gradlew -q :app:dependencyInsight --dependency guava --configuration compile

and could exclude it using

并且可以使用排除它

configurations {
    compile.exclude module: 'guava-jdk5'
}

回答by yvolk

An answer by the question's author didn't help me, because it's too narrow / concrete. This is a modified approach, which works for exclusion of any module:

问题作者的回答对我没有帮助,因为它太狭隘/具体。这是一种修改后的方法,适用于排除任何模块:

  1. Check currently recognized dependencies using this command in the "Terminal":

    gradlew app:dependencies
    
  1. 在“终端”中使用此命令检查当前识别的依赖项:

    gradlew app:dependencies
    

The command is issued from the root of your project and "app" here should be replaced with a name of your application's "module".

该命令是从项目的根目录发出的,此处的“app”应替换为应用程序“模块”的名称。

You may also cd inside your application's folder and launch this:

你也可以在你的应用程序文件夹中 cd 并启动这个:

..\gradlew -q dependencies

Note: Use ../gradleon Linux.

注意:../gradle在 Linux 上使用。

You will see that severalconfigurations exist, not "compile" only (e.g. "_debugCompile" etc.). Each "configuration" has its own dependencies! This is why e.g. excluding something in "compile" configuration may be not enough.

您将看到存在多个配置,而不仅仅是“编译”(例如“_debugCompile”等)。每个“配置”都有自己的依赖关系!这就是为什么例如在“编译”配置中排除某些内容可能还不够的原因。

  1. Now apply exclusion to all "configurations" this way: Add this block to your application's ("module") gradle.build file (with your list of modules to exclude, of course :-) ):

    configurations {
        all {
            exclude module: 'httpclient'
            exclude module: 'httpcore'
        }
    }
    

    Tip: Advances exclusion cases are discussed e.g. here: https://discuss.gradle.org/t/how-do-i-exclude-specific-transitive-dependencies-of-something-i-depend-on/17991/7

  2. Repeat steps 1. and 2. until you don't see modules, which you want to exclude, in a list of dependencies of any configuration.

  1. 现在以这种方式将排除应用于所有“配置”:将此块添加到您的应用程序(“模块”)gradle.build 文件(当然还有要排除的模块列表:-)):

    configurations {
        all {
            exclude module: 'httpclient'
            exclude module: 'httpcore'
        }
    }
    

    提示:高级排除案例在此处讨论,例如:https: //discuss.gradle.org/t/how-do-i-exclude-specific-transitive-dependencies-of-something-i-depend-on/17991/7

  2. 重复步骤 1. 和 2. 直到在任何配置的依赖项列表中都看不到要排除的模块。

Tip: Here, inside "all" block, you may decide to put:

提示:在这里,在“all”块中,您可以决定放置:

transitive = false

And you will get rid of all transitive dependencies.

你将摆脱所有的传递依赖。