Android: what is R? Why is it so cryptic?

I am trying to duplicate a sample NotePad example so that if I mess around with the source files to learn from the experiments, the sample source files will not be affected.

Alas, despite the following very clean steps in an unspoilt environment, R turned against me, again !

I got about 20 " R could not be resolved to the variable" errors ...

Now this is like catch 22: I'm trying to learn a NotePad sample, so I have a chance to understand what R. is, but I donโ€™t really like R, so that does not give me the opportunity to learn a NotePad sample.

To add insult to injury, when I google for R , I get completely insignificant results.

  • What is R ?
  • Why is it so cryptic?
  • Why is this always the first thing that cannot be allowed?

Update: I noticed that in the HelloAndroid project (XML layout version) there is a generated Java file called R.java . Is this the infamous R that the project developer complains about?

+67
android android-manifest
Feb 10 2018-11-11T00:
source share
3 answers

R is a class containing definitions for all resources of a particular application package . It is located in the application package namespace .

For example, if you say in your manifest that your package name is com.foo.bar , the class R generated with the characters of all your resources in com.foo.bar.R

There are usually two R classes that you will deal with.

  • Framework resources in android.R and
  • Your own in your namespace

It is called R because it means R sources, and it makes no sense to get people to print something longer, especially since it usually ends with rather long character names after it, which can cause a sufficient number of line wrappers.

+98
Feb 10 '11 at 4:11
source share

What is R: There is nothing very mysterious about R. It's just a class that has many static subclasses, open it in an eclipse and watch (its under Gen /).

Each member of this class is one of two things: 1) static finite classes, or 2) static finite integers that are unique to other members of their class.

Why this is so mysterious: It is easy to confuse, because R is automatically generated by ant. Its mysterious, because you do not have to โ€œtouchโ€ it manually (of course, you can, but your changes are automatically erased during regeneration). It is also cryptic because sometimes eclipse automatically imports androids standard R file (as described in the answers above)

Why is it always the first that cannot be resolved: R exactly follows the rules of Java classes and packages, nothing special about how R acts with respect to imports. R will be automatically placed in the package specified in the package entry in the manifest file

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="package.which.will.contain.R.and.probably.the.main.package.in.the.application" android:versionName="8.6.2011" android:versionCode="1"> <uses-sdk android:minSdkVersion="13" /> 

To find out which package belongs to your R file, simply open the gen / folder folder in eclipse (package view). You will see one package specified there, it will have the name that you specified in the manifest. Try deleting it, it will return if all your resources follow the correct naming rules, and all your xml files follow the correct xml rules, and if the assembly is automatically included in eclipse.

The key to understanding the R file is understanding that it is in the same package as other classes, even if it is in a different directory, and then other files belonging to your โ€œmainโ€ package. Once you understand this and understand the syntax and names of the resource files, troubleshooting with R is easy.

+17
Mar 28 2018-12-12T00:
source share

R is the name for your resources. Any resource that you access through R.$FOLDER.$RESOURCE or something very similar. If it cannot be resolved, make sure that the path is correct and the resource to which there is a link (case sensitive; includes the extension). Also, the confusing part (for me anyway) is that there are two different R . . If you get a lot of "Failed to resolve" errors, try to see that you are importing. Try changing or deleting it. Or you can try to clean your project (WARNING: Sometimes this makes things worse than they are).

+2
Feb 10
source share



All Articles