How to force a version of a transitive dependency without hardcoding the version number

Hi folks, Is there any way to specify that I want to use whatever version of a given transitive dependency is specified by some other dependency?

For example, I might have 2 first-level dependencies that both specify the same transitive dependency but different versions of it. How can I say that I want to use the version of the transitive that is specified by one of the first-level dependencies whatever that version may be.

I’ve read up on how to force a given version of a dependency but this involves hardcoding the version myself. I want to avoid hardcoding the version so that when I upgrade to a new version of my first-level dependency I automatically get the version of the transitive dependency that they require.

If you want to get any version of a transitive dependency specified by some other dependency, you don’t have to force anything, and you’ll get the highest version of the candidates. Does this answer your question?

Hi Peter, That’s not quite what I was after but I can see that my example was a bit ambiguous.

Let’s say I have a first-level dependency named firstLevel1 and another named firstLevel2. They both have a transitive dependency on a library called secondLevel1. I want to be able to say that I will always take the version of secondLevel1 that is specified by firstLevel1, regardless of what is specified by firstLevel2.

There’s currently no out-of-the-box option for that. You might be able to implement this yourself though based on the APIs that Gradle exposes.

Ok, thanks anyway. I think its probably a dubious thing to be doing anyway because its more likely to lead to runtime errors.

I may want to implement dependency conflict resolution mechanisms other than “newest” and “fail”.

Which Gradle APIs, specifically, should I use?

Hi Lars,

There’s currently no real public way to plug in your own conflict resolver. You’d have to fork Gradle. This is absolutely something we are actively working towards though. It’s not a trivial thing.

In Gradle 1.3, we are exposing a brand new API that gives you full visibility of the resolved depenency graph post resolution. What this means is that you can implement your own conflict failure conditions. The new API is based on the ResolutionResult class, which you get via:


What you can do there is inspect the resolved graph and fail the build if the conflict wasn’t resolved in the way you want. You’d then fix the problem by putting in exclude or force rules in your dependency definitions.

Gradle 1.3. will be released within the next few weeks.