PC パソコン

Causes and Solutions for Android Studio “Unresolved reference: R”

When writing Kotlin or Java code in Android Studio, you may encounter an error called “Unresolved reference: R”.

This error occurs when the R class, which is automatically generated in an Android project, cannot be referenced from the code.

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

Therefore, when “Unresolved reference: R” occurs, you need to check not only the Kotlin code itself but also XML file errors, resource names, the package and namespace, Gradle settings, and other related elements.

This article introduces the main causes of “Unresolved reference: R” in Android Studio and the basic ways to resolve it.

What Is “Unresolved reference: R”?

In Android apps, the R class is used to access layouts, strings, images, and other 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:

val button = findViewById<Button>(R.id.button)

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

Unresolved reference: R

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

Therefore, if there is a problem with resource files, build settings, or similar elements, the R class may not be generated correctly, resulting in an error.

Cause 1: There Is an Error in an XML File

One of the typical causes of “Unresolved reference: R” is a syntax error in an XML file inside the res folder.

For example, a tag in a layout XML file may not be closed correctly.

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

If the XML syntax is broken, Android’s resource processing may not complete correctly, and as a result, the R class may not be generated.

Write the tag and attributes correctly.

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

If “Unresolved reference: R” suddenly appears, check whether there is an error in an XML file that you edited immediately before the error occurred.

Cause 2: A Resource Name Violates Android Naming Rules

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

For example, file names like the following may cause problems.

MyImage.png
button-icon.png

For Android resource file 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 may also affect the generation of the R class.

Cause 3: A Nonexistent Resource Is Being Referenced

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

setContentView(R.layout.activity_home)

In this case, if activity_home.xml does not exist in res/layout, 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 file name or ID, check whether the old resource name remains anywhere in the code.

Cause 4: The Wrong R Class Is Imported

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

One import that requires particular attention is 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 android.R, also check the import when “R is recognized, but the intended resource cannot be found.”

Cause 5: The package and namespace Settings Do Not Match

In an Android project, the R class that is referenced may change depending on the relationship between the source code’s package and the namespace configured in Gradle.

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

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

In this case, an R class corresponding to this namespace is normally generated.

If “Unresolved reference: R” occurs after changing the package structure, check the namespace, imports, and the package declaration in the source file.

Cause 6: Gradle Sync Has Not Completed Successfully

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

For example, an error may occur after changing dependencies, the Android Gradle Plugin, SDK settings, or similar configurations.

Run Gradle Sync in Android Studio and check whether another error occurs during synchronization.

If Gradle Sync itself fails, you need to resolve the cause of the synchronization error before addressing “Unresolved reference: R”.

Cause 7: The R Class Has Not Been Generated Because of a Build Error

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

As a result, even if an error is displayed on R in a Kotlin file, the actual cause may exist in another file.

“Unresolved reference: R” may not be the cause itself, but may instead be displayed as a result of another build error.

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

Cause 8: There Is a Mistake in a Resource XML File Such as strings.xml

Not only layout files, but also problems in resource XML files such as strings.xml, colors.xml, and themes.xml may cause resource generation to fail.

For example, an XML tag may not be closed correctly.

<string name="app_name">My App

A closing tag is required.

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

Even a single error in an XML file may cause R-related errors to appear simultaneously in multiple Kotlin files.

Cause 9: The Resource ID Is Written Incorrectly

There may be a mistake in the way an ID is assigned to a View in XML.

Normally, when defining a new ID, you write it as follows.

android:id="@+id/button_submit"

On the Kotlin side, it can be referenced as follows.

R.id.button_submit

Check whether the ID name in the XML matches the ID name specified in the Kotlin code.

Cause 10: You Are Trying to Reference R from Another Module

In Android projects that use multiple modules, the module in which a resource is defined may be different from the module in which the code exists.

Because a different R class is generated for each module, dependencies and imports must be configured correctly when using resources from another module.

If “Unresolved reference: R” is displayed in a multi-module configuration, check which module’s R you are trying to reference.

Cause 11: A Problem Occurred After Changing BuildConfig or Generated Code Settings

Immediately after changing Gradle or Android Gradle Plugin settings, recognition of generated code may become disrupted.

In this case, run Gradle Sync or rebuild the project and check whether there are errors in the configuration files.

In particular, after changing the Android Gradle Plugin version, old settings may not match the current configuration.

First Check XML and Resource Errors

When “Unresolved reference: R” is displayed, it is important not to check only the Kotlin file in which R is displayed in red.

Because R is generated from resources in the project, the cause may be 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

If any of these files display an error in red, fix that error first.

Run Gradle Sync

If you cannot find any obvious problems in the resources or code, run Gradle Sync.

Gradle Sync reloads the project settings, and the error may be resolved when Android Studio recognizes generated classes and dependencies again.

However, if an error is displayed during synchronization, do not simply repeat Sync. Check the error message and fix the cause.

Rebuild the Project

If Gradle Sync completes successfully, rebuilding the project may regenerate the R class and resolve the error.

Run a build or rebuild of the project from Android Studio’s Build menu and check the first error that is displayed.

If the rebuild fails, fix that build error first.

Try Clean Project

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

However, if “Unresolved reference: R” is caused by an error in resource XML or Gradle settings, Clean Project alone will not resolve it.

It is important to first confirm that there are no actual syntax errors or configuration errors.

When the Android Studio Cache Is the Cause

If there are no problems with the code, resources, or Gradle settings, and the build completes successfully, but R is still not recognized only in Android Studio, the IDE’s index or cache may be affecting it.

In this case, restarting Android Studio or rebuilding the cache as necessary may improve the situation.

Deleting or rebuilding the cache is not a way to fix XML or Gradle errors. Try it only after confirming that there are no actual build errors in the project.

Points to Check When “Unresolved reference: R” Appears

When “Unresolved reference: R” is displayed, check the following items in order.

  • Are there any syntax errors in XML files inside the res folder?
  • Do resource names such as images and XML files violate the naming rules?
  • Do the referenced layouts and IDs actually exist?
  • Have you imported the wrong R class, such as import android.R?
  • Are you correctly referencing the app’s R class?
  • Are there any problems with settings related to namespace or package?
  • Has Gradle Sync completed successfully?
  • Are there any other build errors in the project?
  • Are there any errors in files such as strings.xml or themes.xml?
  • In a multi-module project, are you referencing resources from the correct module?

In particular, if all instances of R suddenly turn red in multiple Kotlin files, the problem may not be in each individual file. Instead, a single XML or Gradle error may be preventing the entire R class from being generated correctly.

Summary

Android Studio’s “Unresolved reference: R” is an error displayed when the R class used in an Android project cannot be referenced from the code.

Typical causes include XML syntax errors, invalid resource names, references to nonexistent resources, importing the wrong R class, and problems with namespace or Gradle settings.

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, namespace, Gradle Sync, and build errors in that order.

When “Unresolved reference: R” is displayed, rather than looking only at the line where R is shown in red, it is important to check the project from the perspective of “whether there is an error somewhere in the project that is preventing the R class from being generated.”