PC パソコン

Causes and Solutions for Android Studio “Cannot resolve symbol ‘R’”

When writing Java or Kotlin code in Android Studio, you may encounter an error called “Cannot resolve symbol ‘R'”.

This error occurs when Android Studio cannot correctly recognize the R class that is automatically generated in an Android project.

The R class contains information for accessing resources in the res folder, such as layouts, strings, images, colors, and IDs.

Therefore, when “Cannot resolve symbol ‘R'” occurs, you need to check not only the Java or Kotlin code, but also XML files, resource names, imports, the namespace, Gradle settings, build errors, and other related elements.

This article introduces the main causes of “Cannot resolve symbol ‘R'” in Android Studio and the basic ways to resolve it.

What Is “Cannot resolve symbol ‘R'”?

In Android apps, the R class is used to reference resources placed in the res folder from code.

For example, to specify a layout, you write the following:

setContentView(R.layout.activity_main);

To reference the ID of a View, you write the following:

Button button = findViewById(R.id.button);

If Android Studio cannot recognize this R, an error like the following may be displayed.

Cannot resolve symbol 'R'

The R class is normally generated automatically at build time based on the resource information in the Android project.

Therefore, if there is a problem with resource compilation or the project build, the R class may not be generated correctly, or Android Studio may not be able to recognize it correctly.

Cause 1: There Is a Syntax Error in an XML File

One of the typical causes of “Cannot resolve symbol ‘R'” is a syntax error in an XML file inside the res folder.

For example, you may have forgotten to close a tag in a layout XML file.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello"

If the XML syntax is incorrect like this, Android’s resource processing may fail and affect the generation of the R class.

Close the tag correctly.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello" />

If the error suddenly appears, checking the XML file you edited immediately before the error occurred can make it easier to find the cause.

Cause 2: There Is an Error in strings.xml, themes.xml, or Another Resource File

Not only layout XML files, but also errors in resource XML files such as strings.xml, colors.xml, and themes.xml may affect the generation of the R class.

For example, you may have forgotten a closing tag in strings.xml.

<string name="app_name">My App

The closing tag should be written as follows.

<string name="app_name">My App</string>

Even a single error in one resource XML file may cause R to become unrecognized in multiple source files at the same time.

Cause 3: A Resource Name Violates the Naming Rules

There are restrictions on the characters that can be used in resource file names inside the res folder.

For example, names like the following may cause problems.

MyImage.png
button-icon.png

For Android resource names, lowercase letters, numbers, and underscores are generally used.

For example, change them as follows.

my_image.png
button_icon.png

If an invalid resource name exists, resource compilation may fail, and as a result, the R class may not be generated correctly.

Cause 4: A Nonexistent Resource Is Being Referenced

The code may be referencing a layout or ID that does not actually exist.

setContentView(R.layout.activity_home);

In this case, if activity_home.xml does not exist in the res/layout folder, that resource cannot be referenced.

Change it to the name of a resource that actually exists.

setContentView(R.layout.activity_main);

If the error occurs immediately after changing a layout name or ID, check whether the old name from before the change remains anywhere in the code.

Cause 5: The Wrong R Class Is Imported

An unintended R class may be imported at the top of the source code.

Pay particular attention to imports like the following.

import android.R;

android.R is a class used to reference resources on the Android system side and is different from your app’s R class.

When using your app’s resources, remove the incorrect import.

// Remove import android.R;

If necessary, import the correct R class for the app.

import com.example.myapp.R;

Because Android Studio’s automatic import may add an unintended R class, also check the import statements when an R-related error occurs.

Cause 6: There Is a Problem with the package or namespace Settings

In an Android project, the namespace of the generated R class is determined by the source code’s package and the namespace configured in Gradle.

For example, suppose build.gradle.kts contains the following setting.

android {
    namespace = "com.example.myapp"
}

Normally, an R class corresponding to this namespace is generated.

If the error occurs after changing the package name or directory structure, check whether the source file’s package declaration, namespace, and imports are correct.

Cause 7: Gradle Sync Is Failing

If synchronization does not complete successfully after changing Gradle settings, Android Studio may not correctly recognize the project structure or generated code.

This may occur after changing dependencies, the Android Gradle Plugin, SDK, namespace, or other settings.

Run Gradle Sync and check whether another error is displayed during the synchronization process.

If Gradle Sync is failing, you need to resolve the first error displayed by Gradle Sync before trying to directly fix “Cannot resolve symbol ‘R'”.

