Question

I'm using 'com.android.tools.build:gradle:0.6.+' to build my android application.

It's known fact that Android dex can't contain different versions of same library. But how to handle situation when Maven dependency you want to use requires some library that you already using, but just different version. Example. Having following in build.gradle file:

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

And com.android.support:appcompat-v7:+ already includes com.google.android:support-v4, what results in double inclusion of the same class files.

I'm now looking for answers like: Use Ant Download all dependencies into 'libs' folder and use compile files('...') etc.

Change inclusion order olso doesn't help.

I'm looking for robust and convenient solution. How to use maven dependencies and still e free from exceptions described above? Ideas? How mature android developers manage this?

Was it helpful?

Solution

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.

OTHER TIPS

The problem is that bitmapcache specifically requires r7 of the support library, rather than allowing any r7+ version of the support library. Hence appcompat is attempting to pull in the latest (r19) which is conflicting with the required r7 of bitmapcache.

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.

The easiest solution is for bitmapcache to update to allow for later versions of the support library. Otherwise, you'll need to download and manage the dependency locally as an additional module in your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top