Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse error: R cannot be resolved to a variable [duplicate]

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.

like image 414
JustWork Avatar asked May 20 '13 04:05

JustWork


4 Answers

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

like image 121
Paresh Mayani Avatar answered Nov 19 '22 09:11

Paresh Mayani


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.

like image 24
Kerem Avatar answered Nov 19 '22 07:11

Kerem


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.

like image 9
MarsAtomic Avatar answered Nov 19 '22 08:11

MarsAtomic


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.

like image 4
daniel_c05 Avatar answered Nov 19 '22 07:11

daniel_c05