Friday, April 29, 2016

R.java Internal working and its role in android application.

I am writing this blog because usually android professional use R.java file but most of the people doesn’t know how it works and how it get generated etc. I hope this blog will help to understand such things. You can post me if it helps to you.  
R.java Role and works in java:
R.java Files binds resources and code for the same it use some integer it use public static final int type unique ID. If user users uses Eclipse or android studio and once compile their android project this R.java file is automatically generated in gen folder of project.
Process of R.java
1)      We drag a button from palette and place it in the xml.
2)      As soon as we provide an id to the button, an entry inside R.java class gets created               automatically. As shown in image button1 is added in
R.java -> id -> button1 = some hexadecimal number
3)       We write the code:  Button btn = (Button)findViewById(R.id.button1);
Button btn – Creating a new object of class Button
findViewById(R.id.button1)  – A method to find views in xml files
(Button)  – Casting the view into button type. Casting is needed because findViewById() doesn’t specify the view type. In case the view defined in XML is not found a button, casting as a button in java code will trigger an Invalid cast exception at runtime.
Internal working of R.java
These are the internal works of R.java how it generated and how to it bind the XML and code and how it generate unique ID.
At build time, the aapt tool collects all of the resources you have defined (though separate files or explicit definitions in files) and assigns resource IDs to them.
A resource ID is a 32 bit number of the form: PPTTNNNN. PP is the package the resource is for; TT is the type of the resource; NNNN is the name of the resource in that type. For applications resources, PP is always 0x7f.
The TT and NNNN values are assigned by aapt arbitrarily -- basically for each new type the next available number is assigned and used (starting with 1); likewise for each new name in a type, the next available number is assigned and used (starting with 1).
So if we have these resource files handled by aapt in this order:
layout/main.xml
drawable/icon.xml
layout/listitem.xml
The first type we see is "layout" so that is given TT == 1. The first name under that type is "main" so that is given NNNN == 1. The final resource ID is 0x7f010001.
Next we see "drawable" so that is given TT == 2. The first name for that type is "icon" so that gets NNNN == 1. The final resource ID is 0x7f020001.
Last we see another "layout" which has TT == 1 as before. This has a new name "listitem" so that gets the next value NNNN == 2. The final resource ID is 0x7f010002.
Note that aapt by default makes no attempt to keep these identifiers the same between builds. Each time the resources change, they can all get new identifiers. Each time they are built, a new R.java is created with the current identifiers so your code gets the correct values. Because of this, you must never persistent resource identifiers anywhere where they can be used across different builds of your app.
Once the resources are compiled and identifiers assigned, aapt generates the R.java file for your source code and a binary file called "resources.arsc" that contains all of the resource names, identifiers, and values (for resources that come from separate file, their value is the path to that file in the .apk), in a format that can easily mmapped and parsed on the device at runtime.
You can get a summary of the resources.arsc file in an apk with the command "aapt dump resources <path-to-apk>".
The format of the binary resource table is documented in the header file for the resource data structures here:
The full implementation for reading the resource table on the device is here:

No comments:

Post a Comment