cannot resolve symbol R, cannot resolve symbol r in android, android solve error, unresolved reference r, android studio

Solved cannot resolve symbol R in Android Studio

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

Solved cannot resolve symbol R in Android Studio

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

Solved cannot resolve symbol R in Android Studio

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

Solved cannot resolve symbol R in Android Studio

  • For the manifest file is as follows

Solved cannot resolve symbol R in Android Studio

  • For the build.gradle file for the app is as follows

Solved cannot resolve symbol R in Android Studio

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

Solved cannot resolve symbol R in Android Studio

To rebuild the project,

  • in android studio, click build and then build project

Solved cannot resolve symbol R in Android Studio

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

Solved cannot resolve symbol R in Android Studio

      (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

Solved cannot resolve symbol R in Android Studio

 

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