What is the class R in Android?

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Solution 3 - Android

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.

Solution 4 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndroid EveView Question on Stackoverflow
Solution 1 - AndroidhackbodView Answer on Stackoverflow
Solution 2 - AndroidtjbView Answer on Stackoverflow
Solution 3 - AndroidSahilView Answer on Stackoverflow
Solution 4 - AndroidJohnView Answer on Stackoverflow