When writing Java or Kotlin code in Android Studio, you may encounter an error called “Package R does not exist”.
This error occurs when the compiler cannot correctly reference 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 “Package R does not exist” is displayed, you need to check not only the R reference in the code, but also XML file errors, resource names, imports, package and namespace, Gradle settings, build errors, and other related elements.
This article introduces the main causes of “Package R does not exist” in Android Studio and the basic ways to resolve it.
- What Is “Package R does not exist”?
- Cause 1: There Is a Syntax Error in an XML File
- Cause 2: There Is an Error in a Resource XML File Such as strings.xml or themes.xml
- Cause 3: A Resource File Name Violates the Naming Rules
- Cause 4: A Nonexistent Resource Is Being Referenced
- Cause 5: The Wrong R Class Is Imported
- Cause 6: The package Declaration Is Incorrect
- Cause 7: There Is a Problem with the namespace Setting
- Cause 8: R Is Incorrectly Referenced by Its Fully Qualified Name
- Cause 9: Gradle Sync Has Not Completed Successfully
- Cause 10: The R Class Has Not Been Generated Because of Another Build Error
- Cause 11: R from Another Module Is Being Referenced in a Multi-Module Configuration
- Cause 12: There Is a Problem with Android Studio’s Index or Cache
- First Check for Errors in the res Folder
- Check package, namespace, and import
- Run Gradle Sync
- Rebuild the Project
- Run Clean Project
- Points to Check When “Package R does not exist” Appears
- Summary
What Is “Package R does not exist”?
In Android apps, the R class is used to reference resources placed in the res folder from code.
For example, to specify a layout in Java, 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 the compiler cannot correctly reference this R, an error like the following may be displayed.
Package R does not exist
The R class is normally generated automatically at build time based on the resource information in the Android project.
Therefore, if resource compilation has failed or the package of the referenced R class is incorrect, this error may occur.
Cause 1: There Is a Syntax Error in an XML File
One of the typical causes of “Package R does not exist” 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 incorrect, 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, check whether there is a problem with an XML file you edited immediately before the error occurred.
Cause 2: There Is an Error in a Resource XML File Such as strings.xml or themes.xml
Not only layout files, but also mistakes in strings.xml, colors.xml, themes.xml, and other resource XML files may affect the generation of the R class.
For example, a closing tag may be missing in strings.xml.
<string name="app_name">My App
The closing tag should be written correctly as follows.
<string name="app_name">My App</string>
Even a single error in one resource XML file may cause R-related errors to be displayed in multiple Java or Kotlin files.
Cause 3: A Resource File 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, file 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, 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 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 file name or ID, check whether the old resource name 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 another R class, check the import statements when an R-related error occurs.
Cause 6: The package Declaration Is Incorrect
The package declaration written at the beginning of the source file may not match the current project structure.
For example, suppose there is a package declaration like the following.
package com.example.oldapp;
If the project structure has been changed but the old package name remains, the correct R class may not be referenced.
Check the package declaration in the source file and the current project structure.
Cause 7: There Is a Problem with the namespace Setting
In current Android projects, the namespace of the generated R class is determined by the namespace in the module’s Gradle settings.
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 “Package R does not exist” occurs after changing the namespace, check whether the imports or package declaration still use the old settings.
Cause 8: R Is Incorrectly Referenced by Its Fully Qualified Name
If the R class is written using its fully qualified name, it cannot be referenced if the package name is incorrect.
com.example.oldapp.R.layout.activity_main
If the actual R class is generated in com.example.myapp, change it to the correct package name.
com.example.myapp.R.layout.activity_main
After changing the package name or namespace, check whether any old fully qualified names remain.
Cause 9: 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 code.
This may occur after changing dependencies, the Android Gradle Plugin, SDK, namespace, or other settings.
Run Gradle Sync and check whether another error occurs during synchronization.
If Gradle Sync is failing, rather than trying to fix only “Package R does not exist”, first resolve the first error displayed during Sync.
Cause 10: The R Class Has Not Been Generated Because of Another 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.
Therefore, even if “Package R does not exist” is displayed, 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 11: R from Another Module Is Being Referenced in a Multi-Module Configuration
In Android projects that use multiple modules, an R class corresponding to each module is generated.
If the module containing the resource is different from the module containing the code, the dependencies and imports must be configured correctly.
If “Package R does not exist” occurs in a multi-module configuration, check which module the R you are currently referencing belongs to.
Cause 12: There Is a Problem with Android Studio’s Index or Cache
Even when there are no problems with the code or resources and the build completes successfully, an R-related error may remain 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 build errors in the project.
First Check for Errors in the res Folder
When “Package R does not exist” is displayed, first check whether there are any errors in 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.xmlcolors.xmlthemes.xml- Drawable XML files
- Menu XML files
- Navigation-related XML files
Even a single error in one of these files may affect the generation of the R class.
Check package, namespace, and import
If there are no problems with the XML files or resources, check the relationship between package, namespace, and imports.
In particular, immediately after changing the app’s package structure or copying code from another project, an old package name may remain.
If R is imported, check whether it references the correct R class corresponding to the current namespace.
Run Gradle Sync
If no obvious problems are found in the settings, run Gradle Sync.
Gradle Sync reloads the project settings, and the error may be resolved when generated code 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 actual error in the XML or Gradle settings, simply running Clean Project will not resolve the problem.
It is important to first confirm that there are no syntax errors or configuration errors.
Points to Check When “Package R does not exist” Appears
When “Package R does not exist” is displayed, check the following items in order.
- Are there any syntax errors in XML files inside the
resfolder? - 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? - Is the
packagedeclaration in the source file correct? - Is the Gradle
namespacecorrect? - Are there any fully qualified names using an old package name remaining?
- 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 “Package R does not exist” suddenly begins appearing in multiple files, the problem may not be in each individual piece of code, but may instead be caused by a single resource error, Gradle error, or a change to the namespace.
Summary
Android Studio’s “Package R does not exist” is an error displayed when the compiler cannot correctly reference the R class used in an Android project.
Typical causes include XML syntax errors, resource name problems, 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 package, namespace, imports, Gradle Sync, and build errors in that order.
When “Package R does not exist” is displayed, rather than looking only at the line containing the R error, it is important to check whether there is a problem anywhere in the entire project that is preventing the R class from being generated or referenced.
