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.
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:
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:
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:
- open the
Buildoutput and find the first resource error - fix XML or manifest problems before touching Java code
- 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:
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:
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:
Buildand thenClean ProjectBuildand thenRebuild ProjectFileand thenSync Project with Gradle Files- 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
- '
Ris 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
Rfailures are usually secondary effects.
Related reading
- Rabbit Mq java client parallel consumption
- RabbitListener method testing in SpringBoot app
- RabbitMQ and channels Java thread safety
- RabbitMQ client SSL handshake issue on JDK 11
- RCTBundleURLProvider.h file not found - AppDelegate.m
- React-Native Error Failed to install CocoaPods dependencies for iOS project, which is required by this template
- R keras package Error Python module tensorflow.contrib.keras.python.keras was not found
- Rabbit - Error mnesia_unexpectedly_running

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.