IntelliJ IDEA and Gradle - Cannot be applied to &#

2020-05-22 04:27发布

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?

8条回答
Juvenile、少年°
2楼-- · 2020-05-22 04:40

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'
查看更多
家丑人穷心不美
3楼-- · 2020-05-22 04:45

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.

查看更多
时光不老,我们不散
4楼-- · 2020-05-22 04:45

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.

查看更多
Emotional °昔
5楼-- · 2020-05-22 04:46

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.

查看更多
我命由我不由天
6楼-- · 2020-05-22 04:48

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

查看更多
Luminary・发光体
7楼-- · 2020-05-22 04:57

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

查看更多
登录 后发表回答