Tuesday, May 8, 2018

Android Studio and Cannot Resolve Symbol 'R'

It is one of the error message that appears as Android Studio is used more frequently is,

Cannot resolve symbol 'R'


In the source code, it will show all usage of R as red in colour.


From what I know, here are two situations that cause this error to appear.
  1. Copy of a source file into the project. This include copying of Modules from another existing project.
  2. During one of the build, there was a problem

 Solution

 This is how I did it on Android Studio 3.1.2.

Step 1: Rebuild the project.

Open the Java files that was just copied or the problematic files. Remove any reference of import to the 'R'. In the manifest/AndroidManifest.xml, correct the package name if it is not the same as the current project.

On the Menu bar click Build ->Clean Project.



Step 2: Sync files in the project.

On the Menu bar click Files ->Sync Project with Gradle Files. Close and restart Android Studio.


This will solve the error on most cases. However, if there are still any errors, gradle will not be able to update with the rectified pointer to R references. Read the list of errors as it will indicate the problem areas that you can easily rectify. Once you have fixed the error, or in my case most of the time I just delete them, just repeat Step 1.

Works all the time.

No comments:

Blog Archive