Hello!
I am working on implementing a custom plugin in a standalone project that will be used by other projects. In this plugin, I want to configure many other plugins.
Here’s the Plugin code:
class CodeStylePlugin : Plugin<Project> {
override fun apply(project: Project) {
project.extensions.create(
"myplugin",
CodeStyleExtension::class.java
)
val config = project.extensions.getByType(CodeStyleExtension::class.java)
if (config.addCheckStyle.get()) {
// configure checkstyle
}
}
}
Here’s the extension code:
open class CodeStyleExtension @Inject constructor(objectFactory: ObjectFactory) {
var addCheckStyle: Property<Boolean> = objectFactory.property(Boolean::class.java).convention(false)
}
In some other project, I am calling this plugin and configuring the extension in build.gradle.kts
:
apply(plugin = "code-style")
configure<CodeStyleExtension> {
addCheckStyle.set(true)
}
The config.addCheckStyle.get()
call in the plugin script is always false. I am assuming that the apply()
call in the build.gradle.kts
calls the plugin’s apply()
function and the get()
immediately returns false. I think configure<CodeStyleExtension>
is configured later.
I tried to wrap the val config =
and subsequent lines within a afterEvaluate
code block. However, the build fails with this error: Extension of type 'CodeStyleExtension' does not exist.
I appreciate any suggestions to fix this issue.