Guilherme Delgado
06/20/2022, 2:10 PMclass KotlinJvmOptionsPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
extensions.configure<KotlinJvmOptions> {
jvmTarget = "11"
freeCompilerArgs = freeCompilerArgs + listOf("-Xopt-in=kotlin.RequiresOptIn")
}
}
}
}
but it will fail with:
Caused by: org.gradle.api.UnknownDomainObjectException: Extension of type 'KotlinJvmOptions' does not exist. Currently registered extension types: [ExtraPropertiesExtension, BasePluginExtension, DefaultArtifactPublicationSet, SourceSetContainer, ReportingExtension, JavaPluginExtension, JavaToolchainService, NamedDomainObjectContainer<BaseVariantOutput>, LibraryExtension, LibraryAndroidComponentsExtension]
How can I solve this and have access to kotlinOptions
? Thanks 🙂Guilherme Delgado
06/20/2022, 2:18 PMhfhbd
06/20/2022, 2:20 PMproject.configure<FooExtension> { }
Vampire
06/20/2022, 2:37 PMKotlinJvmOptions
simply is not an extension, so you cannot configure it. I guess LibraryExtension
or CommonExtension
is the extension you want to configure.
You also don't need extensions.
, you can call configure
on the project directly and thus just call configure
as the project is this
in that lambda anyway.
But one important thing to notice, if you write it like that, you introduce an ordering constraint for plugins. Your plugin will onyl work if it is applied after the Kotlin plugin. Such ordering constraints for plugins are bad practice. Better use pluginManager.withPlugin
to react to the Kotlin plugin being applied for configuring it.Guilherme Delgado
06/20/2022, 2:44 PMclass KotlinJvmOptionsPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
configure<LibraryExtension> {
(this as ExtensionAware).extensions.configure<KotlinJvmOptions>("kotlinOptions") {
jvmTarget = "11"
freeCompilerArgs = freeCompilerArgs + listOf("-Xopt-in=kotlin.RequiresOptIn")
}
}
}
}
}
Guilherme Delgado
06/20/2022, 2:45 PMBut one important thing to notice, if you write it like that, you introduce an ordering constraint for plugins. Your plugin will onyl work if it is applied after the Kotlin plugin. Such ordering constraints for plugins are bad practice. Better useThis is a good advice because i was having this problem and I didn’t knew what it was. 🙌to react to the Kotlin plugin being applied for configuring it.pluginManager.withPlugin
Guilherme Delgado
06/20/2022, 2:46 PMpluginManager.withPlugin
your suggestion is to do something like this:
@Suppress("UnstableApiUsage", "unused")
class KotlinJvmOptionsPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("com.android.library")
apply("org.jetbrains.kotlin.android")
}
configure<LibraryExtension> {
(this as ExtensionAware).extensions.configure<KotlinJvmOptions>("kotlinOptions") {
jvmTarget = "11"
freeCompilerArgs = freeCompilerArgs + listOf("-Xopt-in=kotlin.RequiresOptIn")
}
}
}
}
}
?Guilherme Delgado
06/20/2022, 2:51 PMVampire
06/20/2022, 3:19 PMRegarding pluginNo, I did not sayyour suggestion is to do something like this:pluginManager.withPlugin
pluginManager.apply
, I said pluginManager.withPlugin
.
But if you want to apply
you can also do it of course, but you don't need to do it on pluginManager
, you can do it on the Project
directly.
If you use apply
you apply the plugin if it is not applied yet (multiple application is prevented automatically).
If you use pluginManager.withPlugin
you are reacting to a plugin being applied.
So if your plugin is only sensible with that other plugin applied, so it is kind of a pre-requisite, then yes, apply it.
If your plugin can optionally do some work in case another plugin is applied but not do it if that plugin does not get applied, use pluginManager.withPlugin
.Vampire
06/20/2022, 3:20 PMGuilherme Delgado
06/20/2022, 3:24 PM