R is unresolved

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.

3 Likes

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.

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.

2 Likes

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.

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

1 Like

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.

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

Solution:

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

:slightly_smiling_face:

4 Likes

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.

2 Likes

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

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.

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.

3 Likes

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

1 Like

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.

1 Like

Wouldn’t Android Studio auto-import these?

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

I got same error: R unresolved, when trying to link two activities.
I followed the Warnings and suggestions by IDE red marking in .xml file AND resolved the error.

actual error was in the fontFamily . I Changed the fontFamily and debug the app (shift+F9) , ERROR resolved

Hi everyone.
I am a noob, just beginning to learn programming languages. So, read with caution.
I faced the same problem after running the project successfully many times. After all I discovered it was just a typo in one xml file. I put some number into a tag, without noticing. After remove it, the R problem disappeared.
As “nemo” said above, R is a static class that works using the xml code. If xml is wrong, R will not be correctly built.

Hello,
I also had same problem and resolved it by followup my layout resources in code view. I had drag a button and use ‘&’ sign in button’s name. I’d just replace ‘&’ to ‘and’. This ```
unresolved Reference: R , usually come from layout. You should see your layout resource in code view.

I also had to switch Android gradle plugin back to 3.2.1 from stable 3.3.0, using Kotlin version 1.3.0 here

3 Likes