我正在使用“com.android.tools.build:gradle:0.6.+”来构建我的 android 应用程序。
众所周知,Android dex 不能包含同一库的不同版本。但是当你想使用 Maven 依赖需要一些你已经在使用的库,但只是版本不同时,如何处理这种情况。 例子。 在 build.gradle 文件中有以下内容:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.github.chrisbanes.bitmapcache:library:2.3'
}
产生错误:
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)...
发生此错误的原因如下: http://search.maven.org/remotecontent?filepath=com/github/chrisbanes/bitmapcache/library/2.3/library-2.3.pom需要 com.google.android:support-v4
并且 com.android.support:appcompat-v7:+ 已经包含 com.google.android:support-v4,这会导致重复包含相同的类文件。
我现在正在寻找如下答案: 使用 Ant 将所有依赖项下载到“libs”文件夹并使用编译文件(“...”) 等
更改包含顺序也无济于事。
我正在寻找可靠且方便的解决方案。如何使用 Maven 依赖项并且仍然没有上述异常?想法?成熟的 Android 开发人员如何管理这个?
最佳答案
您可以 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'
}
}
它是开源库,因此您应该将此问题通知作者或同时创建拉取请求。
关于java - 管理 Android Gradle 依赖项的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20008525/