Android Studio "cannot resolve symbol" but project compiles and works
Android StudioAndroid Studio Problem Overview
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.
Android Studio Solutions
Solution 1 - Android Studio
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.
Solution 2 - Android Studio
Following has resolved this for me.
-
remove library from gradle cache (in case of linux, it's in ~/.gradle/caches).
-
remove library dependency from the build.gradle and sync
-
restart (maybe invalidate cache)
-
add library dependency to build.gradle and sync
-
clean and rebuild
Solution 3 - Android Studio
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.
Solution 4 - Android Studio
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'
Solution 5 - Android Studio
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.
Solution 6 - Android Studio
This has happened to me a couple of times, there are a few ways you can resolve this problem;
- Sync project files with gradle in the File toolbar
- Use the Invalidate/Restart option in the File toolbar to invalidate the caches.
- Delete the .idea folder and restart Android Studio
Solution 7 - Android Studio
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
Solution 8 - Android Studio
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
Then click on invalidate and restart
Hopefully this helps.
Solution 9 - Android Studio
None of the answers worked for me
Fixed it by:
-
Removing & adding all java files via Project window on the left side of Android Studio
-
Commenting/un-commenting & syncing libraries in the gradle file in small groups.
And cleaning all caches before that (as part of other fixes here).
Solution 10 - Android Studio
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
Solution 11 - Android Studio
I Tried the following in order to resolve the issue:
- Invalidated Caches/Restart
- deleting .idea, .gredle under project and build under app
- done clean project and rebuild project
- deleted the caches in the installed path Eg. {installed path}.AndroidStudio4.0\system\caches
- 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:
-
commented dataBinding.enabled true from build.gredle of your app as its obsolete and replaced with buildFeatures{dataBinding true}.
-
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
-
Restarted the android studio
Now all reference for the Binding and other classes were restored.
Solution 12 - Android Studio
Update
classpath 'com.android.tools.build:gradle:3.X.X'
to the last version worked for me.
Solution 13 - Android Studio
Tried all of the above but nothing worked.
Got Solution by:
- Updating Android studio to latest version
- Updating the Gradle version to latest one