Android studio suddenly cannot resolve symbol R

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.

  • Synchronise ScrollView scroll positions - android
  • Android Studio and Bitbucket plugin
  • Failed to convert @drawable/ into a drawable
  • Jelly Bean DatePickerDialog — is there a way to cancel?
  • Moving Floating Action Button up and down to avoid getting blocked by a snackbar
  • What's the home key for the Android emulator on Mac OS?
  • 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.

    Related posts:

    Android - default button style
    Show soft keyboard even though a hardware keyboard is connected
    Changing the size of the seekbar programmatically but cant get the thumb to be larger than the bar
    Warning about <uses-sdk> in Manifest?
    Ant build fails inside Eclipse
    Android Nested Scrollview not scrolling
  • Show TimePicker with minutes intervals in android
  • Android: Matrix -> what is the different between preconcat and postconcat?
  • how can we use startActivityforResult() for Email intent?
  • I can’t find the Android keytool
  • front camera take very dark capture in android
  • Installing Genymotion Eclipse plug-in
  • 13 Solutions collect form web for “Android studio suddenly cannot resolve symbol R”

    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.

    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.

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

    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.

    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.

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

    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.

    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.

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

    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.

    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.

    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.

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.