Gradle Transitive dependency exclusion is not working as expected. (How do I get rid of com.google.guava:guava-jdk5:13.0 ?)

前端 未结 5 1054
星月不相逢
星月不相逢 2020-12-30 01:57

here is a snippet of my build.gradle:

compile \'com.google.api-client:google-api-client:1.19.0\'
compile \'com.google.apis:google-api-services-oauth2:v2-rev7         


        
5条回答
  •  误落风尘
    2020-12-30 02:10

    Building on @thoutbeckers answer due to a special case, where I didn't think that his answer applied, but it actually did. Hopefully, this answer can help others who shared my special case problem. Originally I thought that the bad transitive dependency was only referenced by one dependency in the build.gradle file but it was actually referenced by two dependencies. This was because both dependencies where the the bad transitive dependency was referenced from had a parent/child relationship, but I only noticed the relationship with the child dependency, and not the parent dependency.

    Consider the following dependency tree (produced by the command gradle :dependencies):

    compileClasspath - Compile classpath for source set 'main'.
    +--- my.org:com.my.pkg.parent:6.+ -> 6.0.4
    |    +--- # misc. dependencies
    |    +--- my.org:com.my.pkg.child:6.0.4
    |    |    +--- # misc. dependencies
    |    |    +--- other.org:bad.transitive.dependency:0.9.1 FAILED
    |    |    +--- # misc. dependencies
    |    |--- # misc. dependencies
    +--- # misc. dependencies
    

    From the dependency tree, it looks like the other.org:bad.transitive:dependency:0.9.1 is only referenced by one dependency in your build file, not two. However, suppose your Gradle file looks like this:

    // ... misc. ...
    dependencies {
        // ... misc. dependencies ...
        compile 'my.org:com.my.pkg.parent:6.+'
        // ... misc. dependencies ...
        compile ('my.org:com.my.pkg.child:6.0.4') {
            exclude group: 'other.org', module: 'bad.transitive.dependency'
    }
    

    For a Gradle file like the one above, the error will persist even though the transitive dependency you wanted to exclude occurs only in the child dependency, not the parent dependency. However, because both the parent and child projects are referenced by the build.gradle file, the bad transitive dependency must be excluded from both dependencies, as @thoutbeckers stated above.

    Note that if you don't want to add the exclusion at the configuration level (as @thoutbeckers showed in their answer), you can always just exclude the transitive dependency from both dependencies where it is referenced, explicitly.

提交回复
热议问题