Elye
Elye

Reputation: 60251

What's the use of constraint in Gradle dependencies resolution?

There is a library i.e. io.github.elye:simplekotlinlibrary with version 1.0.0, 2.0.0 and 3.0.0.

The io.github.elye:easyandroidlibrary:1.0.0 contain io.github.elye:simplekotlinlibrary:1.0.0

The io.github.elye:simpleandroidlibrary:2.0.0 contain io.github.elye:simplekotlinlibrary:2.0.0

So in my Gradle, if I have

    implementation ('io.github.elye:easyandroidlibrary:1.0.0')
    implementation ('io.github.elye:simpleandroidlibrary:2.0.0')
    implementation ('io.github.elye:simplekotlinlibrary:3.0.0')

It will auto-resolve to io.github.elye:simplekotlinlibrary:3.0.0 for the project including the transitive dependencies.

However, with https://docs.gradle.org/current/userguide/dependency_constraints.html#sec:adding-constraints-transitive-deps, it states that the constrains also force an upgrade of dependencies to the indicated version 3.0.0.

    implementation ('io.github.elye:easyandroidlibrary:1.0.0')
    implementation ('io.github.elye:simpleandroidlibrary:2.0.0')
    implementation ('io.github.elye:simplekotlinlibrary')

    constraints {
        implementation('io.github.elye:simplekotlinlibrary:3.0.0') {
            because 'testing force upgrade'
        }
    }

From my understanding, the above two examples will produce the same result. Seems like constraint has no real use? How does constraint differ from the differ auto dependencies resolution?

Upvotes: 8

Views: 12997

Answers (1)

Elye
Elye

Reputation: 60251

I think in the above example they are the same.

However in the event that the top level project doesn't need io.github.elye:simplekotlinlibrary, using constraint, one can still force all transitive dependencies to at-least the version indicated by the constraint as shown below.

    implementation ('io.github.elye:easyandroidlibrary:1.0.0')
    implementation ('io.github.elye:simpleandroidlibrary:2.0.0')

    constraints {
        implementation('io.github.elye:simplekotlinlibrary:3.0.0') {
            because 'testing force upgrade'
        }
    }

Understand Gradle Dependency Resolution Easier gives a further detailed illustration.

Upvotes: 5

Related Questions