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

Azur Haljeta

11/04/2020, 9:05 PM
Hello, why it isn't possible to use the multiplatform plugin with
apply (plugin = ...)
but it is within the
plugins { }
block?
j

Javier

11/04/2020, 9:09 PM
Why is it more flexible?
a

Azur Haljeta

11/04/2020, 9:24 PM
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

gildor

11/04/2020, 10:41 PM
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

louiscad

11/04/2020, 10:44 PM
You can use
apply false
in the plugins DSL and call
apply
conditionally if you really need it that way.
👍 1
☝️ 1
a

Azur Haljeta

11/05/2020, 9:42 AM
what's wrong with this script, such a simple example doesn't work
is this also related to the Kotlin DSL
l

louiscad

11/05/2020, 9:50 AM
Well, look at the official examples in the docs, where they also apply the right android plugin
g

gildor

11/05/2020, 9:51 AM
I think error message is pretty explanatory
l

louiscad

11/05/2020, 9:51 AM
Yep, there's a link to the doc in that error message @Azur Haljeta
g

gildor

11/05/2020, 9:51 AM
Mpp plugin complaining that you didn't configure target platform
l

louiscad

11/05/2020, 9:52 AM
@gildor If you look at the applied plugins, you can see the culprit I think 😉
j

Javier

11/05/2020, 10:13 AM
@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

Azur Haljeta

11/05/2020, 10:53 AM
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

louiscad

11/05/2020, 10:54 AM
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

Azur Haljeta

11/05/2020, 10:58 AM
@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

louiscad

11/05/2020, 10:59 AM
apply
alone will NEVER work with Kotlin DSL, it only works with dynamic Groovy
j

Javier

11/05/2020, 11:00 AM
I have a multimodule project with dozens of multiplatform modules and I only use
plugins
block 🤷‍♂️
l

louiscad

11/05/2020, 11:00 AM
Same
a

Azur Haljeta

11/05/2020, 11:00 AM
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

Javier

11/05/2020, 11:00 AM
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

louiscad

11/05/2020, 11:01 AM
@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

Azur Haljeta

11/05/2020, 11:03 AM
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

louiscad

11/05/2020, 11:05 AM
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

Azur Haljeta

11/05/2020, 11:07 AM
that's very useful, thanks @louiscad