Eclipse error: R cannot be resolved to a variable

AndroidEclipse

Android Problem Overview


I am getting this classic error in Eclipse IDE. I am bored of Eclipse's bugs. They driving me mad.

I almost tried everything which suggested as solution (by Googling). None of them worked.

My project was working normally but not code changes.

I see that my code changes does not acting on my app (on AVD) then I cleaned my project (under Project > Clean...). R.java file was gone and res folder was empty.

Now I am getting that error: R cannot be resolved to a variable.

What should I do?

And also I tried to delete project and create again with Android Project from Existing Code I've backed-up before. It already gives same error after cleaning.

Any ideas would be appreciated.

Android Solutions


Solution 1 - Android

I assume you have updated ADT with version 22 and R.java file is not getting generated.

If this is the case, then here is the solution:

Hope you know Android studio has gradle building tool. Same as in eclipse they have given new component in the Tools folder called Android SDK Build-tools that needs to be installed. Open the Android SDK Manager, select the newly added build tools, install it, restart the SDK Manager after the update.

enter image description here

Solution 2 - Android

I'm not posting this as an answer but a confirmation to Paresh's accepted answer. I recently updated SDK tools to Revision 22 and I noticed my code changes was not being affective on the device i'm testing at all. Such as the url I was using, I was getting errors for connection time out regarding the url I was "previously" using. Therefore I cleaned the project and built again only to find out that autogenerated R.java file is missing.

After reading Paresh's answer and checking what's going on with my sdk manager this is what I saw: enter image description here

SDK Build-tools 17 was not installed and there was already a new update to SDK tools even though it does not mention any change related to this problem in the changelog, this update brought back my R.java file and the related problems were gone after an eclipse restart and final clean/rebuild on the project.

Solution 3 - Android

The R file can't be generated if your layout contains errors. If your res folder is empty, then it's safe to assume that there's no res/layout folder with any layouts in it, but your activity is probably calling setContentView and not finding anything -- that qualifies as a problem with your layout.

Solution 4 - Android

Try to delete the import line import com.your.package.name.app.R, then, any resource calls such as mView= (View) mView.findViewById(R.id.resource_name); will highlight the 'R' with an error, a 'Quick fix' will prompt you to import R, and there will be at least two options:

  • android.R
  • your.package.name.R

Select the R corresponding to your package name, and you should be good to go. Hope that helps.

Solution 5 - Android

In addition to install the build tools and restart the update manager I also had to restart Eclipse to make this work.

Solution 6 - Android

I had a fully working project to which I was doing a minor change when this occured after updateting the SDK. Eclipse updated the SDK and the ADT but I could still not build the project. Exlipse said there were no further updates available.

The problem persisted until I manually uninstalled the ADT from eclipse and re-installed it. Only then would my project build. I had restarted eclipse inbetween each step.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustWorkView Question on Stackoverflow
Solution 1 - AndroidParesh MayaniView Answer on Stackoverflow
Solution 2 - AndroidKeremView Answer on Stackoverflow
Solution 3 - AndroidMarsAtomicView Answer on Stackoverflow
Solution 4 - Androiddaniel_c05View Answer on Stackoverflow
Solution 5 - AndroidclearwaterView Answer on Stackoverflow
Solution 6 - Androiduser2497389View Answer on Stackoverflow