Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to manage Android Gradle dependencies

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?

like image 369
Viktar Patotski Avatar asked Nov 15 '13 18:11

Viktar Patotski


People also ask

Where should I add dependencies in Gradle project?

Gradle declares dependencies on JAR files inside your project's module_name /libs/ directory (because Gradle reads paths relative to the build.gradle file). This declares a dependency on version 12.3 of the "app-magic" library, inside the "com.example.android" namespace group.

How do I resolve Gradle dependencies?

Obtaining module metadata Given a required dependency, with a version, Gradle attempts to resolve the dependency by searching for the module the dependency points at. Each repository is inspected in order. Depending on the type of repository, Gradle looks for metadata files describing the module ( .

What is Gradle dependency management?

In most cases, a project relies on reusable functionality in the form of libraries or is broken up into individual components to compose a modularized system. Dependency management is a technique for declaring, resolving and using dependencies required by the project in an automated fashion.

What is the dependency manager for Android?

In Android side, things are simpler, as it's main IDE Android Studio is actually IntelliJ that has uses Gradle as it's dependency management within. No additional installation is needed, nor setup. Once you setup the Android project, your Gradle file is all setup accordingly.


1 Answers

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.

like image 159
Sergii Pechenizkyi Avatar answered Oct 20 '22 03:10

Sergii Pechenizkyi