Hello, why it isn't possible to use the multiplat...
# multiplatform
a
Hello, why it isn't possible to use the multiplatform plugin with
apply (plugin = ...)
but it is within the
plugins { }
block?
j
Why is it more flexible?
a
because you can do something like
Copy code
if (something){
	apply (...)
}
this is useful when you would like to link a KMM project directly into android studio, and both reference the kotlin plugin, so you must write a logic where you apply that plugin only if it isn't already applied
and that's why it is flexible you can't do that when using plugins via
plugins {}
at least I'm not aware of
g
This is not something multiplatform plugin related, please, check Kotlin DSL documentation Plugins dsl is the only way to generate type safe accessors for plugins. It also recommended way to apply plugins in general, buildscript is legacy mechanism https://docs.gradle.org/current/userguide/kotlin_dsl.html#type-safe-accessors
l
You can use
apply false
in the plugins DSL and call
apply
conditionally if you really need it that way.
👍 1
☝️ 1
a
what's wrong with this script, such a simple example doesn't work
is this also related to the Kotlin DSL
l
Well, look at the official examples in the docs, where they also apply the right android plugin
g
I think error message is pretty explanatory
l
Yep, there's a link to the doc in that error message @Azur Haljeta
g
Mpp plugin complaining that you didn't configure target platform
l
@gildor If you look at the applied plugins, you can see the culprit I think 😉
j
@Azur Haljeta you need you add the android library plugin too and setup the android config like a normal library outside of kotlin, and add android() and its sourcesets inside kotlin
a
ok ok thanks, the error message is somewhat misleading but still reasonable
but how about this? what's wrong with this one
I'm intentionally pasting whole screenshots so that the script as well as the error logs are shown in a coherent form
l
We already told you why this one doesn't work, plus I think it's not at the right place
You can use the KMM plugin to make a "playground" project and see how the working config from the template is done. See the getting started docs here: https://kotlinlang.org/docs/mobile/getting-started.html
a
@louiscad I've already done that and all is fine, but I need to tweak the configuration since it's a multi-module project and not beginner stuff. Because of that, I need to demystify how the plugin works and why it doesn't work in Kotlin DSL with the
apply
notation. I already have read most of the docs and that's why I'm asking the community since my issue is specific, and I don't think that routing back to the very beginner guides will help.
l
apply
alone will NEVER work with Kotlin DSL, it only works with dynamic Groovy
j
I have a multimodule project with dozens of multiplatform modules and I only use
plugins
block 🤷‍♂️
l
Same
a
apply
 alone will NEVER work with Kotlin DSL, it only works with dynamic Groovy
That's what I was missing the whole time. Thank you all!
j
I think the problem you have is a design error in your build logic and you are trying to solve it in the wrong way but I am not a Gradle expert
👍 1
l
@Azur Haljeta I think you're confusing the root "project" with the sub-projects/modules. In your last screenshot, you were adding config in the root project that should target subprojects
Most plugins are not designed to be applied to the root project and propagate to child modules. At least, that's the case for the Kotlin multiplatform and Android plugins.
👍 1
a
the screenshots are just tiny hello-world projects for gradle, not related to anything specific what I really need to do is decouple the KMM project (which includes an android app module) into two projects for two repositories, one being pure KMM and the other one is an android app which links to the KMM via
settings.gradle
and that setup works when using groovy gradle but not for kotlin DSL
l
I'm not sure I follow everything, but I can tell you that Kotlin can work like Groovy if you also use the plugins DSL +
apply false
in it. Example:
Copy code
plugins {
    kotlin("multiplatform") apply false // Applied conditionally later
}
👍 1
💯 1
a
that's very useful, thanks @louiscad