Java project with Gradle in IntelliJ IDEA: cannot resolve symbol 'google' but project compiles
Asked Answered
W

11

50

I have a toy Java project set up with Gradle in IntelliJ IDEA 13.1.2. It compiles and runs fine, but the IDE highlights 'google' in

import com.google.common.base.Strings;

as red and warns "Cannot resolve symbol 'google'". Any idea how to fix it?

I have tried 1) deleting .idea/ and re-creating the project in IntelliJ IDEA, and 2) re-importing project from the manually created Gradle configuration file build.gradle, but to no avail.

Wingate answered 6/7, 2014 at 22:14 Comment(3)
Look at the dependencies and artifacts of your IDEA project.Schulte
possible duplicate of How do I get IntelliJ to resolve Gradle dependencies for custom source sets?Gamb
There is an "idea" plugin for gradle, see the question linked aboveGamb
W
0

It turns out that the depended packages need to be separately specified in IntelliJ IDEA in addition to build.gradle and explicitly added as a dependency. Namely,

  1. download the com.google.guava package following the instruction in documentation -- I picked com.google.guava:guava-base:r03 to download
  2. follow the automatically-prompted window to add it as a project library to the Java project.
  3. Specify that the Java module depends on the com.google.guava package in the project settings. See the documentation instruction "Configuring Module Dependencies and Libraries".
Wingate answered 6/7, 2014 at 23:12 Comment(4)
Not sure if understood well but importing gradle project into IntelliJ works pretty fine - using of gradle wrapper is recommended.Cairistiona
doesn't this pretty much negate the whole point of using gradle?Andrel
This link solved the same problelm i was having jetbrains.com/idea/help/…Gastroenteritis
This was the only solution/description that worked for me (IntelliJ 14.1.2): https://mcmap.net/q/53919/-intellij-inspection-gives-quot-cannot-resolve-symbol-quot-but-still-compiles-codeCaneghem
S
61

I think user Sap is correct, at least in my case. You should not have to manually add the dependency.

Did you change the dependencies in the gradle file without syncing intellij? Try this button:

screenshot of sync button

For more information, see: https://www.jetbrains.com/idea/help/synchronizing-changes-in-gradle-project-and-intellij-idea-project.html

Scriptural answered 11/5, 2016 at 19:26 Comment(2)
This did not work for me. Only "Invalidate Caches and Restart" worked.Grantley
^ File dropdown in IntelliJTrass
B
19

Check this.

You can simply open Gradle tool window at [ View ] - [ Tool Windows ] - [ Gradle ].

In the window, you can refresh by clicking refresh button.

All dependencies manually added directly into build.gradle file will be resolved.

Bluefarb answered 28/12, 2016 at 1:55 Comment(0)
I
11

One of the solutions that worked for me after trying everything listed on the internet to solve this issue was to install the lombok plugin.

Got to File --> Settings --> Plugins and look for Lombok.

Make sure the "Enable annotation processing" is ticked

Inflationary answered 19/3, 2018 at 12:3 Comment(1)
Thanks for providing the linkHypognathous
B
8

In my case, I have to do the following:

  • close project
  • close idea
  • remove .idea project directory
  • remove (idea.system.path) directory
  • start idea

You can find your idea.system.path here: https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-store-settings-caches-plugins-and-logs

Backhanded answered 30/6, 2021 at 16:43 Comment(3)
This is the only thing that worked for me with the exception that I didn't have an idea.system.path directory (v2021.2.1 Ultimate)Uneducated
Same as @Uneducated 2021.3.3Andyane
Close Intellij and delete .idea directory worked for me.Carrion
C
6

My Gradle project is using Intellij 2019.2.3, and File->Invalid Cache/Restart... doesn't work, but View->Tool Windows->Gradle->sync(the circular icon) works instantly.

Crystallization answered 31/12, 2019 at 1:56 Comment(0)
M
4

What helped me was checking "dependencies" in "project settings/modules" section. Apparently, Idea did not pick up them correctly.

Steps which worked for me:

  1. delete all modules from "project settings/modules"
  2. refresh the project from Gradle plugin - that triggers generation of modules

This trick helped me to get modules with correct dependencies generated. Probably it happened because initially, I imported the project as non-Gradle one.

Mut answered 28/11, 2020 at 15:49 Comment(2)
This was what did it for me too, for those who don't know, you can find this under File -> Project Structure -> project settings/modules. Then just delete all the modules there.Ramie
I'll add the issue for me was that my project had a build.gradle in a subdirectory. It's an AWS Lambda project created by the wizard that got out of sync and I had to re-import it and separately add the subdirectory as a module.Divvy
A
1

For those who tried the above suggestion by refreshing the gradle projects but didn't work.

I ran gradle build in the terminal successfully, but IntelliJ didn't work. I clicked on the build icon within IntelliJ, wait for the build, and it works.

See screenshot. enter image description here

Arriaga answered 18/6, 2023 at 3:19 Comment(0)
W
0

It turns out that the depended packages need to be separately specified in IntelliJ IDEA in addition to build.gradle and explicitly added as a dependency. Namely,

  1. download the com.google.guava package following the instruction in documentation -- I picked com.google.guava:guava-base:r03 to download
  2. follow the automatically-prompted window to add it as a project library to the Java project.
  3. Specify that the Java module depends on the com.google.guava package in the project settings. See the documentation instruction "Configuring Module Dependencies and Libraries".
Wingate answered 6/7, 2014 at 23:12 Comment(4)
Not sure if understood well but importing gradle project into IntelliJ works pretty fine - using of gradle wrapper is recommended.Cairistiona
doesn't this pretty much negate the whole point of using gradle?Andrel
This link solved the same problelm i was having jetbrains.com/idea/help/…Gastroenteritis
This was the only solution/description that worked for me (IntelliJ 14.1.2): https://mcmap.net/q/53919/-intellij-inspection-gives-quot-cannot-resolve-symbol-quot-but-still-compiles-codeCaneghem
F
0

In my case (Apache Beam sources) a ./gradlew clean was needed.

Fortify answered 11/6, 2019 at 21:22 Comment(0)
C
0

In My Case, I've Updated the Gradle version(module: project) from 3.2.2 to 3.5.2, and also there was a problem with the NDK file location it was on the wrong path, I've just switched it to the default NDK path, then invalidate and restart the project.

Cynic answered 18/11, 2019 at 14:33 Comment(0)
S
0

I was having the wrong import.Check if you have right import. In case you have imported using:

import org.junit.Test 

and you have org.junit.jupiter.api.Test in class path, try importing with :

import org.junit.jupiter.api.Test;
Syreetasyria answered 20/7, 2021 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.