Dependency constraints in Gradle Kotlin Multiplatform plugin

Is it possible to use Gradle dependency constraints (Upgrading versions of transitive dependencies) with the Gradle Kotlin Multiplatform plugin? I would like to be able to specify the version to use for a dependency in the parent project and have it automatically applied in the child project as shown in the example below.

This script fails to compile as definition for the constraints block cannot be found.

Thanks in advance.

settings.gradle

rootProject.name = 'parent'
include(':child')

build.gradle.kts (Parent project)

plugins {
    kotlin("multiplatform") version "1.3.72"
}

...

subprojects {
    kotlin {
        sourceSets {
            jvm().compilations["main"].defaultSourceSet {
                dependencies {
                    constraints { // <= Compilation error: Unresolved reference: constraints
                        implementation("org.hibernate:hibernate-core:5.4.3.Final")
                    }
                }
            }
        }
    }
}

child/build.gradle.kts (Child project)

plugins {
    kotlin("multiplatform")
}

kotlin {
    jvm {
        ...
    }

    sourceSets {
        jvm().compilations["main"].defaultSourceSet {
            dependencies {
                implementation("org.hibernate:hibernate-core")
            }
        }
    }
}
1 Like

I’m struggling with the exact same issue, anybody?

I asked the same question in slack and got an answer:
Jendrik Johannes 18 minutes ago

Looks like dependencies { } inside a Kotlin target block does not support constraints. That’s something the Kotlin plugin adds (not part of Gradle core).But the mechanism is only an alternative to Gradle’s core dependency declaration mechanism.
You can use that instead to define dependencies and constraints .
Basically:
kotlin.sourceSets.commonMain.dependencies.implementation
is the same as:
dependencies.commonMainImplementation

dependencies {
    commonMainImplementation("...") // <-- dependency
    constraints {
         commonMainImplementation("...") // <-- dependency constraints
    }
}