51

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.

3

11 Answers 11

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

2
  • 10
    This did not work for me. Only "Invalidate Caches and Restart" worked. Commented Mar 27, 2019 at 9:37
  • 2
    ^ File dropdown in IntelliJ Commented Apr 5, 2021 at 5:31
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.

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

1
  • Thanks for providing the link Commented Mar 19, 2018 at 12:08
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

3
  • 1
    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)
    – Pink
    Commented Nov 10, 2021 at 23:45
  • Same as @Pink 2021.3.3
    – DannyK
    Commented May 12, 2022 at 10:46
  • Close Intellij and delete .idea directory worked for me. Commented Jul 25, 2022 at 5:27
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.

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.

2
  • 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. Commented Aug 4, 2021 at 18:38
  • 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.
    – Philip
    Commented Feb 20, 2022 at 4:24
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

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".
4
  • Not sure if understood well but importing gradle project into IntelliJ works pretty fine - using of gradle wrapper is recommended.
    – Opal
    Commented Jul 7, 2014 at 9:17
  • 62
    doesn't this pretty much negate the whole point of using gradle?
    – jk.
    Commented Oct 20, 2014 at 15:22
  • 1
    This link solved the same problelm i was having jetbrains.com/idea/help/…
    – Sap
    Commented May 18, 2015 at 11:45
  • This was the only solution/description that worked for me (IntelliJ 14.1.2): stackoverflow.com/a/31195053/2584278
    – Faber
    Commented Feb 27, 2016 at 12:43
0

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

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.

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.