97

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?

Seb Charrot
  • 2,816
  • 1
  • 22
  • 24

8 Answers8

57

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.

Jon Nichols
  • 2,211
  • 1
  • 20
  • 21
  • 2
    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: https://stackoverflow.com/a/49543980/7267809 – Aanand Kainth Apr 12 '18 at 04:05
  • 17
    For the sake of clarity, "re-open the project directly from the Gradle file" means: [Open] > Select `build.gradle` > [Open as Project] – noobar Sep 27 '19 at 01:42
32

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'
Rusty Shackleford
  • 1,111
  • 2
  • 14
  • 19
Beholder
  • 470
  • 5
  • 7
  • 7
    For those that may ask the question, "then why does `group 'com.me.blah'` work?," take a look at Groovy's [`methodMissing` hook](http://groovy-lang.org/metaprogramming.html#_methodmissing). The default Gradle `Project` implementation apparently uses this hook to set the property with the name of the missing method, if it exists. – Rusty Shackleford Apr 12 '18 at 01:02
  • 4
    This results in `Cannot assign 'String' to 'Object'` for me. – PHPirate Nov 24 '18 at 09:57
19

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.

FGreg
  • 14,110
  • 10
  • 68
  • 110
19

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

8

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".

Seb Charrot
  • 2,816
  • 1
  • 22
  • 24
7

For me, after trying out all this answers without result, changing the Java SDK of the project did the trick, I was on 1.8 and changed it to the newest one, but still a project level language of 8.

Hope this helps!

Ariel Mirra
  • 1,117
  • 1
  • 10
  • 17
  • 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. – RenatoIvancic Jun 09 '21 at 18:45
6

What I found is that this warning is from Groovy inspection of the Intelliji. So if your project is not pure groovy, you can disable this inspection by going to File -> settings -> Editor -> Inspections -> disable Groovy. Again, just a workaround.

Neal Sha
  • 87
  • 1
  • 2
  • 2
    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 – plamenti Aug 31 '21 at 20:51
5

Happened to me recently on windows. I tried all of the above but it didn't work.

What i did:

1 - I had JAVA_HOME environment variable already set. So IDEA picked that as project SDK and these warnings showed up.

2 - So, i removed the default JAVA_HOME from project SDK and manually added jdk path. Restarted and all warnings were gone.

Maybe it will help someone. Thanks.

Rajkishan Swami
  • 3,569
  • 10
  • 48
  • 68