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.

2. Drawables

Drawables (images) or xml's contained here are the real culprits most of the times for such issues. You won't even see a red error cross displayed over the drawable folder (but if you clean and build your project you would find the error messages displayed under logCat console)

Check that images and XML files follows the below mentioned naming conventions,

  • All file names must be in lower-cases
  • Files under drawables must not contain any special characters other than _ (underscore)
  • Files must not contain numeric values.
Drawables Error resulting in R Not found error
Drawables Error resulting in R Not found error

3. AndroidManifest.xml

If you have changed the package name for your app in the Manifest file, this may not generate R.class resulting in such a error. Make sure you clean and build.

4. Check Order of Export

Right-Click on you project

Project Properties

Java Build Path

Order and Export

5. Check if Android Target is set.

Right-click on your Project Folder.

Select Properties.

Select Android.

Check the box next to appropriate Project Build Target.

Click Apply and OK.






Hello there!,

You are using AdBlocker!

I am an independent developer trying to help fellow developers and students to resolve issues faced on a day-to-day basis, we 'Code to Care' . There is a lot of expenses involved in hosting and managing a website that's the reason we have ads.

A humble request to you to disable adBlocker on Code2care.org and support us pay our bills.

Thanks,
Cod2care - Line's of code for change.

To see contents reload page after unblocking
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities. Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, Tips-n-tricks, How Tos, Blogs, Articles, etc. on various platforms like Windows, Mac, Linux, Mobile platforms, etc. Technologies/Languages like Java, Objective-C, PHP, .Net, Android, SharePoint, jQuery, HTML, CSS, etc.