Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio cannot resolve symbol but code executes correctly

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?

like image 869
BVB Avatar asked Sep 24 '13 20:09

BVB


People also ask

What is Cannot resolve symbol in Android Studio?

Most often “R cannot be resolved” error appears if there is an issue with some of your resource files. Due to this error, you are unable to build your application. That's why we need to solve this error as it not getting away by just doing a simple restart or hitting Alt+Enter.

What does Cannot resolve symbol mean?

“cannot resolve symbol” means that the Java compiler cannot locate a symbol referenced in your Java source code. The causes for this common error include: A missing class file. A faulty CLASSPATH.


2 Answers

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.

like image 115
Senthil Avatar answered Sep 28 '22 00:09

Senthil


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.

like image 38
BVB Avatar answered Sep 27 '22 23:09

BVB