Always a problem with IntelliJ and Gradle

已回答

Why can't I ever import a gradle project and have IntelliJ succeed in loading it such that I can press the build button and have it work?

Please help me understand why I always have a problem in IntelliJ with importing projects that use Gradle.

I'm currently using:

IntelliJ IDEA 2021.1.2 (Ultimate Edition)
Build #IU-211.7442.40, built on May 31, 2021
Licensed to <redacted>
You have a perpetual fallback license for this version.
Subscription is active until <redacted (but in the future)>
Runtime version: 11.0.11+9-b1341.57 x86_64
VM: Dynamic Code Evolution 64-Bit Server VM by JetBrains s.r.o.
macOS 10.15.7
GC: ParNew, ConcurrentMarkSweep
Memory: 4012M
Cores: 12

I've had these issues for at least 3 years, though, through many versions of IntelliJ.

Newly cloned repos will build just fine for me in the terminal (e.g. `./gradlew build`), but IntelliJ always has a problem. Always.

Here's what happens:

  1. Clone a repo (e.g. `git clone .....`).
  2. Open IntelliJ.
  3. Tell IntelliJ to import the newly cloned project (selecting either repo's root directory or the main `build.gradle` or `build.gradle.kts` file).
  4. IntelliJ complains about not being able to do stuff with respects to gradle.

Most often, what I see is that the sync window says there was a failure, and the only message is "Starting Gradle Daemon...". Through trial and error, I found that I have to go to Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle and change the "Gradle JVM Value". It defaults to JAVA_HOME, but it NEVER likes it. Sometimes all I have to do is select the exact same thing that JAVA_HOME is listed as (the entry with the version number instead of "JAVA_HOME").

I currently use SDKMAN for managing my java SDKs (and also which JRE to use in a given terminal). My JAVA_HOME is set to `<redacted>/.sdkman/candidates/java/current`. That hasn't always been the case though. I've used various methods/tools over the last few years. None of them interact with IntelliJ directly, I just wanted to mention it.

Recently I upgraded IntelliJ (to the version listed above), and on the first attempt at this, I get something slightly different. The sync windows indicates a failure with a little more info. Under the old error (on the left) there's a new sub-entry "Gradle Sync Issues":

Java home is different.
Expecting: '<redacted>/.sdkman/candidates/java/current' but was: '<redacted>/.sdkman/candidates/java/8.0.265-zulu/zulu-8.jdk/Contents/Home'.

Please configure the JDK to match the expected one.

Open JDK Settings

My JAVA_HOME is set to <redacted>/.sdkman/candidates/java/current

So I'm guessing that's where the "expected" value is coming from.

That location is a symlink to <redacted>/.sdkman/candidates/java/8.0.265-zulu

I tried clicking the "Open JDK Settings" link, changing the JDK location, and clicking "Apply" but I get the error "The Android SDK location cannot be at the filesystem root." But I don't do android development and don't have an Android SDK locally (that I know of). And given all the issues I've had with different JDK versions, I'm certain that just downloading an Android SDK so that I have a value to put there will just cause dumb problems later. So now, with this one, I'm completely stuck, unable to use IntelliJ for the project.

So my main question is, what am I doing wrong that this is always causing these problems for me?

A followup question is, why is a project's JVM setting located in the global Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle area?

1

The issue is Zulu JDK specific, see https://stackoverflow.com/a/61806948/104891. Either use a different JDK vendor or adjust JAVA_HOME to include Contents/Home on macOS.

> A followup question is, why is a project's JVM setting located in the global Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle area?

It's a project setting:

0

Adjusting JAVA_HOME to include Contents/Home will cause all the other vendor SDKs to not work (since the Contents/Home directory is unique to zulu). I'll give the AdoptOpenJDK SDKs a shot.

It's annoying that IntelliJ can't handle the zulu SDKs. I'm guessing it's because they use symlinks to directories rather than the directories directly.

> echo $JAVA_HOME
<redacted>/.sdkman/candidates/java/current
> cd $JAVA_HOME
> pwd -P
<redacted>/.sdkman/candidates/java/8.0.292-zulu
> ls -al
total 0
drwxr-xr-x 19 <redacted> <redacted> 608 Apr 6 11:34 .
drwxr-xr-x 8 <redacted> <redacted> 256 Jun 25 14:56 ..
lrwxr-xr-x 1 <redacted> <redacted> 43 Jun 25 14:53 ASSEMBLY_EXCEPTION -> zulu-8.jdk/Contents/Home/ASSEMBLY_EXCEPTION
lrwxr-xr-x 1 <redacted> <redacted> 49 Jun 25 14:53 CLASSPATH_EXCEPTION_NOTE -> zulu-8.jdk/Contents/Home/CLASSPATH_EXCEPTION_NOTE
lrwxr-xr-x 1 <redacted> <redacted> 35 Jun 25 14:53 DISCLAIMER -> zulu-8.jdk/Contents/Home/DISCLAIMER
lrwxr-xr-x 1 <redacted> <redacted> 32 Jun 25 14:53 LICENSE -> zulu-8.jdk/Contents/Home/LICENSE
lrwxr-xr-x 1 <redacted> <redacted> 43 Jun 25 14:53 THIRD_PARTY_README -> zulu-8.jdk/Contents/Home/THIRD_PARTY_README
lrwxr-xr-x 1 <redacted> <redacted> 37 Jun 25 14:53 Welcome.html -> zulu-8.jdk/Contents/Home/Welcome.html
lrwxr-xr-x 1 <redacted> <redacted> 28 Jun 25 14:53 bin -> zulu-8.jdk/Contents/Home/bin
lrwxr-xr-x 1 <redacted> <redacted> 29 Jun 25 14:53 demo -> zulu-8.jdk/Contents/Home/demo
lrwxr-xr-x 1 <redacted> <redacted> 32 Jun 25 14:53 include -> zulu-8.jdk/Contents/Home/include
lrwxr-xr-x 1 <redacted> <redacted> 28 Jun 25 14:53 jre -> zulu-8.jdk/Contents/Home/jre
lrwxr-xr-x 1 <redacted> <redacted> 28 Jun 25 14:53 lib -> zulu-8.jdk/Contents/Home/lib
lrwxr-xr-x 1 <redacted> <redacted> 28 Jun 25 14:53 man -> zulu-8.jdk/Contents/Home/man
lrwxr-xr-x 1 <redacted> <redacted> 35 Jun 25 14:53 readme.txt -> zulu-8.jdk/Contents/Home/readme.txt
lrwxr-xr-x 1 <redacted> <redacted> 32 Jun 25 14:53 release -> zulu-8.jdk/Contents/Home/release
lrwxr-xr-x 1 <redacted> <redacted> 31 Jun 25 14:53 sample -> zulu-8.jdk/Contents/Home/sample
lrwxr-xr-x 1 <redacted> <redacted> 32 Jun 25 14:53 src.zip -> zulu-8.jdk/Contents/Home/src.zip
drwxr-xr-x 3 <redacted> <redacted> 96 Apr 6 11:31 zulu-8.jdk

And the AdoptOpenJDK stuff for comparison:

> pwd -P
<redacted>/.sdkman/candidates/java/8.0.292.hs-adpt
> ls -al
total 103416
drwxr-xr-x 13 <redacted> <redacted> 416 Apr 20 15:02 .
drwxr-xr-x 8 <redacted> <redacted> 256 Jun 25 14:56 ..
-r--r--r-- 1 <redacted> <redacted> 1522 Apr 20 15:01 ASSEMBLY_EXCEPTION
-r--r--r-- 1 <redacted> <redacted> 19274 Apr 20 15:01 LICENSE
-r--r--r-- 1 <redacted> <redacted> 155003 Apr 20 15:01 THIRD_PARTY_README
drwxr-xr-x 46 <redacted> <redacted> 1472 Apr 20 15:04 bin
drwxr-xr-x 9 <redacted> <redacted> 288 Apr 20 15:01 include
drwxr-xr-x 7 <redacted> <redacted> 224 Apr 20 15:01 jre
drwxr-xr-x 9 <redacted> <redacted> 288 Apr 20 15:02 lib
drwxr-xr-x 5 <redacted> <redacted> 160 Apr 20 15:01 man
-rw-r--r-- 1 <redacted> <redacted> 335 Apr 20 15:02 release
drwxr-xr-x 12 <redacted> <redacted> 384 Apr 20 15:02 sample
-rw-r--r-- 1 <redacted> <redacted> 52762230 Apr 20 15:02 src.zip

I haven't run into anything else that uses JAVA_HOME and has a problem.

0

I have seen this on Windows for several years. It seems like it is getting worse.  There is ALWAYS a gradle problem. Projects that worked fine, no longer work.  There are a variety of different gradle errors.  Tweak lots of settings, lots of files, no joy.  I end up having to take the code and resources files and build a new project - and it works fine.  

0

Paul H Comitz Glad to hear you've got the Project working!

Could you please share more details about the issues you've been experiencing so we can look into them and make sure they won't re-occur?

0

请先登录再写评论。