gradle – library duplicates in dependencies

The question:

I have an Android project with gradle. The problem is:
In project view I see few versions of support-v4 libraries, for example support-v4-21.0.3 and support-v4-22.2.0.

But in build.gradle I don’t have support-v4 at all.

But I have ~10 other dependencies in build.gradle. For example
appcompat-v7:22.2.0. I can suggests that appcompat-v7:22.2.0 depens on support-v4-22.2.0 in maven dependencies and implicitly pulls it. But I have no ideas who pulls support-v4-21.0.3.

As far as I know all this libs will packed in my APK and increase weight of APK.

So I have next questions:

  1. How to avoid library duplicates?
  2. How to see maven dependencies in Android Studio?
  3. How detect which library require this library? For example which library require support-v4-21.0.3 in my project?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

To find duplicate dependencies or its required dependencies, you can visualize library dependencies in tree. Execute gradle command as below.

gradle -q dependencies yourProject:dependencies --configuration compile

Note that, run gradlew in Windows as below.

gradlew -q dependencies yourProject:dependencies --configuration compile

The command result will show you human-readable tree hierarchy of all dependencies as below.

compile - Classpath for compiling the main sources.
+--- org.androidannotations:androidannotations-api:3.2
+--- com.squareup:otto:1.3.6
+--- in.srain.cube:grid-view-with-header-footer:1.0.10
+--- com.nostra13.universalimageloader:universal-image-loader:1.9.3
+--- com.github.chrisbanes.photoview:library:1.2.3
+--- org.simpleframework:simple-xml:2.7.1
+--- -> 6.5.87
+--- project :yourProject
|    +---
|    +--- org.apache.httpcomponents:httpmime:4.2.5
|    |    --- org.apache.httpcomponents:httpcore:4.2.4
|    ---
+--- project :facebook
|    ---
|         ---
|              --- -> 22.2.0

You can see overriden dependencies and decide in mind which ones should be avoided. In above example, last line presents overriden from 22.1.1 to 22.2.0 internally.

To avoid duplicates, you can add exclude clauses in each project build.gradle file.

compile('com.github.chrisbanes.photoview:library:1.2.3') {
    exclude group: ''

compile('org.simpleframework:simple-xml:2.7.1') {
    exclude module: 'stax'
    exclude module: 'stax-api'
    exclude module: 'xpp3'

compile('') {
    exclude module: 'support-v4'

For more information, you can see the tutorial at

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment