https://kotlinlang.org logo
Title
g

Grégory Lureau

07/08/2022, 9:51 AM
Hello! I'm trying to define a gradle convention plugin with parameters on a KMP project, and I want to be able to enable platforms on demand, something like:
plugins {
    id("com.deezer.kmp.convention-plugin") version "0.1.0"
}
kmpp {
    platformJvm.set(true)
}
but I've a couple of issues: • when I use a Property (from an abstract Extension), the value is accessible only if I use
afterEvaluate
• (I think) the multiplatform plugin expects to be configured in the configuration pass , and what I do in the afterEvaluate is too late Is it possible to listen Property changes so that I can apply multiplatform plugin when the value is set? (I suppose it's still the configuration phase) Any help or idea is much appreciated.
j

Javier

07/08/2022, 9:55 AM
public fun Project.kmpp(action: Action<KMPPExtension> = Action {}) {
    action.invoke(the())
    // configure here
}
rename the real extension creation name to something like InternalKmpp
m

mbonnin

07/08/2022, 10:15 AM
Is 'kmpp' your own extension?
j

Javier

07/08/2022, 10:16 AM
I think so, if not my approach is useless
👍 1
g

Grégory Lureau

07/08/2022, 11:16 AM
Yes it is. Thanks for the trick, I'll test that right now!
Yay ! Big thanks @Javier and @mbonnin I had to adds the sourcesets configuration as a 2nd parameter of the extension function due to the restriction of KMP to have at least one platform defined, but it's fine for me. Just if someone found this conversation, here is the result:
public fun Project.kmpp(
    action: Action<KmppConventionPluginExtension> = Action {},
    sourceSetsConfigure: Action<NamedDomainObjectContainer<KotlinSourceSet>> = Action {}
) {
    action(the())
    val ext = extensions["kmppExt"] as KmppConventionPluginExtension
    plugins.apply("org.jetbrains.kotlin.multiplatform")
    val kotlin = extensions.getByName<KotlinMultiplatformExtension>("kotlin")
    if (ext.platformJvm.get()) kotlin.jvm()
    // More configuration
    sourceSetsConfigure(kotlin.sourceSets)
}
I was expecting to be able to use
kotlin { ...}
directly after my extension function, but it's tricky in my case : if the multiplatform plugin is applied in the
Plugin
apply method, it fails because there is no targets, and if it's in the extension function then
kotlin { ... }
is not available in the build.gradle.kts as the plugin has not been applied yet.
j

Javier

07/08/2022, 11:51 AM
I haven't it without the source set part
you probably can get rid of them for sure
g

Grégory Lureau

07/08/2022, 11:57 AM
If I've always one target enabled by default I don't need this trick for sure, but that's not the case on my current project. Also limiting to the sourceSets seems like a good idea for now, but I guess I'll have to experiment more before seeing some limitations.
j

Javier

07/08/2022, 12:03 PM
I haven't added a default target and I get it working
I even configure the source set to remove
src
part with no issues
If I remove all targets in that extension, KMP plugin fails with the typical message that you need at least one target
g

Grégory Lureau

07/08/2022, 12:06 PM
Oh yes ok, I presume it's because you have the kotlin {} block in the method itself. I was trying to use it after the ext func (
hubdle
)
j

Javier

07/08/2022, 12:06 PM
no
that kotlin it is not the default kotlin
it is a custom one
this doesn't exist in the default Kotlin extension
kotlin {
    multiplatform { ... }
}
👍 1
g

Grégory Lureau

07/08/2022, 12:07 PM
How do you define the sourceSets ?
j

Javier

07/08/2022, 12:07 PM
that is the real Kotlin extension
which I would avoid in my projects, it is only a fallback for complex use cases
nothing special
g

Grégory Lureau

07/08/2022, 12:10 PM
Okay thanks, it makes sense then (I was not clear about the execution order). Thanks for your snippets, I'll work more on that 🤩🙏
j

Javier

07/08/2022, 12:11 PM
you're welcome 🙂
I am going to remove the marker so the nested kotlin extension (the original Kmp extension) renders as yellow instead of that purple
Anyway if you need to copy something more, you can check it here: https://github.com/JavierSegoviaCordoba/hubdle
:thank-you: 1