CLOVIS
03/14/2023, 9:44 PMkotlin("jvm")
, kotlin("js")
or kotlin("multiplatform")
. Is it possible to create a convention plugin that configures all of them? I don't want to create one plugin per Kotlin plugin…mbonnin
03/14/2023, 9:46 PMmyExtension {
jvm {
// stuff
}
js {
// foo
}
multiplatform {
// bar
}
}
pluginManager.withId("org.jetbrains.kotlin.jvm") {
// configure JVM
}
CLOVIS
03/14/2023, 9:47 PMwithId
, can I still use the kotlin {}
block inside of it?mbonnin
03/14/2023, 9:48 PMCLOVIS
03/14/2023, 9:48 PMmbonnin
03/14/2023, 9:48 PMkotlin-dsl
you can have generated accessors I think but I never use thoseCLOVIS
03/14/2023, 9:48 PMkotlin-dsl
, yesmbonnin
03/14/2023, 9:49 PM(project.extensions.getByName("kotlin") as KotlinJvmExtension).apply {}
works all the time, regardless of what plugins you applyset the jvmTarget/language version/API version…That should be pretty doable
tasks.withType(KotlinCompile::class.java).configureEach {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + listOf(
"-opt-in=kotlin.RequiresOptIn",
)
apiVersion = "1.5"
languageVersion = "1.5"
(this as? KotlinJvmOptions)?.let {
it.jvmTarget = "1.8"
}
}
}
CLOVIS
03/14/2023, 9:51 PMKotlinCompile
) but doesn't need to apply it, and the final build can apply whichever it wants?mbonnin
03/14/2023, 9:57 PMcompileOnly
dependencyCLOVIS
03/14/2023, 9:58 PMimplementation
?mbonnin
03/14/2023, 9:58 PMimplementation
and that'd most likely work but it's sending the wrong messageCLOVIS
03/14/2023, 10:01 PMcompileOnly
, Gradle won't know about it in the downstream project, so it risks selecting an incompatible version, right?
Whereas, if it's implementation
, it'll select the highest requested version during resolution anyway so it doesn't block the users?mbonnin
03/14/2023, 10:03 PMimplementation("kgp:1.8.20")
and I as a user do this:
plugins {
id("org.jetbrains.kotlin.jvm").version("1.7.0")
}
2 things can happen (I'm not sure which one actually)
1. 1.8 gets selected and as a user I am very surprised
2. 1.7 gets selected and your plugin might crash on missing KGP symbolscompileOnly
CLOVIS
03/14/2023, 10:04 PMVampire
03/15/2023, 7:53 AMIf you're using the kotlin-dsl you can have generated accessors I think but I never use those
No. You only got the generated type-safe accessors for plugins applied using the
plugins { ... }
block. For all other cases you have to use the more verbose syntax.
But you can use
configure<KotlinJvmExtension> { ... }
instead of
(project.extensions.getByName("kotlin") as KotlinJvmExtension).apply { ... }
tapchicoma
03/15/2023, 9:02 AMOr you can just "react" to the plugin the user has applied:Better to react on `org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin`:
plugins.withType<org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin>() {
// Do something
}
CLOVIS
03/15/2023, 9:04 AMtapchicoma
03/15/2023, 9:09 AMtasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask>().configureEach {
compilerOptions {
languageVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0)
// Other common compilation options
}
}
If you want to configure specific platform compilation options - it could be a little trickier. There is org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
task interface for Kotlin/JVM, but not for other platforms. And you need to use specific task types in this case 😞CLOVIS
03/15/2023, 9:10 AMtapchicoma
03/15/2023, 9:13 AMextensions.configure<org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension> {
jvmToolchain(17)
}
CLOVIS
03/15/2023, 9:15 AMtapchicoma
03/15/2023, 9:16 AMkotlin("jvm")
and kotlin("js")
plugins, but MPP will be done in later releases due to its complexityCLOVIS
03/15/2023, 9:17 AMVampire
03/15/2023, 9:38 AMBetter to react on `org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin`:
```plugins.withType<org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin>() {
// Do something
}```Actually not. The JavaDoc of
plugins
is telling you not to use it.
The better way is what @mbonnin meant to say above, pluginManager.withPlugin(...) { ... }
tapchicoma
03/15/2023, 10:38 AMVampire
03/15/2023, 10:39 AMProject#getPlugins
🙂CLOVIS
03/16/2023, 7:28 PMVampire
03/16/2023, 7:33 PMCLOVIS
03/16/2023, 7:34 PMplugins.withType<???> { … }
But nevermind, there's also plugins.withId("com.android.application") {}
Vampire
03/16/2023, 7:34 PMpluginManager.withPlugin
plugins
should not be used, look at its JavaDocCLOVIS
03/16/2023, 7:35 PM