Fixing dependency conflicts in Gradle.

Eoin Ahern
3 min readOct 13, 2017

--

So yesterday I started to refactor the google android architecture examples. Specifically the clean mvp branch. which is an example of a simple todo list application.

I added a few libraries to my build.gradle and the following error occured!

Error:Execution failed for task ‘:app:prepareMockDebugAndroidTestDependencies’.
> Dependency Error. See console for details.

this is a regular occurrence and I thought id take some time to write a guide on how to resolve these so called ‘Transitive Dependencies’.

Directly after adding Dagger and Butterknife dependencies to my build.gradle the messages console showed up with a number of warnings. (see below)

So, What exactly does this mean? Well, from the provided link http://g.co/androidstudio/app-test-app-conflict it tells us.

“When instrumentation tests are run, both the main APK and test APK share the same classpath. Gradle build will fail if the main APK and the test APK use the same library (e.g. Guava) but in different versions. If gradle didn’t catch that, your app could behave differently during tests and during normal run (including crashing in one of the cases).”

Meaning Daggers and Butterknife’s dependencies are in conflict with whats in the test APK.

Solution

The first thing I do when I have this issue is use the console command.

> gradlew androidDependencies

This outputs a dependency tree for the application. which can be huge. In my case it is as The application has a number of build flavors. This would be kind of hard to parse with the eyes. Using control + F we can look for the conflicting sub-dependencies and thus discover which direct dependency it belongs to. Shown below is a screen shot of the tree which shows the conflict between Butterknife and the com.android.support.test:runner. notice com.android.support:support-annotations version number differs.

Finally its time to fix the problem. This can be accomplished a few ways.

One approach we could take would use configurations.all method and force a resolution strategy shown below.

now we can see in the messages tab that this warning is gone. hooray!

note less warnings. wow

now ill attempt to get rid of the com.google.code.findbugs:jsr305 conflict using a different method. Ill do this by excluding the module. First ill need to find which of my dependencies have this sub-dependency. I did this again using control + F after outputting the dependency tree. shown is how I managed to exclude the earlier version com.google.code.findbugs:jsr305.

now only one dependency conflict remained on support-compat. The easiest way to fix this was to use the above method of forcing the resolution and the build was fixed.

Finally, I tried just updating to the latest version of the android support library (26.1.0 when writing this post) without my previous changes and the build worked. This was by far the quickest solution and I overlooked it initially. Oh well! That shows its important to keep your dependencies up to date.

heres my final build.gradle dependencies method.

--

--