Cause 8: Another Build Error Is Occurring

Because the R class is generated during the build process, another build error in the project may prevent it from being generated correctly.

Therefore, even if only R is displayed in red in a Java or Kotlin file, the actual cause may exist in another XML file or Gradle setting.

Check Android Studio’s Build window and fix the errors in order, starting with the first error that occurs.

Cause 9: The Resource ID Names Do Not Match

The ID defined in XML and the ID specified in the code may not have the same name.

For example, suppose you set the following ID in XML.

android:id="@+id/button_submit"

Use the same name in the code.

findViewById(R.id.button_submit);

If a different name is specified in the code, that resource cannot be referenced.

Check whether the ID names on the XML side and the code side match.

Cause 10: A Different R Is Being Referenced in a Multi-Module Configuration

In Android projects that use multiple modules, an R class corresponding to each module is generated.

Therefore, if the module containing the resource is different from the module containing the code, the dependencies and imports must be configured correctly.

If “Cannot resolve symbol ‘R'” occurs in a multi-module configuration, check which module the R you are currently trying to reference belongs to.

Cause 11: There Is a Problem with Android Studio’s Index or Cache

Even when there are no problems with the code or resources and the project build completes successfully, R may still be displayed in red only in Android Studio.

In this case, the state of Android Studio’s index or cache may be affecting it.

Restarting Android Studio or rebuilding the cache as necessary may improve the situation.

Rebuilding the cache is not a way to fix XML syntax errors or Gradle configuration problems. Try it only after confirming that there are no actual errors in the project.

First Check for Errors in the res Folder

When “Cannot resolve symbol ‘R'” is displayed, first check the files inside the res folder.

In particular, check whether you recently edited files such as the following.

  • Layout files such as activity_main.xml
  • strings.xml
  • colors.xml
  • themes.xml
  • Drawable XML files
  • Menu XML files
  • Navigation-related XML files

Even a single error in one of these resource files may affect the entire R class.

Run Gradle Sync

If there are no obvious problems with the XML files or resource names, run Gradle Sync.

Gradle Sync reloads the project settings, and the error may be resolved when generated classes and dependencies are recognized again.

However, if an error is displayed during Sync, do not ignore the error and repeatedly run Sync. Fix the displayed cause first.

Rebuild the Project

If Gradle Sync completes successfully, rebuild the project.

Rebuilding processes the resources again and may resolve the error by regenerating the R class.

If the rebuild fails, check the first error displayed in the Build window.

Run Clean Project

If the problem appears to be caused by old build results remaining in the project, running Clean Project and then building again may improve the situation.

However, if there is an error in the XML or Gradle settings themselves, running Clean Project will not resolve the problem.

It is important to first check whether there are any actual syntax errors or configuration errors.

Points to Check When “Cannot resolve symbol ‘R'” Appears

When “Cannot resolve symbol ‘R'” is displayed, check the following items in order.

  • Are there any syntax errors in XML files inside the res folder?
  • Are there any mistakes in strings.xml, themes.xml, or similar files?
  • Do the resource file names violate Android’s naming rules?
  • Do the referenced layouts and IDs actually exist?
  • Have you imported the wrong R class, such as import android.R?
  • Are you referencing the correct R for the app?
  • Are there any problems with the package or namespace settings?
  • Has Gradle Sync completed successfully?
  • Is another build error occurring?
  • In a multi-module project, are you referencing the correct module’s R?
  • Is there a problem with Android Studio’s index or cache?

In particular, if all instances of R suddenly appear in red in multiple Java or Kotlin files, the problem may not be in each individual file, but may instead be caused by a single resource error or Gradle error.

Summary

Android Studio’s “Cannot resolve symbol ‘R'” is an error displayed when Android Studio cannot correctly recognize the R class that is automatically generated in an Android project.

Typical causes include XML syntax errors, incorrect resource naming, references to nonexistent resources, importing the wrong R class, package or namespace settings, and failures in Gradle Sync or the build process.

In addition, the problem may not be with R itself. Another resource error or build error may prevent the R class from being generated correctly.

Basically, it is easier to identify the cause by first checking the XML files and resource names inside the res folder, and then checking imports, package, namespace, Gradle Sync, and build errors in that order.

When “Cannot resolve symbol ‘R'” is displayed, rather than looking only at the line where R is shown in red, it is important to check whether there is an error somewhere in the entire project that is preventing the R class from being generated or recognized.