27

Android applications have an automatically generated file called "R.java". Please explain its purpose.

E.g.

  • What is contained in it?
  • What generates it?

Duplicate: What is the concept behind R.java?

Community
  • 1
  • 1
pawarrohit14
  • 497
  • 1
  • 4
  • 15
  • 18
    Would have to disagree with the closure reason, I came with this exact question also. I feel it perfectly valid to ask high-level questions of purpose of elements of application architecture, especially those that are automatically generated. If we restrict questions to only those with specific detail, we miss the opportunity to provide introductory information to new users. Additionally the question has been viewed many times and has an accepted answer, validating its purpose. – J Collins Jan 24 '13 at 13:06
  • 1
    I agree it is a worthy question. However, it is a duplicate: http://stackoverflow.com/questions/10004906/what-is-the-concept-behind-r-java – Nick Jul 09 '13 at 11:40
  • 6
    Then it should be closed as a duplicate instead. Instead it's closed as "not a real question", which is just plain wrong. – BrainSlugs83 Jul 30 '13 at 20:44
  • yes, this question is quite broad, but it's a valid question. Also, it's not even a duplicate. Those two questions are different, from different background knowledge and perspective. – starriet Oct 22 '20 at 01:28

3 Answers3

37

Every application uses resources such as strings, drawbles, layouts, and styles. Instead of hard coding such resources into an application, one externalizes them and refers to them by ID. The R.java file contains all those resource IDs, whether assigned by the programmer or generated by the sdk.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
jeet
  • 29,001
  • 6
  • 52
  • 53
11

R.java is where you have access to anything you have in your resources:

  • Drawables
  • Layouts
  • Strings
  • Arrays
  • ....etc

You can read more here.

iTurki
  • 16,292
  • 20
  • 87
  • 132
5

R.java is autogenerated on build. It's content is based on the resource files (including layouts and preferences).

When you delete it, it gets recreated, but if you create your own, you will get into trouble as the build system will not be able to replace it.

Community
  • 1
  • 1
Chirag
  • 56,621
  • 29
  • 151
  • 198