The “Cannot Resolve Symbol R” error is one of the most frustrating issues Android developers face, especially for those working with Java or Kotlin in Android Studio. This error typically prevents successful compilation of your app and can disrupt your entire development process. Thankfully, it’s usually easy to fix once you understand its root causes.

The R
class in Android is an auto-generated file that maps all the resources (like layouts, strings, drawables, etc.) from the res/
folder. When Android Studio fails to generate or locate this file, the “Cannot resolve symbol R” error appears, especially in MainActivity.java
or other Java classes.
Here are the most common causes of this error:
1. XML File Errors
Even a small typo in an XML layout file (like a missing </TextView>
) can break the entire build process. When Android can’t parse XML correctly, it fails to generate the R
class.
2. Incorrect Package Declaration
Ensure the package name at the top of your Java or Kotlin files matches the package defined in your AndroidManifest.xml
.
3. Build Not Synced
Sometimes, Android Studio fails to sync the Gradle build system. This can cause the R
file to not be generated properly.
4. Missing or Corrupted import
Statement
You might mistakenly import android.R
instead of your actual project’s R
class. This replaces your reference to the correct resource file with the Android system’s, which causes issues.
5. Gradle Build Failed
If the Gradle build fails (shown in the Build tab), then the R
class won’t be created. Always check for errors and fix them before proceeding.
Solutions to Fix the Error:
Problem | Solution |
---|---|
XML Syntax Errors | Check and fix all XML files in res/ . Use Android Studio’s XML preview |
Wrong Package Name | Match package name in Java files with AndroidManifest.xml |
Build Not Synced | Click File > Sync Project with Gradle Files |
Wrong Import | Remove import android.R; and use your app’s package import |
Gradle Build Fails | Open Build tab, read logs, and fix all errors |
Bonus Tips:
- Clean and Rebuild: Go to
Build > Clean Project
and thenBuild > Rebuild Project
. - Invalidate Caches: Navigate to
File > Invalidate Caches / Restart
, then click “Invalidate and Restart”. - Check Resource Naming: Resource names must be lowercase and use underscores instead of spaces or capital letters.
- Use External Help: If none of the above works, refer to forums such as Stack Overflow.
This error may seem intimidating, but it’s typically easy to diagnose once you follow the systematic approach outlined above. Keeping your resources clean, your XML valid, and your Gradle in sync will prevent most instances of the “Cannot Resolve Symbol R” issue in Android Studio.