Solved – Cannot resolve symbol Android Studio

In Java, if you receive ”cannot resolve symbol” error, it simply mean that you have used a name that the compiler was not able to recognize.

1. Class name

If you do misspell in the class name, the class won’t get imported properly, and the compiler won’t be able to find the class. These names are case sensitive and you must be specifically careful with them, or the class simply won’t get recognized by the compiler.

Another probability is that the class is not available in the current working directory. This happens after a renaming of package, so be sure that all the required imports got renamed, and check for if the CLASSPATH has ‘.’ ‘the current directory’ in it.

 

2. Variable name

If the issue is in a variable name, then most probably you did not declare your variable before you try to use it. This is also case sensitive, and so check for if you did misspell it.

Another chance is that the variable is written in the wrong scope. Like if you declare the variable in the method A and try to access it from method B. If you need to access the variable in both the methods, you should make it common to both by declaring it in the main method.

class A {
int a=5;
}
class B {
System.out.println(a); //a cannot be resolved
}
}

 

3. Methods

You might have given the method the wrong parameter datatypes, For example, calling an integer where you need a String value.

Another problem might be when you try to invoke a constructor that is not within the scope.

class A
{
private A(int a)
{
}
}

A a=new A(5); //this won’t work, unless you change the scope ‘private’ to ‘public’

A a=new A(“Hello world!”); //this won’t work, unless you change the Parameter type to String

This should be written like

class A
{
A(String a)
{
}
}

 

4. Missing resources R

All the files in the resources directory are assigned an integer value in a common class file called R.java, which is auto generated at build time. This acts as a pointer to all the files in the resources folder. The compiler during compilation simply checks for the R.java file and picks the correct resource from the resources directory using this R.java class file.

Once the compiler picks up the wrong value, it eventually misses the resource file and that is when missing R problem is notified. This happens if you accidentally delete the R.java file, or after a package rename if the CLASSPATH miss the ‘.current directory’.

 

When it comes to Android Studio or IntelliJ, a simple Invalidate and refresh of the IDE’s caches might address the problem.

  • You can safely delete the ‘build’ folder that is in the app directory.
  • Then File -> Invalidate Caches / Restart
  • After the project is reloaded and indexed, do a clean and rebuild from the build menu, so thatĀ build folder will be regenerated and the missed Resources error is gone!

It will still show “cannot resolve R” error, only if you have performed refactoring the package name before. In such case, try importing the R by pressing ALT + ENTER simultaneously.

 

Cannot resolve symbol appstate

This happens when you’re using the old version of basegameutils filesĀ in your project.

AppStateManager is deprecated, and Basegameutils is updated now.

Simply replace the class files GameHelper.java and BaseGameActivity.java with the latest ones found here

https://github.com/playgameservices/android-basic-samples/tree/master/BasicSamples/libraries/BaseGameUtils/src/main/java/com/google/example/games/basegameutils

Leave a Reply

Your email address will not be published. Required fields are marked *