Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to remove transitive dependency from gradle in an Android Project

I am trying to remove the transitive dependencies from my Android project and for some reason exclude is not working when i try to remove a dependency from my particular dependency.

For example i want to remove support-annotations from my project

if i use

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

The dependency gets excluded and from the dependency tree. i can see the dependency tree by using ./gradlew app:dependencies enter image description here

But if i use

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

Then i still see the dependency in the dependency tree.enter image description here

So my question is that why is it not working when i try to remove the dependency from a particular dependency ?

Update 1:

Also can anyone tell me what does the star (*) symbol next to dependency in the tree represent ?

Update 2

I am also using Fresco I tried the same thing with Fresco and exclude rule seems to work for it

Dependency Tree of Fresco enter image description here

Dependency Tree when i exclude imagepipeline-base in Fresco

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

enter image description here

As you can see the imagepipeline-base dependency gets excluded. So i don't know why this doesn't work for Android Support Annotations transitive dependency

like image 742
Sheraz Ahmad Khilji Avatar asked May 26 '16 12:05

Sheraz Ahmad Khilji


People also ask

How do you remove transitive dependency in Gradle?

When you specify a dependency in your build script, you can provide an exclude rule at the same time telling Gradle not to pull in the specified transitive dependency. For example, say we have a Gradle project that depends on Google's Guava library, or more specifically com. google. guava:guava:30.1.

How do you remove a transitive dependency?

If a transitive dependency exists, we remove the transitively dependent attribute(s) from the relation by placing the attribute(s) in a new relation along with a copy of the determinant.

How do you change transitive dependency on Gradle?

To override the version of a transitive dependency in Gradle, exclude it from the declared dependency that pulls it in, and then explicitly declare the version that you prefer to use in your build.

Does Gradle support transitive dependencies?

Gradle automatically resolves those additional modules, so called transitive dependencies. If needed, you can customize the behavior the handling of transitive dependencies to your project's requirements. Projects with tens or hundreds of declared dependencies can easily suffer from dependency hell.


1 Answers

So i have figured this out with the help of one of my friends. The reason why i was not able to remove support-annotation was because most of the other dependencies were using support-v4 as transitive dependency and those support-v4 also had their own copy of support-annotation.

Now there are 2 solutions to this

Solution 1:

exclude support-annotation from all the dependencies that containsupport-v4 as transitive dependency.

Solution 2:

exclude support-annotation only from my support-v4 dependency and remove support-v4 dependency from all other dependencies that have support-v4 as transitive dependency.

Note: Using one of the above approaches i was able to solve my problem and figure out how we can remove transitive dependencies when they are referenced from multiple dependencies.

And regarding the ( * ) symbol it means that the dependency tree is for that dependency is already shown. Instead of showing the whole tree for those dependencies again gradle shows ( * ) symbol with them.

Sample build.gradle file is available here

like image 77
Sheraz Ahmad Khilji Avatar answered Oct 24 '22 09:10

Sheraz Ahmad Khilji