Kotlin-bom in multiplatform projects?

I was told that it is generally recommended to add kotlin-bom as a dependency to all projects to make sure all Kotlin components are version-aligned. My gradle code looks like this:

plugins {
    kotlin("multiplatform") version "1.4.0" apply false
}

allprojects {
    repositories {
        jcenter()
    }
    dependencies {
        implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
    }
}

This fails with an “Unresolved reference: implementation” error. From what I gather, this is because the Kotlin plugin isn’t actually applied here, and implementation is part of the Kotlin Gradle DSL.

(I don’t apply that plugin because in the root Gradle script I don’t actually declare any kotlin targets, so I’d get “Please initialize at least one Kotlin target” errors. I only want to pin the kotlin-multiplatform version to 1.4.0 in the root script, that’s all.)

But now I am wondering how to apply kotlin-bom. I could add the implementation line for each platform (in commonMain, commonTest, linuxMain, linuxTest etc.) but there has to be a better way that does not require such a repetition, right?

1 Like

I’m not entirely sure, but "implementation"(platform("...")) could work

Doesn’t work. implementation works if I use it in a subproject, but then, platform isn’t known. I suppose this is because I do not add the java-platform plugin to the build.gradle.kts file of the subproject. Source.

So I now wonder, do I have to add java-platform to add the kotlin-bom? Wouldn’t this collide with Kotlin/Native for example? Isn’t there something like kotlin-platform?

1 Like

The platform() function is not available in Kotlin Multiplatform plugin’s KotlinDependencyHandler but only in Gradle’s standard DependencyHandler.

As stated in YouTrack KT-40489 Kotlin team does not want to add support for it (at least for now) so as a workaround you can call the Gradle’s DependencyHandler directly:

    dependencies {
        implementation(project.dependencies.platform("org.jetbrains.kotlin:kotlin-bom"))
    }
5 Likes