'Android Studio “cannot resolve symbol R” but project compiles and runs

Android Studio displays in all java files in red : “cannot resolve symbol R” but the project compiles and runs.

I have tried every solution here: Android Studio "cannot resolve symbol" but project compiles and works, but with no luck.

I tried to :

  • invalidate caches and restart android studio

  • delete .gradle and .idea folders

  • clean and rebuild the project

  • install a new version of android studio

  • mess up with gradle file then undo changes and sync project

One thing that I want to try, but I don't know how to achieve, is to reopen the project from scratch as I was opening it for the first time, but I don't know how to do this.

I also tried answers from here: Android Studio says "cannot resolve symbol" but project compiles, but again with no luck.

Android studio version = 3.2.1

dependency versions in gradle project file:

classpath 'com.android.tools.build:gradle:3.4.0-alpha01'
classpath 'com.google.gms:google-services:4.0.0'


Solution 1:[1]

For some reason that i do not understand ,

this combination of build versions made the issue :

grade version = 4.10.1

classpath 'com.android.tools.build:gradle:3.4.0-alpha01'

when i switched to these build versions :

grade version = 4.6

classpath 'com.android.tools.build:gradle:3.2.1'

The issue was solved !

Solution 2:[2]

  1. File -> Close project
  2. Open an existing Android Studio Project
  3. Open you project

Hope it will work.

Solution 3:[3]

sometimes, R file is not generated because of package name on android manifest is not match with package module that you have.

Solution 4:[4]

Is any wrong syntax or spelling in your xml?

Check your layout or any xml file.

Solution 5:[5]

Android Studio seems to have a caching issue with R.java occasionally. I rarely have a problem with this, but when I do, I actually open R.java (double press shift and type "R.java") or navigate to R.java under "app/build/generated/source" directory, opening the file and checking if the relevant XML id has been created. If it has, it forces Android Studio to now recognise the id's that are not being resolved. It's quick to try, and doesn't require clean and rebuild.

Solution 6:[6]

From your programs menu, open android studio. instead of opening your project from the recent files, select to open a project from your computer and then locate the path to your project. When all else fails, sometimes this works.

Also, try commenting out the support libraries from your gradle implementation, sync your project, and after sync fails comment them back in and sync again. (not sure if that's what you tried already when you said you messed with the gradle file)

Solution 7:[7]

In my case,

I am using - Android Studio version 3.3.1 at home - Android Studio version 3.2.1 at office

When i pulled projects to my office computer that are firstly created at my home computer , Android Studio can not resolved R file but runs application with no error. Because projects gradle and Android Studio version are incompatible.

Only thing that you sholud do, change the gradle version in project level gradle file

  classpath 'com.android.tools.build:gradle:3.3.1'

to

  classpath 'com.android.tools.build:gradle:3.2.1'

Solution 8:[8]

I was facing same issue,

first thought the issue might because of some xml file or naming of drawable resources incorrectly. After analysing, this case wasn't applicable to me. So

Updating Android studio from older version 3.2 to newer version 3.3.2 along with new build tool version from SDK manager resolved this issue for me.

I've tried invalidate cache and Restart AS with clean rebuild all the options but didn't work for me.

Solution 9:[9]

It says cannot resolve symbol, but it can run.

  1. In my case, I just reload the needed *.jar files to the libs folder.

  2. File >> invalidate caches/restart

  3. Rebuild Project

  4. Sync

  5. Run

Works for me!
It looks like the library did not load my "imports" properly the first time.
That's why the import methods cannot be seen in my main_activity.xml.

Solution 10:[10]

This happened to me when I was going back and forth between different SDK versions.

Apparently SDK manager copied all the source files but it didn't finish cleanly. As a result I could compile and run my project just fine but IDE didn't recognize the SDK and reported all java symbols unresolved.

None of the above and other solutions in SO didn't work for me, but just uninstalling/reinstalling the specific SDK version did a job.

Solution 11:[11]

If you renamed your package (inside java folder), make sure you change your Manifest package name to the same

Solution 12:[12]

The only thing that worked for me was,

replacing,

import package_name.R

with

import package_name.*

Solution 13:[13]

simply go to project settings : settings.gradel and change the rootProject.name to your current name

rootProject.name = "write the project name here"