12

I was having a project open in Android studio. It was generated by the wizard and working fine.

I did some small changes to activity_main.xml and when I changed back to MainActivity.java i get the error in several places that it cannot resolve R. I might have done something to cause this. But I´m not sure what since it appered when I edited the xml.

Does anyone know what might be the solution to this? I can find the R.java in r/release/packege/com/ and it looks fine.

Einar Sundgren
  • 4,325
  • 9
  • 40
  • 59
  • 1
    have you tried cleaning your project? – Anila Jun 14 '13 at 15:03
  • 4
    This can happen due to errors within XML files. Have a look over the things you've changed, just in case. Sometimes, however, it's just a case of doing a 'clean' also. I've had both instances myself. – laminatefish Jun 14 '13 at 15:04
  • Thanks. I did both. I had a small error in my xml and I also rebuilt the project. Now it works. Strange thing is that the ide still warns that something is wrong. – Einar Sundgren Jun 14 '13 at 15:11

15 Answers15

35

Most of the time it is due to a bad XML file. XML files can be layout files, value files, or the Manifest file. Please check your xml files and try to rebuild the project. Sometimes cleaning the project and rebuilding it also works.

tjpaul
  • 343
  • 3
  • 16
Shaleen
  • 829
  • 7
  • 17
  • Also make sure no folders got duplicated. Especially in the `/res` directory. – Wenger Jun 14 '13 at 15:38
  • 1
    I'm not completely certain this was the reason for the problem at the time of my question. But since this in my experience is the most common reason for both AS and eclipse having trouble resolving R I consider this to be the proper answer. – Einar Sundgren Nov 24 '13 at 21:16
  • This has happened to me a lot. Usually it is some non-escaped character in my comments of the XML. I wish Android Studio would point the problem out rather than requiring you comb through all your previous XML changes. – codingjeremy Oct 02 '15 at 21:54
  • In `AndroidManifiest.xml` some string dose not exists – shareef Nov 29 '15 at 20:40
  • Also, and this should go without saying but I have made this mistake, pay attention to the error message. When it's a problem in one of your XML files the editor will not take you to the problem - it won't even take you to the XML file at all. But it may give you the name of the XML entry that's causing the problem in the error message. – jwehrle May 27 '16 at 19:57
  • NOTE: Sometimes it is not enough to clean / rebuild etc., what worked for me is closing AndroidStudio and reopening it. – Mercury Dec 14 '16 at 00:06
6

In addition, make sure you do not have a drawable with an invalid name. I had a drawable with a numeric filename and that didn't sit well with Android so it failed to compile R.java.

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
5

Downgrade Your Gradle Plugin Version

No amount of cleaning, rebuilding and restarting would do the trick for me.

The only thing that did the trick was downgrade our Gradle version from 3.4.0-alpha02 to 3.2.1.

So, instead of:

dependencies {
    classpath 'com.android.tools.build:gradle:3.4.0-alpha02'
}

We used:

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
}

After making that change and then doing a Gradle sync, everything worked.

Not sure if it's related to the alpha release or to that version or if changing your Gradle version at all just forces a refresh that otherwise cannot be done with the other commands, but there ya go.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
  • Downgrading is probably a very good suggestion for those that use any alpha versions in the toolchain. Just downgrade to a production vesion. Since this question was asked in 2014 with the entire Android studio in alpha, I should probably have done that as well. – Einar Sundgren Nov 07 '18 at 12:10
  • 1
    @EinarSundgren Agreed. I typically avoid alpha releases, especially for production releases, but this was set by another developer. Still, I'm surprised this ended up being the culprit. Of course, a nice error message indicating what the issue was would have been nice. – Joshua Pinter Nov 07 '18 at 18:50
3

Make sure in your AndroidManifest.xml the package name is correct. That fixed the problem for me when my R.whatever was marked red!

craned
  • 2,991
  • 2
  • 34
  • 38
2

Try to clean the project by doing the following:

If you are using Windows, open CMD, cd to your project directory and run gradlew clean.

