Java
programming
error
Android Studio
troubleshooting

R cannot be resolved to a variable?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The Android error R cannot be resolved to a variable usually does not mean Java forgot what a variable is. It means the generated R class was not created correctly, the wrong R was imported, or the file is being compiled in the wrong package context.

What the R Class Actually Is

In an Android project, R is a generated class that contains integer identifiers for resources such as layouts, strings, colors, and drawables. When resource compilation succeeds, you can reference values like R.layout.activity_main or R.string.app_name from Java or Kotlin.

A normal usage looks like this:

java
1package com.example.demo;
2
3import android.os.Bundle;
4import androidx.appcompat.app.AppCompatActivity;
5
6public class MainActivity extends AppCompatActivity {
7    @Override
8    protected void onCreate(Bundle savedInstanceState) {
9        super.onCreate(savedInstanceState);
10        setContentView(R.layout.activity_main);
11    }
12}

If Android Studio says R cannot be resolved, the cause is usually upstream. The fix is rarely to change that line alone.

Check Resource Errors First

The most common root cause is a resource file that failed to compile. If a layout XML file, value resource, or manifest entry contains an error, Android cannot generate the app's R class.

For example, a malformed layout can break resource generation:

xml
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3    android:layout_width="match_parent"
4    android:layout_height="match_parent"
5    android:orientation="vertical">
6
7    <TextView
8        android:layout_width="wrap_content"
9        android:layout_height="wrap_content"
10        android:text="Hello" />
11
12</LinearLayout>

If a tag is not closed properly, an attribute name is invalid, or a resource reference points to something that does not exist, the R file may stop generating. The right debugging sequence is:

  1. open the Build output and find the first resource error
  2. fix XML or manifest problems before touching Java code
  3. rebuild after the resource layer is clean

The first error matters most. Later errors are often just fallout.

Remove the Wrong R Import

Another classic cause is accidentally importing android.R instead of your application's R class. That import points to framework resources, not your app's resources, so your custom layouts and IDs disappear.

This is wrong:

java
import android.R;

In most app classes, the correct fix is to remove that import entirely. Your own R class in the current package will then be used automatically, or Android Studio will import the correct app package version.

After removing the bad import, the file should look more like this:

java
1package com.example.demo;
2
3import android.os.Bundle;
4import androidx.appcompat.app.AppCompatActivity;
5
6public class MainActivity extends AppCompatActivity {
7    @Override
8    protected void onCreate(Bundle savedInstanceState) {
9        super.onCreate(savedInstanceState);
10        setContentView(R.layout.activity_main);
11    }
12}

If the IDE keeps re-adding the wrong import, there is usually still another unresolved issue in the project.

Verify Package and Build State

If resources are valid and imports are correct, check package consistency. The Java file package, the manifest package or namespace configuration, and the generated sources all need to line up.

You should also force a fresh build state. In current Android Studio and Gradle projects, a safe recovery sequence is:

  1. Build and then Clean Project
  2. Build and then Rebuild Project
  3. File and then Sync Project with Gradle Files
  4. if needed, close the IDE and reopen the project

That sequence does not fix broken XML, but it does clear stale build artifacts after you correct the real problem.

One more area to inspect is resource naming. Android resource names must use lowercase letters, digits, and underscores. A drawable named MyIcon.png or a layout named Login Screen.xml can cause build failures that eventually surface as an R resolution error.

Common Pitfalls

The biggest pitfall is treating R cannot be resolved as a Java syntax problem. In most cases the Java file is fine and the resource pipeline is broken.

Another frequent mistake is fixing the symptom instead of the cause. Deleting imports, invalidating caches, or restarting the IDE can help only after the actual XML or Gradle issue has been corrected.

Developers also often miss the android.R import because the file still compiles partially and the editor auto-completes names. That single import is enough to hide the app's own resource identifiers.

Finally, do not ignore the first build error. Once resource generation fails, many unrelated files may show R errors. The earliest resource compiler message usually tells you where the real defect is.

Summary

  • 'R is a generated Android resource class, not a normal hand-written variable.'
  • The most common cause of this error is a broken XML resource or manifest entry.
  • A wrong import android.R; line can also trigger the problem.
  • Clean and rebuild only after fixing the underlying resource or package issue.
  • Start with the first build error, because later R failures are usually secondary effects.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.