I have been working on a simple android tutorial and while browsing through the project folders I found this R.java
file in gen
folder...
When I opened it seemed to me as a mess...
R
itself is a class
.multiple Inner classes
defined within eg drawable
,id
,layout
,etc.and that inner classes had lots of variables declared as below which were assigned with hex values
public static final int addr=0x7f080003; ... ... and much more
R is auto generated and acts as some pointer for other files
Questions for R.java
"Acts as some pointer to other files" is actually absolutely correct, now the question is which files it points to how it is done.
R file contains IDs for all the resources in the res
folder of your project and also some additional IDs that you define on your own (in the layouts, for example). The IDs are needed for the Android resource management system to retrieve the files from the APK. Each ID is basically a number which corresponds to some resource in the resource management system.
The file itself is needed so you can access or reference the resource from code by giving the ID of the resource to the resource manager. Say, if you want to set the view in the activity, you call
setContentView(R.layout.main);
main
in the R file contains the number which is understood by the Android resource management system as the layout file which is called main.
It's harder to make a mistake with the generated fields. If you write the field name incorrectly, your program won't compile and you will know that there's an error immediately. If you write an incorrect string, however, the application won't fail until it is launched.
If you want to read more on this topic, you should check the Android documentation, especially the Accessing Resources part.
This holds your resource ids. So when you do something like
TextView tv = (TextView) findViewById(R.id.mytextview);
it looks up your id here for that View
, layout
, etc... This way the app has an easy way to look up your ids while you can use easy to remember names. Anytime you create a resource
it automatically creates an id
for it and stores it here. That's why you never want to try and edit this file yourself.
One way to think about how valuable R.java is, imagine a world without it. Its amazing how android brings the xml and java world together to help avoid coding the UI manually completely. With legacy java building UI using the java language was a pain. Invaluable.
With Android you can not only build your UI using only xml, but also see it while you build it. Invaluable.
Every element in the xml can be referenced in the java code WITHOUT writing a single line of code to parse the xml :). Just R.id.nameOfElement. Invaluable.
Rapid development is beautifully done in android. Imagine if iPhone would have 5000 screens to fit that one piece of code, they would crumble on their XCode. Google has done a wonderful job with just R.java. Invaluable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With