Android Studio cannot resolve symbol but code executes correctly
Solution 1
EDIT: For most folks, Senthil's answer will be more appropriate. I am leaving this one as the accepted answer because it solved my specific problem.
I found the issue - my SDK.jar
was not generated correctly. It included .java
files instead of .class
files. This explains why the IDE was not able to find the SDK class. The package structure was still correct in the Jar, which is why the package name itself is not a red color. The code worked correctly, because the compiler knew to compile the .java
files.
To solve the issue, I modified my build.gradle
of my SDK project to include .class
files, instead of .java
files, when creating the Jar. Including this new Jar instead of the old Jar fixed the IDE issue.
Solution 2
I have faced this issue when IntelliJ IDEA got closed abruptly due to OS crash.
You can do "File" -> "Invalidate Caches...", and select "Invalidate and Restart" option to fix this.
Solution 3
Try adding the library to the Project Structure
.
To do this, on the menu choose File
-> Project Structure
. Select the Libraries
option and click the green +
to add your library.
Solution 4
This was always happening to me after switching branches on my current project: a lot of non-sense erros being reported by the IDE.
The solution is to modify and force a build.gradle
synchronization.
Solution 5
This problem regularly generated in android 3.1. I have tried every possible way. But it resolved when i delete .gradle and .idea file and start Android studio which is already close.
Android studio create .idea and .gradle file for your project again. After this click on sync project with gradle. Now everything works fine.
Related videos on Youtube
BVB
I enjoy any topics that deal with improving user experience, especially in mobile applications.
Updated on June 16, 2021Comments
-
BVB almost 3 years
I have recently created a library Jar file that I imported in my Android project. I am able to call functions and use classes that are present in this Jar file, but Android Studio keeps telling me that it cannot "resolve symbol" of the class I am using. Please see the screenshot below:
Click here for full-size image
The code builds and executes successfully. The function
isThisThingWorking()
simply returnstrue
, and that is just whatboolean blah
gets set to.I have tried pressing the
Sync Project with Gradle Files
button and using theInvalidate Caches / Restart...
option from Android Studio'sFile
menu, but none of this solved the issue. What can I do to make the Android Studio IDE not display theCannot resolve symbol 'xxxSDK'
error?-
BVB almost 9 yearsWhat do you mean? I have accepted the answer that has solved my specific problem. The answer with the most upvotes here applies to most people who have the same issue. I am not familiar with any other solutions, unfortunately.
-
BVB almost 9 years@grAPPfruit the premise of the question is different and the posted date is earlier than the question you linked.
-
user1506104 about 6 yearsin 2018, this can still happen when your Android Studio and Gradle don't agree. I fixed it using three options as discussed here: stackoverflow.com/questions/50313885/…
-
RBT almost 6 years
-
-
BVB over 10 yearsI have added it there already by right clicking on the Jar and selecting the
Add as Library
option. -
hqt over 10 yearsthanks :) my problem as you, that after OS crashes :D
-
clocksmith over 10 yearsThis makes sense. What if the .jar is not owned by me. How can I get it from .java to .class?
-
BVB over 10 yearsYou would have to compile the Java files.
-
sivag1 almost 10 yearsAfter scrambling a lot, this fixed, it. It happens mainly to the Eclipse imported projects.
-
Jared Burrows over 9 yearsThank for you posting this. This is the correct solution.
-
Dmitry Kolesnikovich over 9 yearsThis saves me a lot of time!
-
scubbo over 9 yearsThank you! I had spent about three days following other solutions, but none worked completely until I did this!
-
lilbyrdie about 9 yearsThis happened to me after an OS crash, too. Good tip! Just a clean build couldn't resolve this.
-
eyadMhanna almost 9 yearsThis fixed it for me, thank you so much, it's been hours trying to solve the problem!
-
flobacca almost 9 yearssorry, but I don't have a dependency for '.java' files. Could you post the line from your build.gradle file?
-
BVB almost 9 yearsWhat kind of a dependency do you have?
-
chaoskreator over 7 yearsWorked for a brief period, but when I re-made the project, the issue popped up again.
-
Alireza Noorali over 6 yearsGreat Answer, You Saved My Day! it must be the true answer. my problem occured after OS crashing, too.
-
Herbert Schiller over 5 yearsI would say this is the correct Answer, but I had a very specific case, so just in case someone still having the issue after doing all that, I have 2 flavours, 1 for a paid version and 1 for a free version, I updated some libraries on my app.gradle on the free version, and for some reason I enabled "Global Gradle Settings > Offline work" and when I changed the build type to the pro version I started getting the errors, the app would run but I would get these erros, so I disabled offline work, built it again and all went back to normal, just frustrating lost an hour with this silly thing...
-
Mir Stephen almost 3 yearsThank you Brother