IntelliJ IDEA and Gradle - Cannot be applied to '(groovy.lang.Closure)'

67,526

Solution 1

For anyone looking for a similar fix, this boiled down to the type of the module. My module was defined in my .iml file as

type="WEB_MODULE"

I created a new Gradle module and pasted in the same contents, deleted the original, renamed the new module to have the same name as the old one, and everything worked fine. When I diffed the results the only change was that the .iml file now said:

type="JAVA_MODULE"

So there's the answer, seemingly. Change your module from "web" to "java".

Solution 2

I've noticed this often. When creating a new project and using the 'Gradle' template, IntelliJ seems to get it all wrong. So what I've resorted to doing is after creating a new project, delete the .idea and .iml files and re-open the project directly from the Gradle file. IntelliJ seems to better understand what is going on in this case.

Solution 3

For the first part of the question: group is a property, so there must be an assignment, not a function call. The right code is:

group = 'com.me.blah'

Solution 4

I found this suggestion in the IDEA bug tracker(IDEA-142683):

Workaround:
One can add a comment such as
//noinspection GroovyAssignabilityCheck
to suppress the warning, but this should not be necessary as these are standard every day Gradle usages.

This issue was recently updated and is marked fixed, ready for release with version 2016.2 release.

Solution 5

Try File | Invalidate Caches and restart. It worked for me after i mess the syntaxe somewhere else in the file.

Share:
67,526
Seb Charrot
Author by

Seb Charrot

Updated on July 24, 2022

Comments

  • Seb Charrot
    Seb Charrot almost 2 years

    I have a Gradle file which, whenever I load open it in IntelliJ IDEA 14.1.5, shows IDE errors for the entire file. Namely all the errors seem to be either:

    java.lang.String errors

    java.lang.String errors

    or

    groovy.lang.Closure errors

    groovy.lang.Closure errors

    I've tried clearing the file's contents and only writing the top line:

    group 'com.me.blah'
    

    but even that results in an error.

    For context's sake - this is an individual module in a larger SpringBoot project. This module is a set of simple static files (with Gradle for CSS compilation, static analysis, etc), while the rest are Java modules, and are not having Gradle issues.

    Can anyone think why IntelliJ would be struggling to understand this Gradle file?

  • user2649601
    user2649601 over 8 years
    This did not do the trick for me unfortunately. What did work is "Disable inspection". Not sure if that is the best way to go though.
  • MartyIX
    MartyIX almost 8 years
    It should be fixed in the next EAP version: youtrack.jetbrains.com/issue/IDEA-142683#comment=27-1448045
  • stewenson
    stewenson almost 8 years
    where do I need to add that comment? I am on 15.0.6 and it is annoying
  • FGreg
    FGreg almost 8 years
    @stewenson Just put it right above the block of code that is complaining.
  • Rusty Shackleford
    Rusty Shackleford about 6 years
    For those that may ask the question, "then why does group 'com.me.blah' work?," take a look at Groovy's methodMissing hook. The default Gradle Project implementation apparently uses this hook to set the property with the name of the missing method, if it exists.
  • Aanand Kainth
    Aanand Kainth about 6 years
    This worked to fix the above error, but it deleted all of my configuration, so the project structure and modules were not stored. Instead, I found that this worked: stackoverflow.com/a/49543980/7267809
  • PHPirate
    PHPirate over 5 years
    This results in Cannot assign 'String' to 'Object' for me.
  • Displee
    Displee almost 5 years
    I don't have an iml file.
  • noobar
    noobar over 4 years
    For the sake of clarity, "re-open the project directly from the Gradle file" means: [Open] > Select build.gradle > [Open as Project]
  • Jonathan E. Landrum
    Jonathan E. Landrum over 4 years
    @Displee it is possibly in your .idea directory
  • Andres Farias
    Andres Farias almost 4 years
    It's happening again to me with version 2019.1.2
  • Dhruv garg
    Dhruv garg over 3 years
    I am on 2020.2 RC version and this is still happening
  • RenatoIvancic
    RenatoIvancic almost 3 years
    I had this issue inside of the buildSrc directory. As I changed language level from 14 down to 8, warning went away. I don't know why java language level is interfering with the Gradle script with Groovy DSL syntax.
  • plamenti
    plamenti over 2 years
    No need to disable the whole Groovy section but only 'Incompatible type assignments'. To disable this inspection: File -> Settings... -> Editor -> Inspections -> Groovy -> Assignment issues -> disable Incompatible type assignments