简体   繁体   English

管理Android Gradle依赖项的最佳方式

[英]Best way to manage Android Gradle dependencies

I'm using 'com.android.tools.build:gradle:0.6.+' to build my android application. 我正在使用'com.android.tools.build:gradle:0.6.+'来构建我的android应用程序。

It's known fact that Android dex can't contain different versions of same library. 众所周知,Android dex不能包含相同库的不同版本。 But how to handle situation when Maven dependency you want to use requires some library that you already using, but just different version. 但是,当你想要使用Maven依赖时,如何处理情况需要一些你已经使用的库,但只是不同的版本。 Example. 例。 Having following in build.gradle file: 在build.gradle文件中有以下内容:

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.github.chrisbanes.bitmapcache:library:2.3'
}

produces error: 产生错误:

UNEXPECTED TOP-LEVEL EXCEPTION:
    java.lang.IllegalArgumentException: already added: Landroid/support/v4/app/FragmentManager$OnBackStackChangedListener;
    at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
    at com.android.dx.dex.file.DexFile.add(DexFile.java:163)...

This error happens for the following reasons: http://search.maven.org/remotecontent?filepath=com/github/chrisbanes/bitmapcache/library/2.3/library-2.3.pom requires com.google.android:support-v4 出现此错误的原因如下: http//search.maven.org/remotecontent? filepath = com / github / clinbanes / bitmapcache / library3.3 / library2.3.​​pom需要com.google.android:support-v4

And com.android.support:appcompat-v7:+ already includes com.google.android:support-v4, what results in double inclusion of the same class files. com.android.support:appcompat-v7:+已经包含com.google.android:support-v4,这导致双重包含相同的类文件。

I'm now looking for answers like: Use Ant Download all dependencies into 'libs' folder and use compile files('...') etc. 我现在正在寻找以下答案:使用Ant将所有依赖项下载到'libs'文件夹并使用编译文件('...')等。

Change inclusion order olso doesn't help. 更改包含顺序olso没有帮助。

I'm looking for robust and convenient solution. 我正在寻找强大而方便的解决方案。 How to use maven dependencies and still e free from exceptions described above? 如何使用maven依赖关系,仍然没有上述异常? Ideas? 想法? How mature android developers manage this? 成熟的android开发者如何管理这个?

You can exclude transitive dependencies : 您可以排除传递依赖项

dependencies {
    compile 'com.android.support:appcompat-v7:19.0.0'
    compile ("com.github.chrisbanes.bitmapcache:library:2.3"){
        exclude group: 'com.google.android', module: 'support-v4'
    }
}

It is opensourced library so you should notify the author about this issue or create pull request as well. 它是开源库,因此您应该通知作者有关此问题或创建拉取请求。

The problem is that bitmapcache specifically requires r7 of the support library, rather than allowing any r7+ version of the support library. 问题是bitmapcache特别需要支持库的r7,而不是允许支持库的任何r7 +版本。 Hence appcompat is attempting to pull in the latest (r19) which is conflicting with the required r7 of bitmapcache. 因此appcompat试图引入与bitmapcache所需的r7冲突的最新版本(r19)。

ActionBarSherlock, for example, does not specify a version of the support library as per its pom file which allows you to use it alongside any version of the support library. 例如,ActionBarSherlock没有根据其pom文件指定支持库的版本,这允许您将其与任何版本的支持库一起使用。

The easiest solution is for bitmapcache to update to allow for later versions of the support library. 最简单的解决方案是更新bitmapcache以允许更高版本的支持库。 Otherwise, you'll need to download and manage the dependency locally as an additional module in your project. 否则,您需要在本地下载和管理依赖项作为项目中的附加模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM