无法从Android项目中的gradle中删除传递依赖项

时间:2016-05-26 12:18:58

标签: android android-studio gradle build.gradle gradle-dependencies

我正在尝试从我的Android项目中删除传递依赖,并且由于某种原因,当我尝试从我的特定依赖项中删除依赖项时,exclude无效。

例如,我想从项目中删除support-annotations

如果我使用

configurations {
    all*.exclude group: 'com.android.support', module:'support-annotations'
}

依赖项从排除并从依赖关系树中获取。我可以使用./gradlew app:dependencies enter image description here

来查看依赖关系树

但如果我使用

 compile('com.android.support:support-v4:23.4.0')
            {
                exclude group: 'com.android.support', module:'support-annotations'
            }

然后我仍然在依赖树中看到依赖关系。enter image description here

所以我的问题是,当我尝试从特定依赖项中删除依赖项时,为什么它不起作用?

更新1:

也可以有人告诉我树中依赖项旁边的star(*)符号代表什么?

更新2

我也在使用Fresco我在Fresco尝试了同样的事情,exclude 规则似乎适用于它

Fresco的依赖树 enter image description here

我在Fresco中排除imagepipeline-base时的依赖关系树

compile("com.facebook.fresco:fresco:0.9.0")
            {
                exclude group: 'com.facebook.fresco', module: 'imagepipeline-base'
            }

enter image description here

如您所见,imagepipeline-base依赖项被排除在外。 所以我不知道为什么这对于Android支持注释传递依赖

不起作用

2 个答案:

答案 0 :(得分:4)

所以我在一位朋友的帮助下想到了这一点。我无法删除support-annotation的原因是因为大多数其他依赖项使用support-v4作为传递依赖,而support-v4也有自己的support-annotation副本。

现在有2个解决方案

解决方案1:

从包含support-annotation的所有依赖项中排除support-v4作为传递依赖项。

解决方案2:

仅从support-annotation依赖项中排除support-v4,并从support-v4作为传递依赖项的所有其他依赖项中删除support-v4依赖项。

注意:使用上述方法之一,我能够解决我的问题并弄清楚如何从多个依赖项中引用传递依赖项时删除传递依赖项。

关于(*)符号,这意味着已经显示了依赖关系树用于该依赖关系。而不是再显示那些依赖关系的整个树,gradle会显示(*)符号。

示例build.gradle文件可用here

答案 1 :(得分:0)

有更优雅的解决方案:您可以在 build.gradle 文件中使用 configueation.all 块,如下例所示:

configurations.all {
  exclude group: 'com.android.support', module: 'support-annotations'
}

它应该排除应用程序中所有内部模块的所有传递依赖项。