When Eclipse won’t generate/locate R.java

I’ve just come across a rather irritating habit of the ADT toolset for Eclipse and thought I’d share it.

If you get a red line under “R” in a reference to a layout, string or id, (e.g. the “R” in R.layout.main) then your problem could be one of the following:

  • You have ‘android.R’ as an import in your java class. This should not be there, remove it
  • You need to ‘clean’ your project by going to Project > Clean on the Eclipse menu bar
  • You do not have Eclipse set to ‘Build Automatically’. Do this by going to Project in the menu bar and ticking ‘Build Automatically’

My problem, however, was none of these. Eclipse just refused to generate R.java.

It turns out that if you have any errors in your xml files (strings.xml, layouts, etc.), then your R.java file will not be generated and you will not be told this is the reason.

Make sure any errors in your xml files are fixed and your R.java file should be generated automatically when you save.