Monday 15 December 2014

HB Blog 42: What Is The Purpose Of R.java File In Android Application?

R.java is an auto generated file when you build an android application. It contains unique identifiers (normally 32bit numbers) for elements in each category (drawable, string, layout, color, etc.) of resources (elements under directory res) available in your android application.
The main purpose of R.java file is quick accessibility of resources in the project. If any resource has deleted or added to the project, R.java file will be updated automatically, this can be done by ADT plugin in Eclipse.
The ADT plugin in Eclipse will give you a warning if you try to modify this R.java file. Even if you modified this file, it may result into unknown output of your application. Below is the sample R.java file.

Once you provide a resource in your application, you can apply it by referencing its resource ID. All resource IDs are defined in your project's R class, which the aapt tool automatically generates.
When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory. For each type of resource, there is an R subclass (for example, R.drawable for all drawable resources), and for each resource of that type, there is a static integer (for example, R.drawable.icon). This integer is the resource ID that you can use to retrieve your resource.

Although the R class is where resource IDs are specified, you should never need to look there to discover a resource ID.
A resource ID is always composed of:
    The resource type: Each resource is grouped into a "type," such as string, drawable, and layout.
    The resource name, which is either: the filename, excluding the extension; or the value in the XML android:name attribute, if the resource is a simple value (such as a string).

There are two ways you can access a resource:

    In code: Using a static integer from a sub-class of your R class, such as:
    R.string.hello
    string is the resource type and hello is the resource name. There are many Android APIs that can access your resources when you provide a resource ID in this format. See Accessing Resources in Code.

    In XML: Using a special XML syntax that also corresponds to the resource ID defined in your R class, such as:
    @string/hello
    string is the resource type and hello is the resource name. You can use this syntax in an XML resource any place where a value is expected that you provide in a resource. See Accessing Resources from XML.

How R.java file will be created ?
In R.java file, each resource category will be created as one class. In each resource class all respective elements will be created as static members, means constants. So, we should not change these values. All these are final members also, so we must access them with their class names, like R.drawable.ic_launcher, R.layout.main, etc. See below pictures to know how classes will be created for each resource category.


The values directory under res directory is not a resource category. Some resource types such as strings, colors, styles, arrays, etc. are grouped and named as values. So that, no values class found in R.java file.


How many classes existed with name R in an android application ?
There are 2 classes available in each android application with name R.
1. First class is part of android core system or android SDK, which can be accessed as android.R We can see this class in R.class file which is available in android.jar file, which is automatically included in your project by ADT plugin.
2. Second class is part of our application, which can be accessed as yourpackagename.R (yourpackagename is like my.apps.dialer). We can see this class in R.java file which is available in directory gen. This R.java file is visible, only after successful build of your project.

No comments:

Post a Comment