Breaking

Wednesday 6 February 2019

How would I solve "cannot resolve symbol 'R' in android studio?

How would I solve "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.



Answers 1 :-


I had this this issue too. A simple 'gradlew clean' and 'gradlew build' did the trick.

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

Answers 2 :-


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.



Answers 3 :-

          I have similar problem and here is what I did:
         clean project and Sync Project with Gradle ,

    Check buildTools version in my sdk 



From build gradle (module ) change minSdkVersion from 8 to 9
defaultConfig {
        applicationId "PackageName"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
But all this didn't help.
Finally I found the answer (this worked in my case)


Change from build.gradle(module:app)
android {
    compileSdkVersion 21
    buildToolsVersion '21.0.1'
......
....
}
Or
select File | Project Structure change Build Tools Version to 21.1.1


Answers 4 :-


  1. Check your xml files.
  2. Clean Project.
This is it.
For example I had an entry in my strings.xml:
<string name="A">Some text</string>
And in activity_main.xml I used this string entry
<TextView
android:id="@+id/textViewA"
android:text="@string/A"/>

While'd been working with project, I deleted that
 <string name="A">Some text</string>
And consequently activity_main.xml became not valid. As the result: cannot resolve R.
So, check your *.xml files and clean project.

No comments:

Post a Comment