What is the class R in Android?

R is a class containing the definitions for all resources of a particular application package. It is in the namespace of the application package.

For example, if you say in your manifest your package name is com.foo.bar, an R class is generated with the symbols of all your resources in com.foo.bar.R.

There are generally two R classes you will deal with

  1. The framework resources in android.R and
  2. Your own, in your namespace

It is named R because that stands for Resources, and there is no point in making people type something longer, especially since it is common to end up with fairly long symbol names after it, that can cause a fair amount of line wrapper.


Android R.java is an auto-generated file by AAPT (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/directory. If you create any component in the activity_main.xml file, id for the corresponding component is automatically created in this file. This id can be used in the activity source file to perform any action on the component.


What is R: There is nothing very mysterious about R. It is just a class that has many static subclasses, open it up in eclipse and take a look (its under gen/).

Every member of this class is one of two things: 1) static final classes or 2) static final integers which are unique with respect to the other members of their class.

Why is it so cryptic: Its easy to get confused because R is automatically generated by ant. Its cryptic because you aren't supposed to "touch" it manually (of course you can but your changes will be automatically erased upon regeneration). Its additionally cryptic because sometimes eclipse automatically imports androids standard R file (as discussed in the answers above)

Why is it always the first one that cannot be resolved: R follows the rules of Java classes and packages exactly, there is nothing special about how R acts with respect to importation. R will be automatically placed in the package specified by the package entry in your manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="package.which.will.contain.R.and.probably.the.main.package.in.the.application" android:versionName="8.6.2011" android:versionCode="1">
    <uses-sdk android:minSdkVersion="13" />

To see what package your R file belongs to just open up the gen/ folder in eclipse (packages view). You will see one package listed there, it will have the name you specified in your manifest. Try to delete it, it will come back if all of your resources follow the correct naming rules and all of your xml files follow the correct xml rules and if build automatically is enabled in eclipse.

The key to understanding the R file is to realize that it is in the same package as the other classes even though it is in a different directory then other files belonging to your "main" package. After you understand this and understand the syntax and naming of resource files troubleshooting problems with R is easy.

Tags:

Android