51

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
  • 5,380
  • 8
  • 41
  • 62
  • What 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 Aug 03 '15 at 21:28
  • @grAPPfruit the premise of the question is different and the posted date is earlier than the question you linked. – BVB Aug 25 '15 at 15:29
  • in 2018, this can still happen when your Android Studio and Gradle don't agree. I fixed it using three options as discussed here: https://stackoverflow.com/questions/50313885/android-studio-cannot-resolve-symbols-from-aar/50327343#50327343 – user1506104 May 14 '18 at 17:40
  • Related post - [Android Studio suddenly cannot resolve symbols](https://stackoverflow.com/q/21100688/465053) & [Android Studio says “cannot resolve symbol” but project compiles](https://stackoverflow.com/q/19508649/465053) – RBT Aug 06 '18 at 00:04

11 Answers11

127

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.

Senthil
  • 1,271
  • 1
  • 7
  • 2
  • thanks :) my problem as you, that after OS crashes :D – hqt Nov 08 '13 at 09:09
  • After scrambling a lot, this fixed, it. It happens mainly to the Eclipse imported projects. – sivag1 Jul 04 '14 at 21:02
  • Thank for you posting this. This is the correct solution. – Jared Burrows Nov 06 '14 at 04:02
  • This saves me a lot of time! – Dmitry Kolesnikovich Dec 08 '14 at 11:12
  • Thank you! I had spent about three days following other solutions, but none worked completely until I did this! – scubbo Feb 04 '15 at 13:17
  • This happened to me after an OS crash, too. Good tip! Just a clean build couldn't resolve this. – lilbyrdie May 26 '15 at 19:24
  • This fixed it for me, thank you so much, it's been hours trying to solve the problem! – eyadMhanna Aug 09 '15 at 12:14
  • Worked for a brief period, but when I re-made the project, the issue popped up again. – chaoskreator Jan 31 '17 at 01:42
  • Great Answer, You Saved My Day! it must be the true answer. my problem occured after OS crashing, too. – Alireza Noorali Dec 05 '17 at 11:12
  • 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... –  Feb 13 '19 at 09:01
5

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.

BVB
  • 5,380
  • 8
  • 41
  • 62
3

This worked for me:

Go to File -> Sync Project with Gradle Files.

whateven
  • 344
  • 4
  • 9
  • 1
    This works for me. The problem is that the external file is modified but from the Android Studio editor point of view, there is nothing changed, so ask Android Studio to check again the Gradle Files will work. – Chester Fung Apr 28 '23 at 06:16
2

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.

free3dom
  • 18,729
  • 7
  • 52
  • 51
  • I have added it there already by right clicking on the Jar and selecting the `Add as Library` option. – BVB Sep 24 '13 at 21:17
2

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.

Machado
  • 14,105
  • 13
  • 56
  • 97
1

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.

Sumit Kumar
  • 564
  • 6
  • 14
0

Changing the language injection settings worked for me.

Place the cursor on any of the red underlined code and Alt + Enter

enter image description here

Now select Language Injection Settings and a window will open as shown below.

enter image description here

Make sure you have the right file type selected in ID: field and Uncheck the options related to your error codes and click Ok

enter image description here

Hope this helps somebody.

Tricky Bay
  • 796
  • 9
  • 20
0

Try following steps:

  1. Close your project then in your project folder delete project /.idea
  2. Delete /.gradle folder also
  3. Noe choose File option from menu on left top side of android studio
  4. Select the option: Invalidate Cache/Restart It will open a dialog
  5. Click on the first button with option: Invalidate and Restart

After trying above options everything should be working fine.

0xAliHn
  • 18,390
  • 23
  • 91
  • 111
0

For me worked only

File -> Open -> and click build.gradle to reopen project

sedq
  • 305
  • 2
  • 11
0

Close Android Studio and delete the build folder. Reopen Android Studio and Press "Sync With Gradle" if necessary.That's the only thing that will work.

Axes Grinds
  • 756
  • 12
  • 24
0

I deleted all build folders and .idea files of all project modules separately and rebuilt the project. That worked for me.

Mohsen Mousavi
  • 131
  • 2
  • 8