57

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?
Youngjae
  • 24,352
  • 18
  • 113
  • 198
anber
  • 3,463
  • 6
  • 39
  • 68

1 Answers1

103

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

Boken
  • 4,825
  • 10
  • 32
  • 42
Youngjae
  • 24,352
  • 18
  • 113
  • 198
  • Perfect. In my case it gives me too much output, so It's a good idea to add something like `--configuration compile` at the and. – anber Jun 04 '15 at 19:12
  • Thanks for the answer, save my time. – Rohan Kandwal Oct 29 '15 at 12:32
  • How can i tell by reading the output when i have a duplicate dependency ?I am looking at the tree but not sure how to read it. Is it recommended to work backwards ? If android studio complains of a duplicate in gradle then i'd search the tree for all packages that have that code and exclude it ? is that how its done. – j2emanue Dec 17 '15 at 01:13
  • 5
    Using `exclude` for this is a horrible idea - libraries have a minimum version of dependencies for a reason. If you exclude a dependency it is very likely that the behavior will be different or you'll experience a crash. You'll never have a duplicate class in your compiled APK (hence why the dependencies show that they are upgraded to be just a single version in your APK) – ianhanniballake Jul 05 '16 at 20:44
  • Do this exclusions reduce the final APK size? or just to build faster? – AXSM Apr 20 '17 at 17:39
  • @AlexSanchez // of course exclusions itself reduce APK size. But this post addresses _conflict_, so may or may not reduce APK size. – Youngjae Apr 21 '17 at 01:38
  • I cannot able to Obfuscate Android Project(minifyEnabled true) not working in both debug and release modes - Please find the Question link https://stackoverflow.com/questions/51720018/obfuscation-minifyenabled-true-not-working-in-debug-and-release – Naveen Aug 14 '18 at 05:03