This is a very old question, but it still happens a lot and there is no truly comprehensive answer here.
I have had this problem more times than I want to admit. It can be caused by a wide variety of issues that mostly all have to do with your project structure not matching what’s expected. Here’s a list of all the problems I’ve had:
Importing from Eclipse to Android Studio does not always work smoothly, especially with older versions of Android Studio. Make sure you’re using the correct directory tree. It should look like this for a moderately mature app:
AppName/ // AppName is your app's name, obviously
.gradle/ // This is a compiler created directory. You should normally leave it alone
.idea/ // Ditto
build/ // This too
gradle/ // And this
app/
build/
libs/
src/
androidTest/ // Tests based on the Android instrumentation runner should go here
main/
java/
fully/ // Typically com
qualified/ // the domain name. Longer package
// names may have deeper folder trees
AppName/ // All your java files go here
res/
drawable-*/ // One of these for each resolution you support
layout/ // All of your general layouts
menu/ // All of your menu layouts
values/ // All of your resource xml files
xml/ // PreferenceScreen layouts go here
AndroidManifest.xml
debug/
test/ // Pure jUnit tests should go here
.gitignore
app.iml
build.gradle // This is the gradle file you should be making most changes to
proguard-rules.pro
.gitignore
build.gradle // This gradle file should mostly be left alone
gradle.properties
gradlew
local.properties
AppName.iml
settings.gradle
It may not look like this in your IDE. There is a drop-down above the file tree that displays your files in different ways, and it defaults on my system to one called Android that breaks your files out into their different types. It’s easier to fix this kind of problem from the one called Project, and even easier directly from your OS’s file system, because Android Studio collapses folders that only contain another folder onto a single line.
Especially if you ever change the name of your app, you need to make sure that the source tree main/java/com/domain/AppName is updated. Also make sure that the package tag in your AndroidManifest.xml is correct.
If there are errors in either your Gradle files or your AndroidManifest.xml, this will prevent Android Studio from properly building your resource files. Gradle files can be broken by upgrading Android Studio sometimes, especially from the pre-1.0 versions. Sometimes this is because it stops supporting older versions of the Gradle plugin, so you need to update your version numbers. It can sometimes be hard to find what the current versions are. As of today, 7/17/15, my apps are compiling fine with com.android.tools.build:gradle:1.2.3
. This is in the dependencies block in the outermost gradle file,
If your AndroidManifest references a non-existent drawable or string resource or activity, it will break and cause this error. Sometimes if anything references a nonexistent drawable or string resource you will get this error.
If you have a file in your resources that is corrupted, or an invalid xml file, you will get this error.
In my experience, sometimes Android Studio just hiccups for no reason, and you need to restart it and/or your PC. I don’t know why, but sometimes it works.
If you have two xml resources with the same name, in directories that do not override each other, you can have this problem. For instance, you can have the same name in drawable-mhdpi and drawable-xhdpi because they override each other depending on the target device, but if you have the same name in layout and in menu, it will cause a problem. Rename or delete one of the files.
If only some resources are having this problem, those resources are most likely in the wrong directory.
In one case I had to completely reinstall Android Studio. I don’t know what was wrong, but it worked.
In one case I moved my entire project to a different directory and re-imported it as a new project. I don’t know what was wrong, but it worked.
Xml files with reserved words for names can cause this problem. Rename or delete them.
There are a few ways your Gradle file can end up referencing a version of the build-tools that you do not have installed. Correct this by changing Gradle or downloading the appropriate build-tools.
Finally, after you’ve fixed whatever is wrong, you need to clean your Gradle project. You do this by going to the Build menu at the top and selecting Clean Project.
There are different kinds of errors that android developers come across while developing applications while using android studio. The errors form part of everyday experience as they provide an opportunity to explore and learn every day.
In this article, we shall focus on how to debug or solve the error that usually occurs most of the time when the android studio is opened and a project has not to build successfully which is cannot resolve symbol R in android studio or it can also be presented as unresolved reference R
With the way android projects are structured, that is with an error, the project cannot run successfully, it is a requirement that all errors must be fixed
Sometimes as a developer, you might not understand what is causing the error cannot resolve symbol R or unresolved reference R in your project
In most circumstances, the way one can solve the error cannot resolve symbol R or unresolved symbol R is not the way another one can. The error may occur due to various issues which we are going to provide solutions in this article.
To know that your project has the error cannot resolve symbol R or unresolved reference R is that, in the kotlin activity (.kt) or java activity (.java) files, the R will always be in red as shown below
In some cases, the red in the letter R may not be visible until when you run or build your project and that’s when the letter R will turn red and also display the errors in the build output as shown below
One thing with android is when it displays an error and you debug in the logcat or in the messages view section, it always points to the specific piece of code that has an error, the only problem will be if you are not familiar with how to solve the error that is present
How to fix error cannot resolve symbol R in android studio
As we have highlighted above, the error can also be presented as unresolved reference R.
There are different ways in which the error can be fixed.
(i) Ensure the package name is correct
A package name is that name that uniquely identifies your application on your device and it is the same used in the google play store to identify the application. Mostly the package name in android in default is com.example.projectname
From a previous tutorial, we discussed why most developers have a problem with the package name and we noted that it’s due to copying pieces of code from a tutorial and therefore fails to be compatible with their project.
It’s therefore recommended to update the package name by changing or renaming the package name in android
To ensure that the package name for your app is correct, compare what you have in your activity either java or kotlin file with what is there in the android manifest file and also in build.gradle file for the app
- The activity file package name is shown below
- For the manifest file is as follows
- For the build.gradle file for the app is as follows
From the above screenshots, it is clearly noted that the package name present in build.gradle file for app and in the android manifest file is com.example.loginproject while the one in kotlin activity file is package com.example.signupproject
If the package name is not correct in the kotlin or java activity is not the same as that in build.gradle file for the app and in the android manifest file is not the same, the error cannot resolve symbol R or unresolved reference R will be present
To fix the error, ensure that you update the package name in the activities that you have for your project to match the one in the manifest file and also in build.gradle file
When you update the package name in your activities to the correct package name, the red in the letter R will automatically disappear
(ii) Clean and Rebuild the project
In cases where you do not know what is causing the error exactly, the first step to debug the error is to clean and rebuild the project.
The clean and rebuild the project ensures that if there are pieces of code that are not perfectly placed in their positions and if there are unnecessary pieces of code, the clean and rebuild ensures that only the required files are the ones in use
To clean the project,
- in android studio click build and then clean project
To rebuild the project,
- in android studio, click build and then build project
If there are no other existing errors in the code, the clean and rebuild project will resolve the error
(iii) Make the project
The make project in android studio runs through all the files to diagnose if there are cached files that have not yet been updated to the current version of the code.
If there were errors in your project and you have resolved them, the make project will run over your project and update it to the current version
To make the project,
- When using the Windows operating system, click ctrl and f9 and wait for it to build
- Or click the hammer icon in the android studio at the second navigation at the top
(iv) Sync your project with Gradle files
Syncing your project with Gradle files ensures that your project will pick the settings in the Gradle files and use them in the interior files.
The sync is necessary since when any dependency or feature is added in the Gradle file and you fail to sync and then you try to use the features that you have imported, your code will display errors
To sync your project with Gradle files,
- In the android studio, click file and then sync project with Gradle files
Conclusion
With the four methods we have discussed above, the error cannot resolve symbol R or unresolved reference R will be fixed.
Note that in some cases, you can use any of the four methods and your error will be solved or you may be forced to try all of the above four methods to solve the error
That’s it for this article, I hope you have solved the error successfully and you can now run the project and install the apk in your mobile device
You must have encountered the error “Cannot resolve symbol R” many times while building android projects. When you first create a new activity or new class, The R is red and Android Studio says it can’t recognize the symbol R. So you may hover over the R symbol, press Alt + Enter to import missing appropriate files. But doing so doesn’t fix this error. The letter R stands for the Resource. This error occurs because of the incapability of the build process to Sync Resource files with your projects. Usually, this happens because of the improper build of the project.
Why There is a Need to fix “cannot resolve symbol R” in Android Studio?
The error “Cannot resolve symbol R” in Android studio has faced mostly, when you shift your code to another Computer or send the code to another person. The “R” turns red and can’t run the Application and throw the error in “Logcat”. Most often “R cannot be resolved” error appears if there is an issue with some of your resource files. Due to this error, you are unable to build your application. That’s why we need to solve this error as it not getting away by just doing a simple restart or hitting Alt+Enter.
Now the point that comes here is how we can fix “cannot resolve symbol R” in Android Studio. So in this article, we are going to discuss six different methods to fix “cannot resolve symbol R” in Android Studio.
Method 1
Try the sync Gradle. Just follow this path: File > Sync project with Gradle Files.
Method 2
Change Gradle version: Open your build.gradle file, search for gradle version and change the version. Say suppose you are using minSdkVersion 8 then change it to 9 and then build your project and also try to change buildToolsVersion. To change it follow this path: File > Project Structure > Modules.
After selecting the appropriate version click “OK“.
Method 3
Make sure your package name is correct in AndroidManifest.xml. Because sometimes, the R file is not generated because of package name in AndroidManifest does not match with the package module that you have. You might need to check XML Files specifically if you have followed the correct syntax for ids. Then clean the project.
Method 4
Step 1: Clean the Project
To clean project: Click on Build > Clean project
Step 2: Rebuild Project
Click on “Build” and click on “Rebuild Project”
Method 5
You may import com.example.your_project.R file in all your activities, do note that this file is not Android.R but your project R file. All you have to do is add this line to your activities import com.example.your_project.R.
Method 6
Try Invalidate caches and restart. Just click on Files from the top left and choose the “Invalidate Caches / Restart” option.
Last Updated :
07 Mar, 2021
Like Article
Save Article
In this article we will talk about how to fix cannot resolve symbol R error in android studio, following are a number of fixes for cannot resolve symbol R error, I don’t know which one will work for you but can assure you that one of the listed fixes will definitely solve your problem.
Have you known a programmer who does not have faced errors, throughout his entire career while developing or writing code? I don’t know such a blessed programmer, if you do know tell us in comments.
What is R.java file:
The Links between xml files and java files are stored in R.java file. Most often “R cannot be resolved” error appears if there is an issue with some of your resource files. The best practice is to undo any recent xml changes or delete the recently added resources from drawable folder. Start again and watch for the culprit. Do Gradle sync before applying any of the fixes, sometime syncing resolve the errors.
If your android studio is slow, learn how to make android studio faster.
1. Build -> Clean Project
If you don’t have any errors in your project java files and xml files and still getting this error, then it is time to clean your project. Click on Build > clean and wait for it, if the issue is resolved congrats otherwise continue reading.
2. File -> Invalidate Cache
In some case cannot resolve symbol R error disappear on invalidating Cache, if the red R is still there then you might need to check your xml layout files specifically if you have followed the correct syntax for ids. For example android:id=”incorrect” and android: id=”@id/correct”.
3. Check project Resource Files, and Layout XML files:
If you reference a resource in an xml file which do not exists or misspell this error will popup. For example if you have a TextView and it’s text value is defined in the strings.xml.
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#E7FEFF" android:gravity="left" android:padding="5dp" android:text="@string/text_value " // if you type textvalue, txt_value or text_valu will result in error. android:textSize="18sp"/>
strings.xml
<resources> <string name="text_value">Doing good so far</string> </resources>
Do check all the resources references in your project xml files and correct them if misspelled, or remove the references to not existing resources.
Note: In most cases the compiler shows you the source of error in the Message. Try to check it first before attempting other solutions
4. Move your images from drawable-v24 to drawable folder:
If you have placed any of the icons or an image in drawable-v24 folder then this is the main cause of cannot resolve symbol R. All you have to do is copy or move your images or icons assets to drawable folder and start building your project. If the error still persists, follow down the fixes below.
5. Do check drawable resources names:
If your error still persists after applying the above fixes then do check the names of resources in drawable folder. Resource names starting with capital letters should be renamed or replaced with small letters.
6. import com.example.yourproject.R;
This error also appear when you don’t put your activities files in the default folder. In this case you have to import com.example.yourproject.R file in all your activities, do note that this file is not Android.R but your project R file. All you have to do is add this line to your activities import com.example.yourproject.R .
7. Change Gradle version:
Open your build.gradle file, look for gradle version and change the version. if you are using 5.3 change it to 5.2 and build your project and check if the error is gone.
If you are using the Gradle wrapper, then your project will have a gradle/wrapper/gradle-wrapper.properties
folder.
This file contains a line like this:
distributionUrl=https://services.gradle.org/distributions/gradle-5.3-all.zip
We hope that your “Error” is resolved by now, If not, do share with us your experience and difficulties resolving “symbol R” error in comments section, we are here looking forward to help you resolve symbol R error in Android . Kindly do share with us if you know a better solution.
If we have helped you in any way, kindly do share this on social media with your friends.
1. Class name
If you do misspell in the class name, the class won’t get imported properly, and the compiler won’t be able to find the class. These names are case sensitive and you must be specifically careful with them, or the class simply won’t get recognized by the compiler.
Another probability is that the class is not available in the current working directory. This happens after a renaming of package, so be sure that all the required imports got renamed, and check for if the CLASSPATH has ‘.’ ‘the current directory’ in it.
2. Variable name
If the issue is in a variable name, then most probably you did not declare your variable before you try to use it. This is also case sensitive, and so check for if you did misspell it.
Another chance is that the variable is written in the wrong scope. Like if you declare the variable in the method A and try to access it from method B. If you need to access the variable in both the methods, you should make it common to both by declaring it in the main method.
class A {
int a=5;
}
class B {
System.out.println(a); //a cannot be resolved
}
}
3. Methods
You might have given the method the wrong parameter datatypes, For example, calling an integer where you need a String value.
Another problem might be when you try to invoke a constructor that is not within the scope.
class A
{
private A(int a)
{
}
}
A a=new A(5); //this won’t work, unless you change the scope ‘private’ to ‘public’
A a=new A(“Hello world!”); //this won’t work, unless you change the Parameter type to String
This should be written like
class A
{
A(String a)
{
}
}
4. Missing resources R
All the files in the resources directory are assigned an integer value in a common class file called R.java, which is auto generated at build time. This acts as a pointer to all the files in the resources folder. The compiler during compilation simply checks for the R.java file and picks the correct resource from the resources directory using this R.java class file.
Once the compiler picks up the wrong value, it eventually misses the resource file and that is when missing R problem is notified. This happens if you accidentally delete the R.java file, or after a package rename if the CLASSPATH miss the ‘.current directory’.
When it comes to Android Studio or IntelliJ, a simple Invalidate and refresh of the IDE’s caches might address the problem.
- You can safely delete the ‘build’ folder that is in the app directory.
- Then File -> Invalidate Caches / Restart
- After the project is reloaded and indexed, do a clean and rebuild from the build menu, so that build folder will be regenerated and the missed Resources error is gone!
It will still show “cannot resolve R” error, only if you have performed refactoring the package name before. In such case, try importing the R by pressing ALT + ENTER simultaneously.