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"/>
<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.
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
This file contains a line like this:
Thanks for sharing such useful tips and solutions for the most annoying error ” cannot resolve symbol R” in android. I had misspelled a string resource and correcting the spelling resolved my error.
You saved my day, thanks a lot for sharing every possible solution to fix cannot resolve symbol R error.
Hi polite and don’t be tired, thanks a lot for writing such detailed post about fixing “cannot resolve symbol r error in android”. the 3rd solution resolved my error, bookmarked this post for future reference.