Cannot assign 'String' to 'T'

Viewed 399

I am trying to create a Gradle plugin and when I define the plugin's id and implementationClass, IntelliJ says Cannot assign 'String' to 'T'

enter image description here

  • Why it's showing the warning?
  • How can I fix this?

I am using

IntelliJ IDEA 2020.1.1 (Ultimate Edition)
Build #IU-201.7223.91, built on April 30, 2020
1 Answers

IntelliJ is not able to infer types, especially inside of a closure. This is a downside of using the Groovy syntax for your Gradle files. You'll see in some places that IntelliJ is able to correctly infer the type, but other places it can not.

This is where the where the Kotlin DSL truely shines. If you switch over to the Kotlin DSL, you'll get code completion and hints practically everywhere.

Using your example with the Kotlin, I get the following:

kotlin-dsl

You can see I get full code completion thanks to Kotlin being a statically typed language unlike Groovy.

If you really want full code completion and hints with Groovy, then I suggest avoiding the Groovy DSL and use the Gradle API directly:

enter image description here

Simplified version of the above Groovy:

project.extensions.configure(GradlePluginDevelopmentExtension, { GradlePluginDevelopmentExtension gradlePluginExtension ->
    gradlePluginExtension.plugins({ NamedDomainObjectContainer<PluginDeclaration> pluginDeclarations ->
        pluginDeclarations.create("ll.greating", { PluginDeclaration pluginDeclaration ->
            pluginDeclaration.setId("ll.greeting")
        })
    })
})
Related