Jawa
  • 2,336
  • 6
  • 34
  • 39
  • 1
    That is probably a good suggestion. The original problem ocurred in a pre gradle project so that would not have been the solution at that moment. It seemed there was some error with the current Eclipse workspace, probably due to an Aptana installation. When I moved the project to another workspace the problem disappered. – Einar Sundgren Aug 30 '13 at 09:24
  • You can also do via GUI by `Build > Clean Project` or `Build > Rebuild Project`. Also try cleaning by commenting out the error prone line. Once it's successfully cleaned, un-comment it. – Nuhman Apr 20 '19 at 16:16
1

I just had this problem and the reason was that after editing the layout android studio automatically added an import to the source files:

import android.R

Of course the main R class has not all the symbols in your layouts, which made everything a red mess. Removing that line solved for me.

EDIT: Seems like eclipse had the same problem early on.

Community
  • 1
  • 1
1

I had that same problem crop up, too (most recently while I was at lunch--came back and WTF?). And since this was a simple Hello, World program, there wasn't many places these errors could hide. The XML files looked fine--I hadn't even touched the manifest. A clean did nothing.

So I commented out a section of my layout.xml to try to narrow down the problem. Sure enough, the errors went away.

And then I simply UNcommented out exactly what I had commented...and everything was hunky-dory. Strange! Perhaps there was some lingering issues that needed to be touched in order to cause a rebuild of the layout file. You'd think basic stuff like that wouldn't happen these days, but they do.

One more thing! It's possible that something may happen to Java as well. It's very hard to know the status of the Java run-time environment; just easier to start again. So here's one more thing to try: restart your computer, restart Android Studio, AND THEN Invalidate Caches and Restart. Yep, the whole-hog.

Termininja
  • 6,620
  • 12
  • 48
  • 49
SMBiggs
  • 11,034
  • 6
  • 68
  • 83
  • 2
    As an addendum, I remember the old days of programming, when if you accidentally hit the control key while typing, you could easily insert a hidden control character into your code. While you couldn't see these characters, the compiler sure could! Many programmers in the 80s spent long hours hunting down these bugs. Perhaps something like this pops up occasionally in Eclipse? Any comments? – SMBiggs Jul 20 '14 at 16:30
1

I sometimes get this after updating Android Studio. Not always straight away but after a while (perhaps after a restart of the program).

Here is what has worked for me: Tools - Android - Sync project with Gradle files.

For some reason just clicking the "Sync now" link that shows up after changing the build.gradle file has not done any change for me. Even though the messages says approximately the same thing.

Christina
  • 163
  • 2
  • 12
1

I had the same problem because in my strings.xml I forgot to escape a apostrophe. Use " \' " never " ' " in xml file!

Jeremie D
  • 4,145
  • 1
  • 35
  • 41
0

When all else fails restarting android sometimes helps. During the start files are loaded afresh and this enables detection of the R

HagTheDon
  • 141
  • 1
  • 6
0

This problem occurs when you rename the package name. After renaming the package name, you must change the older package name to a newer one. For me it solved the problem once I changed that. After that, you might get errors in xml files, that you have to change the package name there also. That's it.

Amirreza
  • 1
  • 1
0

I just had the R problem and after a few hours found that the problem was my regional spelling as used in one of my xml files. The problem word was behaviour, which is spelled behavior by Americans. I am Australian and we are taught ye olde English way of spelling certain words and so I looked past it a number of times before the penny dropped. Its a small thing, but it can easily sneak up and bite you on the nether regions and is hard to spot and can waste a lot of time.

Rowie
  • 1
  • 1
0

In my case I had just added some new image files to the drawable folder and accidentally put a GIF in there with my PNGs. I had not incorporated them into the project at all yet. I removed the GIF and then did a clean and rebuild. That fixed it for me.

Clif
  • 1
0

I had this exact problem when importing a Udacity Example Program. The Message Pane in Android Studio said the error was failing to load a .PNG in a sub-directory deep in the Project Files.

Turns out the PNG with all of the sub-directory path names was greater than the name length limit defined by Windows 7 (260 characters). I gave the Project a shorter name - to free up some space - and then Android Studio happily compiled.

0

After you tried make project, clean project, rebuild project but still doesn't work. Try restart Android studio

Aldy
  • 2,743
  • 2
  • 12
  • 17