R! where are you?

Where R you?

What’s R?

R is a class, auto-generated by the Android Asset Packaging Tool (aapt), that aims to maintain the references of all the provided resources. Through the nested classes of R, infact, is possible reach every type of resources (the complete list could be found in the official R reference).

When is auto-generated?

R generated code

Aapt (re) generate this class every time a build is executed on the project. If you use Eclipse as IDE, in the project menu you can set the build operation as automatic, selecting “Build Automatically”, so each time there is a change in your code the R class is updated.

When is not so?

The R class will not be generated when in at least one resource (layout, string, color … etc) there is at least an error. Eclipse in this case cannot compile the project, and you, cannot go ahead. Is important to know that Eclipse can only recognize XML parsing error and not all the other that we can do developing android apps, so be aware when you write some resources like strings, style, and layouts.

“R cannot be resolved to a variable”… WTF?

Many times during the developing of an application for Android we see the message “R cannot be resolved to a variable” that comes from the compiler. This means that the R class is not generated, infact you can see that there isn’t the R.java file in the folder “gen” (in the sub-folder that correspond to your package name). How to resolve this creepy situation? Here there is the list of some solutions that helped me in many cases:

  • Execute a clean of the project (“Project->Clean…” using Eclipse)
  • Check for duplicated strings and color
  • Check the java files for import android.R; and if you find them, delete them
  • If you have changed the package name in the AndroidManifest.xml file you have to rename also the subdirectory in gen to match the new name of the package
  • In any other case I suggest you to restart Eclipse and pray (sometimes work).

If there are any suggestion to perform this guide write them to me, I will accept them with pleasures.

One Comment on “R! where are you?”

  • I love it whenever pesople get tοgether aոd share ideas.
    Great website, stiϲk with it!

    posted on 6th of February, 2014 at 12:48 pm

Leave a comment

Your name*

Your email*

Your website

Your comment*