a

    Azur Haljeta

    1 year ago
    Hello, why it isn't possible to use the multiplatform plugin with
    apply (plugin = ...)
    but it is within the
    plugins { }
    block?
    Javier

    Javier

    1 year ago
    Why is it more flexible?
    a

    Azur Haljeta

    1 year ago
    because you can do something like
    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
    gildor

    gildor

    1 year ago
    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
    louiscad

    louiscad

    1 year ago
    You can use
    apply false
    in the plugins DSL and call
    apply
    conditionally if you really need it that way.
    a

    Azur Haljeta

    1 year ago
    what's wrong with this script, such a simple example doesn't work
    is this also related to the Kotlin DSL
    louiscad

    louiscad

    1 year ago
    Well, look at the official examples in the docs, where they also apply the right android plugin
    gildor

    gildor

    1 year ago
    I think error message is pretty explanatory
    louiscad

    louiscad

    1 year ago
    Yep, there's a link to the doc in that error message @Azur Haljeta
    gildor

    gildor

    1 year ago
    Mpp plugin complaining that you didn't configure target platform
    louiscad

    louiscad

    1 year ago
    @gildor If you look at the applied plugins, you can see the culprit I think 😉
    Javier

    Javier

    1 year ago
    @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

    1 year ago
    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
    louiscad

    louiscad

    1 year ago
    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

    1 year ago
    @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.
    louiscad

    louiscad

    1 year ago
    apply
    alone will NEVER work with Kotlin DSL, it only works with dynamic Groovy
    Javier

    Javier

    1 year ago
    I have a multimodule project with dozens of multiplatform modules and I only use
    plugins
    block 🤷‍♂️
    louiscad

    louiscad

    1 year ago
    Same
    a

    Azur Haljeta

    1 year ago
    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!
    Javier

    Javier

    1 year ago
    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
    louiscad

    louiscad

    1 year ago
    @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.
    a

    Azur Haljeta

    1 year ago
    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
    louiscad

    louiscad

    1 year ago
    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:
    plugins {
        kotlin("multiplatform") apply false // Applied conditionally later
    }
    a

    Azur Haljeta

    1 year ago
    that's very useful, thanks @louiscad