Android Studio cannot resolve symbol but code executes correctly

48,026

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.

Share:
48,026

Related videos on Youtube

BVB
Author by

BVB

I enjoy any topics that deal with improving user experience, especially in mobile applications.

Updated on June 16, 2021

Comments

  • BVB
    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:

    SDK class - cannot find symbol

    Click here for full-size image

    The code builds and executes successfully. The function isThisThingWorking() simply returns true, and that is just what boolean blah gets set to.

    I have tried pressing the Sync Project with Gradle Files button and using the Invalidate Caches / Restart... option from Android Studio's File menu, but none of this solved the issue. What can I do to make the Android Studio IDE not display the Cannot resolve symbol 'xxxSDK' error?

  • BVB
    BVB over 10 years
    I have added it there already by right clicking on the Jar and selecting the Add as Library option.
  • hqt
    hqt over 10 years
    thanks :) my problem as you, that after OS crashes :D
  • clocksmith
    clocksmith over 10 years
    This makes sense. What if the .jar is not owned by me. How can I get it from .java to .class?
  • BVB
    BVB over 10 years
    You would have to compile the Java files.
  • sivag1
    sivag1 almost 10 years
    After scrambling a lot, this fixed, it. It happens mainly to the Eclipse imported projects.
  • Jared Burrows
    Jared Burrows over 9 years
    Thank for you posting this. This is the correct solution.
  • Dmitry Kolesnikovich
    Dmitry Kolesnikovich over 9 years
    This saves me a lot of time!
  • scubbo
    scubbo over 9 years
    Thank you! I had spent about three days following other solutions, but none worked completely until I did this!
  • lilbyrdie
    lilbyrdie about 9 years
    This happened to me after an OS crash, too. Good tip! Just a clean build couldn't resolve this.
  • eyadMhanna
    eyadMhanna almost 9 years
    This fixed it for me, thank you so much, it's been hours trying to solve the problem!
  • flobacca
    flobacca almost 9 years
    sorry, but I don't have a dependency for '.java' files. Could you post the line from your build.gradle file?
  • BVB
    BVB almost 9 years
    What kind of a dependency do you have?
  • chaoskreator
    chaoskreator over 7 years
    Worked for a brief period, but when I re-made the project, the issue popped up again.
  • Alireza Noorali
    Alireza Noorali over 6 years
    Great Answer, You Saved My Day! it must be the true answer. my problem occured after OS crashing, too.
  • Herbert Schiller
    Herbert Schiller over 5 years
    I 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
    Mir Stephen almost 3 years
    Thank you Brother