gradle - library duplicates in dependencies

2019-01-03 08:49发布

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?

2条回答
该账号已被封号
2楼-- · 2019-01-03 09:33

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

查看更多
相关推荐>>
3楼-- · 2019-01-03 09:38

The following android studio plugin gives you details about the duplicate dependencies and total method count of each dependencies. Android Methods Count Plugin

enter image description here

查看更多
登录 后发表回答