R is unresolved


#1

I create a new project in Android Studio and when I create a new activity for my project the IDE display me an error says this:

unresolved Reference: R

This error appear in both files in onCreate() method

setContentView(R.layout.activity_main)

I tried build, clean and rebuild the project and nothing resolve this issue.

Someone have any idea how to solve this?

Thanks.


#2

Did you import R? The IDE can help import it. Just invoke one of the quick fix intentions, then import the R from your project.


#3

Hi @fatjoe79, my project import this:

import code.diegohdez.github_api.R

where R is marked with red where says: Unresolved reference: R

setContentView(R.layout.activity_get_token)

also display the same error: Unresolved reference: R

I don’t know what happen.


#4

I assume this worked before, right?

The root cause for this problem is often an error in one of your resource files (layout resource, etc). Check the output of the gradle build.


#5

Click on Build->Clean Project and that will perform a gradle clean


#6

R is a static class R. it created when you create resource and build it. It have all resource ID as Static for you application access all resource in your activity. This error mean you unsuccessfully build resource , please check all your resource have something Error, so R class static not created successful.


#7

If you have a warning in your project please fix it.
I tried almost everything on web, but only worked when I fix the gradle’s warn, in my project


#8

Solution:

  1. Grade Sync.
  2. Invalidate caches and restart.
  3. Clean the project and rebuild.
  4. Alt + Enter import R files.

:slightly_smiling_face:


#9

I’m also facing this issue.
Gradle Sync or Invalidate caches doesn’t work at all and the import is not available for autocomplete.
If I type the package, even if the R is red, the project compiles and runs.
This is just boring for now.
This error for me started when I installed Android Studio 3.2 and Kotlin 1.71.
Good tips are welcome.


#10

Same as me, so annoying, i tried everything but it is not generating


#11

On my case, it might be related with not very well supported libraries I have on my project and that are not necessarily updated, also don’t have Kotlin support. Anyway, when I checkout commits previous to this change, everything compiles properly and class R is generated.
But this libraries are absolutely essential to my project and the work the creators of this libraries have been awesome, Hope this can help and give some clues to solve this issue.


#12

I was having the same problem recently, and this turned out to be an issue with the latest Android Gradle plugin alpha (3.3.0-alpha-13).
Switching back to the previous version solved the problem.
Hope this helps.


#13

Have had the same issue too, had to use a stable gradle plugin for R to be resolved.


#14

This solved my issue.
Thanks!
After checking my git commits I noticed that when I accepted the suggestion to update Kotlin to version 1.71 I also accepted the update for Android Gradle plugin 3.3.0-alpha-13.
I switched to 3.2.1 and the error was there anyway.
Like I mentioned in a previous post, the error comes from a library that uses the same library that I use, but the version on my project was a lower version.
After update the dependency version, the error has gone.
The difference from the version 3.3.0-alpha-13 to 3.2.1, is that the first one (for some reason) was not showing the error in the Logcat and the second yes.
This change made the fix possible.
So the error was unrelated with Kotlin on my case.


#15

Wouldn’t Android Studio auto-import these?


#16

It depends on how you use the editor. But yeah, importing was not the problem in this case.