52

The project compiles fine. However, Android studio 3.0.1 shows reference to one particular library in red and displays "cannot resolve symbol". Even single stepping into that library module doesn't work.

I already tried various options like 1) Invalidate cache/restart 2) deleted build directory 3) clean and rebuild but nothing seems to be working.

Interestingly, when Android Studio is rebuilding indexes after invalidate/restart, it momentarily shows that library references in while but then again in red.

Zoe
  • 27,060
  • 21
  • 118
  • 148
mesibo
  • 3,970
  • 6
  • 25
  • 43
  • I've had something similar, its as if the Android Studio doesn't detect when somethings changed even though it compiles OK. The only way I've found to make it work correctly is to close down all instances of Android Studio and re-open it again. Its a huge pain in the next if your doing frequent code changes to a library. – Boardy Dec 20 '17 at 16:10
  • I had a similar issue using my libraries (not external ones), see how I manage to fix this here: https://stackoverflow.com/a/50569818/373033 – nah0y May 29 '18 at 07:23

14 Answers14

69

I tried Invalidate cache/restart or clean Project -> rebuild project. These didn't work for me.

The final solution was open Project window on the left side of IDE, under Project mode, delete .gradle and .idea folder, then sync, it works.

TaoBit
  • 852
  • 7
  • 10
  • 15
    This worked for me when nothing else would, but I also had to invalidate caches / restart after I deleted the .gradle and .idea folders or else it wouldn't fix it. –  May 13 '18 at 00:26
  • Tried all sorts of things. This was the only thing that worked. – CKP78 May 14 '18 at 08:57
  • 13
    i deleted the `.gradle` folder, and did a `invalidate cache/restart`, not worked. Then i deleted `.idea/libraries` folder and did a `invalidate cache/restart` again and it worked. So maybe only the `.idea/libraries` folder is the one who needs to be deleted? – Gabriel Rohden Aug 02 '18 at 02:04
  • 3
    Same issue here. Clean/Restart/etc. didn't help. Deleting the `.idea/libraries` folder finally did the trick! – mmo Aug 08 '18 at 19:26
  • 1
    Worked for me ! Deleted .gradle, .idea, sync with gradle, invalidate cache, rebuild – Shimbawa Oct 19 '19 at 16:59
  • The same scenario happened to me as well. Interestingly it only happens when I set compiled SDK version to 29 (and corresponding build tools version). If set to 28, everything works fine. My solution is to uninstall SDK for API 29 and then reinstall it, then it should work fine. – Danny Fang May 11 '20 at 09:17
  • In my case, I also had to delete the caches under ~/.AndroidStudio3.6/system/caches/project_resources – shield Jun 16 '20 at 13:47
16

Following has resolved this for me.

  1. remove library from gradle cache (in case of linux, it's in ~/.gradle/caches).
  2. remove library dependency from the build.gradle and sync
  3. restart (maybe invalidate cache)
  4. add library dependency to build.gradle and sync
  5. clean and rebuild
mesibo
  • 3,970
  • 6
  • 25
  • 43
6

This is happen to me when I move my project to other pc or open the project with other pc and with different SDK folder location. Solution: close project, delete folder .gradle and folder .idea inside project folder then open your project to sync gradle.

user2702125
  • 141
  • 2
  • 4
4

In my case changing the Gradle version worked.

In gradle-wrapper.properties

changing

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

to

distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

and in build.gradle (project level)

changing

classpath 'com.android.tools.build:gradle:3.3.0'

to

classpath 'com.android.tools.build:gradle:3.2.1'
muzamil
  • 196
  • 7
2

Invalidate cache/restart didn't work for me.
Simple and working solution:
1: Remove all dependencies from App level Gradle and Sync.
2: Re-add all these dependencies and Sync.

It will work fine.

Sajjad Ali
  • 33
  • 11
  • 1
    All my references to dependencies were giving me error at startup.Your solution worked for me. I'm glad I don't have to totally delete the library and re-add. I can just delete the dependency, sync, then re-add the dependencies. – Smitty-Werben-Jager-Manjenson Nov 18 '19 at 15:06
2

This has happened to me a couple of times, there are a few ways you can resolve this problem;

  1. Sync project files with gradle in the File toolbar
  2. Use the Invalidate/Restart option in the File toolbar to invalidate the caches.
  3. Delete the .idea folder and restart Android Studio
2

Firsty try to Clean and rebuild project from Build -> Clean project & then Build -> Rebuild project.

If that doesn't work Invalidate Caches/Restart always works for me. Do it from File -> invalidate Caches/Restart

Invalidate Caches/Restart

Then click on invalidate and restart

invalidate and restart

Hopefully this helps.

1

For me problem was that AS suggested I upgrade gradle version, (from 3.5 to 4.4), and then it started doing it.

Solution was to delete the 3.5 folder from .gradle in project

urSus
  • 12,492
  • 12
  • 69
  • 89
  • This worked for me, I had several different folders (4.2, 4.4, 4.6) and I removed all but the last version. – Nemanja Kovacevic Oct 03 '18 at 16:10
  • I ran into this problem after an gradle upgrade, but deleting the previous gradle folders from .gradle did not work so I reverted back the the earlier gradle version and the problem was fixed. – user1608385 Jul 22 '22 at 00:06
0

None of the answers worked for me

Fixed it by:

  1. Removing & adding all java files via Project window on the left side of Android Studio

  2. Commenting/un-commenting & syncing libraries in the gradle file in small groups.

And cleaning all caches before that (as part of other fixes here).

Inoy
  • 1,065
  • 1
  • 17
  • 24
0

I've tried Invalidate Caches/Restart..., tried manually delete .idea and .build folders and it didn't work for my case. I using VCS (git) and I just commit my changes, removed the whole project, and Checkout from Version Control. It fixed this problem.

Android Studio 3.6.3

Dyvoker
  • 2,489
  • 1
  • 9
  • 8
0

I Tried the following in order to resolve the issue:

  1. Invalidated Caches/Restart
  2. deleting .idea, .gredle under project and build under appenter image description here
  3. done clean project and rebuild project
  4. deleted the caches in the installed path Eg. {installed path}.AndroidStudio4.0\system\caches
  5. upgraded the studio to latest version [I have installed as suggested by windows, so its installed same path. I might have tried new path.]

Non of these tries helped to resolved this issue. Finaly I have tried these two steps:

  1. commented dataBinding.enabled true from build.gredle of your app as its obsolete and replaced with buildFeatures{dataBinding true}.

  2. Deleted the content of the log, tmp and caches folder from the installed path manually. Eg. {installed path}.AndroidStudio4.0\system\caches, {installed path}.AndroidStudio4.0\system\log, {installed path}.AndroidStudio4.0\system\log

  3. Restarted the android studio

Now all reference for the Binding and other classes were restored.

Ashok
  • 61
  • 1
  • 2
0

I ran into this problem after upgrading Android Plug In Version from 3.54 to 7.2.1 and Gradle version 5.41 to 7.3.3 . Reverting back to the earlier versions fixed the problem. Simply deleting the 5.41 folder in .gradle as suggested in another answer above did not work.

user1608385
  • 609
  • 8
  • 17
-1

Update

classpath 'com.android.tools.build:gradle:3.X.X' 

to the last version worked for me.

toto_tata
  • 14,526
  • 27
  • 108
  • 198
-1

Tried all of the above but nothing worked.

Got Solution by: 1. Updating Android studio to latest version 2. Updating the Gradle version to latest one