What is the class R in Android?

android

Solution

`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

- The framework resources in `android.R` and

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

Problem

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`?

Original source