Hello! I'm trying to define a gradle convention pl...
# gradle
g
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:
Copy code
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
Copy code
public fun Project.kmpp(action: Action<KMPPExtension> = Action {}) {
    action.invoke(the())
    // configure here
}
rename the real extension creation name to something like InternalKmpp
m
Is 'kmpp' your own extension?
j
I think so, if not my approach is useless
👍 1
g
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:
Copy code
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
I haven't it without the source set part
you probably can get rid of them for sure
g
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
I haven't added a default target and I get it working
message has been deleted
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
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
no
that kotlin it is not the default kotlin
it is a custom one
this doesn't exist in the default Kotlin extension
Copy code
kotlin {
    multiplatform { ... }
}
👍 1
g
How do you define the sourceSets ?
j
message has been deleted
that is the real Kotlin extension
which I would avoid in my projects, it is only a fallback for complex use cases
message has been deleted
message has been deleted
message has been deleted
nothing special
g
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
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
🙏 1