49

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.

Meng Lu
  • 13,726
  • 12
  • 39
  • 47
  • 1
    Look at the dependencies and artifacts of your IDEA project. – PM 77-1 Jul 06 '14 at 22:16
  • possible duplicate of [How do I get IntelliJ to resolve Gradle dependencies for custom source sets?](http://stackoverflow.com/questions/24538929/how-do-i-get-intellij-to-resolve-gradle-dependencies-for-custom-source-sets) – jzahedieh Mar 30 '15 at 12:26
  • There is an "idea" plugin for gradle, see the question linked above – jzahedieh Mar 30 '15 at 12:27

11 Answers11

60

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

forestj
  • 983
  • 6
  • 15
18

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.

ghchoi
  • 4,812
  • 4
  • 30
  • 53
10

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

Namita
  • 97
  • 1
  • 4
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.

4b0
  • 21,981
  • 30
  • 95
  • 142
TGU
  • 181
  • 2
  • 7
6

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

JasmineOT
  • 1,978
  • 2
  • 20
  • 30
3

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.

Nikita Koselev
  • 185
  • 2
  • 10
  • 1
    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. – wellthatdidn'twork Aug 04 '21 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 Feb 20 '22 at 04: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

ThangTD
  • 1,586
  • 17
  • 16
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".
Meng Lu
  • 13,726
  • 12
  • 39
  • 47
  • Not sure if understood well but importing gradle project into IntelliJ works pretty fine - using of gradle wrapper is recommended. – Opal Jul 07 '14 at 09:17
  • 62
    doesn't this pretty much negate the whole point of using gradle? – jk. Oct 20 '14 at 15:22
  • 1
    This link solved the same problelm i was having https://www.jetbrains.com/idea/help/synchronizing-changes-in-gradle-project-and-intellij-idea-project.html – Sap May 18 '15 at 11:45
  • This was the only solution/description that worked for me (IntelliJ 14.1.2): http://stackoverflow.com/a/31195053/2584278 – Faber Feb 27 '16 at 12:43
0

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

Udi Meiri
  • 1,073
  • 8
  • 14
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;
Mohammed Siddiq
  • 477
  • 4
  • 16