Android R Cannot Be Resolved To A Variable

This is the most common error encountered while working with Android Eclipse ADT or Android Studio IDE. You get this error if R.class file is not generated under gen folder, this file is auto generated every time the build happens, if there is any error encountered while build you get R Cannot Be Resolved to a Variable.

There are many reasons that may cause this error. I am trying to list down as many reasons as I can to my knowledge.

R Cannot Be Resolved to a Variable
R Cannot Be Resolved to a Variable

1. R.java Class Import

Check if android.R import statements are present in any of your Java files under src folder, it is not required as R is static class, but many a time we do Ctrl + Shift + O which causes this import statement being added.

Eclipse somethings cries for these imports but just don't add them, the problem is due to some other stuffs like layouts or drawables.

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!