https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

janvladimirmostert

09/30/2020, 3:58 PM
For Kotlin Multimodule Multiplatform projects, how does one configure the plugin block? Currently doing it per sub project
Copy code
plugins {
	val kotlinVersion = "1.4.10"
	kotlin("multiplatform") version kotlinVersion
	kotlin("plugin.serialization") version kotlinVersion
}
but it no longer compiles complaining that i'm redefining the Kotlin plugin i guess this now needs to move to the
Copy code
allprojects {
block in the root build.gradle.kts i'm trying
Copy code
allprojects {
  	plugins.apply(kotlin("multiplatform"))
}
but that's not compiling any ideas?
m

mbonnin

09/30/2020, 4:01 PM
What you can do is define the version in the root project without applyting them:
Copy code
//root/build.gradle.kts
plugins {
  kotlin("multiplatform").version("1.4.10").apply(false)
}
Then in your individual modules apply them without version:
Copy code
//root/module/build.gradle.kts
plugins {
  kotlin("multiplatform")
}
j

janvladimirmostert

09/30/2020, 4:02 PM
that's nifty! i take it i can just apply(true) to have it applied to all submodules as well, all modules are Kotlin
m

mbonnin

09/30/2020, 4:04 PM
Copy code
plugins {
  kotlin("multiplatform")
}
is the same as
Copy code
plugins {
  kotlin("multiplatform").apply(true)
}
j

janvladimirmostert

09/30/2020, 4:06 PM
is the plugin definition required in teh submodules if apply(true) is in the root ?
m

mbonnin

09/30/2020, 4:07 PM
You don't want to
apply(true)
from the root project since there's no kotlin file to compile there
What
kotlin("multiplatform").version("1.4.10").apply(false)
in the root project does is that it adds the Kotlin plugin jar to the buildscript classpath.
This ensures that this is made in a single central place
j

janvladimirmostert

09/30/2020, 4:08 PM
this is brilliant, you just saved me from having to redefine versions all over the place, thanks for this! can the same be done with dependencies ?
m

mbonnin

09/30/2020, 4:09 PM
You can look into java platforms for dependencies
j

janvladimirmostert

09/30/2020, 4:10 PM
i see dependencies block works in allprojects, so i guess the answer is yes
m

mbonnin

09/30/2020, 4:10 PM
Basically you define a "platform" module that centralizes all the different versions and that all your other modules depend on https://docs.gradle.org/current/userguide/java_platform_plugin.html
j

janvladimirmostert

09/30/2020, 4:10 PM
thanks for the help Martin!
m

mbonnin

09/30/2020, 4:11 PM
you can use
allprojects {}
too
Although you're certainly more looking for
subprojects {}
to exclude the root project (which doesn't contain any kotlin in typical cases)
j

janvladimirmostert

09/30/2020, 4:14 PM
yeah, i've got a build.finished block which loops through all projects and it contains the root project which i currently have to exclude
Copy code
gradle.buildFinished {

		allprojects.forEach { subproject ->
		val lib = "$buildDir/library/${project.name}-${subproject.name}"
subprojects might be a better solution here too thanks for the help Martin, this is going to make my KMMMP setup much cleaner 😄