How to enforce strict version on transitive dependencies and fail the build


I am trying to enforce strict versioning on transitive dependencies but couldn’t make it work. If any library tries to bring a version outside of the major version I set; this should fail the build.

Here is my setup:

App → LibraryA → LibraryB:v2

App → LibraryB:v3

  • I want to enforce that my app only uses v3 of LibraryB.
  • If any library tries to bring in v2 or v4; fail the build.

I tried adding strictly("[3.0.0, 4.0.0[") but this doesn’t work.

I need this because there are binary incompatible changes between v2 and v3 of the LibraryB. If my app depends on v3, but LibraryA was compiled with v2 I will get runtime errors.


A strict version is not what you want then.
A strict version just forces that version.
It only fails if some other strict version for that dependency appears too.

To fail, you probably need a resolution strategy that fails if a version outside your range is requested or something like that.

1 Like

Thank you!

I was able to get it working by doing something like this. It would have been awesome if there was a possibility to enforce this when declaring the dependency but this works for now.

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.module.toString() == 'com.example:module') {
            if (!(details.requested.version.startsWith('3.'))) {
                throw new GradleException("${details.requested.module} dependency must be within the major version 3")
1 Like