摇篮 - 依赖关系库复制摇篮、关系

2023-09-08 08:31:22 作者:如履薄冰

我有摇篮的Andr​​oid项目。问题是: 在项目查看我看到支持-V4的几个版本的库,例如支持-V4 -21.0.3 支持-v4-22.2.0

但是 build.gradle 我没有支持-V4 的。

不过,我在 build.gradle 〜10等的依赖关系。例如 appcompat-V7:22.2.0 。我可以表明, appcompat-V7:22.2.0 实际上还要考虑支持-v4-22.2.0 的Maven依赖和隐式拉它。但我没有想法谁拉支持-v4-21.0.3

据我知道这一切的库将在我的APK包装和增加体重APK的。

所以,我有下一个问题:

如何避免重复库? 如何看到Android的工作室Maven的依赖? 如何检测哪些库需要这个库?比如哪个库需要支持-v4-21.0.3 在我的项目? 解决方案

查找重复的依赖或所需的依赖,你可以想像在树库的依赖。执行摇篮命令如下。

 摇篮-q依赖yourProject:依赖--configuration编译
 

需要注意的是,在Windows中运行 gradlew 如下。

  gradlew -q依赖yourProject:依赖--configuration编译
 

该命令的结果会告诉你所有的依赖人类可读的树层次结构如下图所示。

 编译 - 类路径编制的主要来源。
+ --- org.androidannotations:androidannotations-API:3.2
+ --- com.android.support:support-annotations:22.1.1
+ --- com.squareup:奥托:1.3.6
+ --- in.srain.cube:网格视图与 - 页眉页脚:1.0.10
+ --- com.nostra13.universalimageloader:通用图像装载机:1.9.3
+ --- com.github.chrisbanes.photoview:库:1.2.3
+ --- org.simpleframework:简单的xml:2.7.1
+ --- com.google.android.gms:玩-服务基地:6.5 +  - >。 87年6月5日
+ ---项目:yo​​urProject
| + --- 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
+ ---项目: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
 
Cobar分布式关系数据库访问代理

您可以看到被覆盖的依赖关系,并决定考虑哪些应该避免的。在上面的例子中,最后一行 com.android.support:support-annotations presents从 22.1.1 重写以 22.2.0 内部。

为了避免重复,您可以添加排除每个项目的条款 build.gradle 文件。

 编译('com.github.chrisbanes.photoview:图书馆:1.2.3'){
    排除组:com.android.support
}
编译(org.simpleframework:简单的xml:2.7.1'){
    排除模块:STAX
    排除模块:STAX-API
    排除模块:XPP3
}
编译(com.google.android.gms:播放服务基地:6.5 +。')
        {
            排除模块:支持-V4
        }
 

有关详细信息,您可以查看本教程的https://docs.gradle.org/current/userguide/userguide_single.html#sec:listing_dependencies

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:

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

解决方案

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