gradle - library duplicates in dependencies

Solution 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.android.support:support-annotations:22.1.1
+--- 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
+--- com.google.android.gms:play-services-base:6.5.+ -> 6.5.87
+--- project :yourProject
|    +--- com.loopj.android:android-async-http:1.4.6
|    +--- org.apache.httpcomponents:httpmime:4.2.5
|    |    \--- org.apache.httpcomponents:httpcore:4.2.4
|    \--- com.google.code.gson:gson:2.3.1
+--- project :facebook
|    \--- com.android.support:appcompat-v7:22.1.1
|         \--- com.android.support:support-v4:22.1.1
|              \--- com.android.support:support-annotations:22.1.1 -> 22.2.0

You can see overriden dependencies and decide in mind which ones should be avoided. In above example, last line com.android.support:support-annotations 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: 'com.android.support'
}

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

compile('com.google.android.gms:play-services-base:6.5.+') {
    exclude module: 'support-v4'
}

For more information, you can see the tutorial at https://docs.gradle.org/current/userguide/userguide_single.html#sec:listing_dependencies