7

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'
Zoe
  • 27,060
  • 21
  • 118
  • 148
user2469133
  • 1,940
  • 3
  • 21
  • 33

14 Answers14

11

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 !

user2469133
  • 1,940
  • 3
  • 21
  • 33
2
  1. File -> Close project
  2. Open an existing Android Studio Project
  3. Open you project

Hope it will work.

Muhammadjon
  • 1,476
  • 2
  • 14
  • 35
2

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

nsaudria
  • 607
  • 6
  • 9
1

Is any wrong syntax or spelling in your xml?

Check your layout or any xml file.

eltoryn7
  • 68
  • 6
  • Check this option, OP. It's a common issue. – George Nov 18 '18 at 11:45
  • i'm working on a large project , so it's hard to iterate over all layout files to know if there were syntax errors... do you know something that i can do that could determine if i have syntax errors inside the layout filse? – user2469133 Nov 19 '18 at 13:39
  • @user2469133 Try Analyze->Inspect Code, then you can find some errors and warnings in inspection results. – eltoryn7 Nov 19 '18 at 14:12
1

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.

angryITguy
  • 9,332
  • 8
  • 54
  • 82
0

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)

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
  • i tried this also and i did not work , i even changed the project location and removed it from android studio recent projects , then opened it again from the new location , but it seems to sync very fast , as there was something still cached there ! – user2469133 Nov 18 '18 at 11:21
  • also tried what you mentioned in the second part of the answer (commenting support libraries .....) , but with no luck – user2469133 Nov 18 '18 at 11:22
0

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'
Arda Kaplan
  • 1,720
  • 1
  • 15
  • 23
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
MobileEvangelist
  • 2,583
  • 1
  • 25
  • 36
0

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.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Jay Jay
  • 1
  • 1
0

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.

visor24
  • 51
  • 1
  • 6
0

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

0

The only thing that worked for me was,

replacing,

import package_name.R

with

import package_name.*

0

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

rootProject.name = "write the project name here"

devio
  • 607
  • 9
  • 29
0

Close Android Studio Project, Delete .idea .gradle folder, and reload the project after reopen Android Studio

Trinea
  • 649
  • 8
  • 10