9

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...

  • first R itself is a class.
  • it had 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

  • what it is basically for
  • how it works
  • why
  • values are in hex
  • what role did it performs while the actual application is running

3 Answers 3

4

"Acts as some pointer to other files" is actually absolutely correct, now the question is which files it points to how it is done.

What does it contain?

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.

Why is it better than just plain file names?

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.

6
  • @malcom: does this file is actually used while app is running?
    – karan
    Apr 13, 2013 at 22:35
  • You can use it if you need to, and you usually do, but not necessarily.
    – Malcolm
    Apr 13, 2013 at 22:35
  • @malcom: i don't want to use it...but does emulator or device use this to refer certain files or values while running the app?
    – karan
    Apr 13, 2013 at 22:37
  • It's just one of the source files of your app which has nothing except fields containing some numbers. If you don't use these fields, they aren't used anywhere.
    – Malcolm
    Apr 13, 2013 at 22:38
  • @KaranMer You'll go back to this question later anyway to accept some answer, so you'll be able to upvote then as well.
    – Malcolm
    Apr 13, 2013 at 22:40
1

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.

3
  • That's the id that it looks for. Set a breakpoint at one of your Views and inspect the id
    – codeMagic
    Apr 13, 2013 at 22:29
  • but how the other files use it...i mean how they refer to it...is it necessary while running the application also?
    – karan
    Apr 13, 2013 at 22:32
  • When it tries to retrieve an id like in my above example it references that file. Yes, very necessary
    – codeMagic
    Apr 13, 2013 at 22:33
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.