115

In AndroidStudio, when I create a project using an empty activity, I get the following piece of code in the MainActivity.java file:

package my.company.my_proj;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

where a cryptic class named R is used. What is the purpose of this class R?

nbro
  • 15,395
  • 32
  • 113
  • 196
Android Eve
  • 14,864
  • 26
  • 71
  • 96

4 Answers4

144

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

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

There are generally two R classes you will deal with

  1. The framework resources in android.R and
  2. Your own, in your namespace

It is named R because that stands for Resources, and there is no point in making people type something longer, especially since it is common to end up with fairly long symbol names after it, that can cause a fair amount of line wrapper.

nbro
  • 15,395
  • 32
  • 113
  • 196
hackbod
  • 90,665
  • 16
  • 140
  • 154
  • 16
    After solving all problems and proceeding with the book, I can add (from the book): This is an auto-generated **utility class** that provides references to the resources in the project. It is called the "R class" because its name is **R.java**. It is generated by AAPT from the resourced defined within the **res** folder. – Android Eve Feb 11 '11 at 18:59
  • 27
    There is a point making people typing something longer: it's called clean code and might not have reached everyone yet. One letter classes, extensions, variables maybe fine in a local context, but never globally. Ugly names like this lead to redundant questions like this. – brainray Dec 16 '15 at 16:51
23

What is R: There is nothing very mysterious about R. It is just a class that has many static subclasses, open it up in eclipse and take a look (its under gen/).

Every member of this class is one of two things: 1) static final classes or 2) static final integers which are unique with respect to the other members of their class.

Why is it so cryptic: Its easy to get confused because R is automatically generated by ant. Its cryptic because you aren't supposed to "touch" it manually (of course you can but your changes will be automatically erased upon regeneration). Its additionally cryptic because sometimes eclipse automatically imports androids standard R file (as discussed in the answers above)

Why is it always the first one that cannot be resolved: R follows the rules of Java classes and packages exactly, there is nothing special about how R acts with respect to importation. R will be automatically placed in the package specified by the package entry in your 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 see what package your R file belongs to just open up the gen/ folder in eclipse (packages view). You will see one package listed there, it will have the name you specified in your manifest. Try to delete it, it will come back if all of your resources follow the correct naming rules and all of your xml files follow the correct xml rules and if build automatically is enabled in eclipse.

The key to understanding the R file is to realize that it is in the same package as the other classes even though it is in a different directory then other files belonging to your "main" package. After you understand this and understand the syntax and naming of resource files troubleshooting problems with R is easy.

tjb
  • 11,480
  • 9
  • 70
  • 91
  • Oh no, it's very very very very very very very very very very very very very very very very mysterious - Cannot resolve symbol 'R'. (16 different Rs) – AriesConnolly Dec 14 '22 at 18:35
5

Android R.java is an auto-generated file by AAPT (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/directory. If you create any component in the activity_main.xml file, id for the corresponding component is automatically created in this file. This id can be used in the activity source file to perform any action on the component.

Sahil
  • 75
  • 2
  • 9
4

R is the name for your resources. Any resource you access by R.$FOLDER.$RESOURCE or something very similar.

If it can't be resolved, make sure the path is correct and the referenced resource exists(case-sensitive; include the extension).

Also, the confusing part (to me, anyway) is that there are two different R's. If you're getting a lot of "Cannot be resolved" errors, try seeing what you're importing. Try changing or removing it. Or you can try cleaning your project (WARNING: Sometimes that makes things worse than they already are).

nbro
  • 15,395
  • 32
  • 113
  • 196
John
  • 15,418
  • 12
  • 44
  • 65
  • Yes, cleaning my project seems to be a daily routine -- eclipse is always going haywire and forgetting things, but a good cleaning seems to fix things up (though I've never experienced things being worse than they already are, as John mentions). – Brian D Feb 10 '11 at 03:46
  • @Brian: When I clean my project, Eclipse destroys and regenerates my `R` file. Once in a while, it misses the "regenerate" step leaving me without(or with an empty) `R` and `R` is programmed to automatically undo any changes made by hand. – John Feb 10 '11 at 03:49
  • @John: what OS are you using, just out of curiosity? – Brian D Feb 10 '11 at 03:56
  • @John, Cleaning doesn't help in my case. You can see the actual list of errors in this thread (which hasn't been answered yet): http://stackoverflow.com/questions/4928784/how-to-duplicate-an-sdk-sample-project-into-workspace – Android Eve Feb 10 '11 at 04:04