QBoard » Artificial Intelligence & ML » AI and ML - R » cannot resolve symbol R" in Android Studio

cannot resolve symbol R" in Android Studio

  • In every instance in all of my classes where I reference R.id.something, the R is in red and it says "cannot resolve symbol R". Also every time there is R.layout.something it is underlined in red and says "cannot resolve method setContentView(?)". The project always builds fine. It is annoying to see this all the time. I have read many other questions on here about something similar but most involved importing projects from Eclipse. I am using what I believe to be the most recent version of Android Studio and the project was created with Android Studio and worked without any "cannot resolve R" problems. I would like to know what causes this if anyone knows.
      September 24, 2021 1:59 PM IST
    0
  • The error “Cannot resolve symbol R”  in Android studio has faced mostly, when you shift your code to another Computer or send the code to another person. The “R” turns red and can’t run the Application and throw the error in “Logcat”. Most often “R cannot be resolved” error appears if there is an issue with some of your resource files. Due to this error, you are unable to build your application. That’s why we need to solve this error as it not getting away by just doing a simple restart or hitting Alt+Enter.

    Now the point that comes here is how we can fix “cannot resolve symbol R” in Android Studio. So in this article, we are going to discuss six different methods to fix “cannot resolve symbol R” in Android Studio. 

      November 30, 2021 12:28 PM IST
    0
  • If you see this error after moving java files or directories to other locations, then you can guarantee that Android Studio has gotten confused. And guess what? undo-ing those actions doesn't fix the problem.
    So you try a clean, but that doesn't work.
    And restarting doesn't work either.
    But try File -> Invalidate Caches / Restart... -> Invalidate and Restart
    Android Studio maintains information about which files are dependent on which other files. And since moving files around is not implemented correctly, moving files causes errors. And that's not all: caches of these dependencies are used in an attempt to speed up the build.
    This means you not only have to restart, but you need to invalidate those caches to restore (or more accurately, rebuild) sanity.
      December 1, 2021 10:54 AM IST
    0
  • In the latest versions of Android Studio, at least for me, the following works:

    "Tools" -> "Android" -> "Sync Project with Gradle Files"

    In latest Android Studio 3.1.3 (July 2018), "Sync Project with Gradle Files" is available in main menu bar.

    enter image description here

      September 25, 2021 2:28 PM IST
    0
  • I was using gradle 3.3.0. It was the main culprit. Wasted 6.50 hours from my life. Gradle 3.2.1 removed the error.

    classpath 'com.android.tools.build:gradle:3.2.1'
    

     

    Problem resolved after changing the gradle version. Details history can be found here.

     
      September 30, 2021 12:46 PM IST
    0