53

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.

JustWork
  • 1,944
  • 3
  • 22
  • 34
  • 2
    Please check your res/values folder is there any error ? – OMAK May 20 '13 at 04:32
  • Can you post the imports for the Class that is giving you issues? – daniel_c05 May 20 '13 at 04:33
  • @user1911784 No, there is no error there. – JustWork May 20 '13 at 04:33
  • @daniel_c05 This import giving the error: "import com..app.R;" Sorry to hide app name. – JustWork May 20 '13 at 04:37
  • Have you tried unchecking "Build Automatically". Then clean without rebuilding. Then "Build Project". At any point keep checking the "Problems" view (Windows -> Show Views -> Other -> Problems) for anything that is not "R cannot be resolved to a variable" – JRomero May 20 '13 at 04:41
  • Have you updated your ADT with version 22? – Paresh Mayani May 20 '13 at 04:41
  • The R.java is not generated when you have some sort of error in nay of your xml files. Check your xml file for errors, or a "." where it should not supposed to be or anything like that. – Atif Farrukh May 20 '13 at 04:57
  • @J.Romero When I clean without rebuilding, errors disappeared. But then build project, they appeared again. – JustWork May 20 '13 at 05:06
  • @PareshMayani Yes, I updated. Maybe, it occurs the error. I don't know. – JustWork May 20 '13 at 05:06
  • 4
    Hope my G+ post helps you out. Check: https://plus.google.com/u/0/101756163023567589496/posts/TWTi1w63Zdg – Paresh Mayani May 20 '13 at 05:08
  • @JustWork Did you check the Problems view for anything other than "R cannot be resolved to a variable" it would tell you what is actually preventing it from generating the R.java file. – JRomero May 20 '13 at 05:08
  • @J.Romero I have errors in 73 items in there. But none of them different than "R cannot be resolved to a variable". – JustWork May 20 '13 at 05:12
  • I noted that using an old workspace gave me this problem, even after installing the new build tools. When I created a completely new workspace for my Android projects the issue dissapered. – Einar Sundgren Jul 09 '13 at 09:28
  • 12
    SO moderators please don't mark such questions as duplicate. Eclipse, Xcode keep on changing every year and old solutions don't work anymore. Perhaps SO can suggest the latest similar questions so that many people wouldn't have to trawl old questions for hours before stumbling across the latest one. – Bilbo Baggins Jul 31 '13 at 08:07

6 Answers6

122

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

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • 1
    Your suggestion didn't solve my problem. Only changing of target API to 19 in project -> properties -> Adroid -> Project Build Target did provide the solution. – burseaner Sep 06 '14 at 20:49
17

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.

Kerem
  • 2,867
  • 24
  • 35
  • +1 Great words!! Cheers – Paresh Mayani Jul 19 '13 at 08:52
  • This answer provides an important detail. Restart of Eclipse is a must. But restarting of SDK Manager (mentioned in another answer) was not helpful, and was not even necessary in my case. – Stan May 29 '15 at 23:18
9

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.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • How did you get that my res folder is empty? It is not empty and also res/layout is not empty and there are 9 xml files in it. – JustWork May 20 '13 at 04:32
  • Quoted from your own post: "and res folder was empty." – MarsAtomic May 20 '13 at 05:29
  • Care about full sentence: "then I cleaned my project (under Project > Clean...). R.java file was gone and res folder was empty." It was empty after I cleaned. Such things happen. Thank you anyway. – JustWork May 20 '13 at 18:50
  • 1
    The totality of the sentence was less than helpful without an indication somewhere that you recreated the res folder afterwards. Absolute clarity is the path to good answers, but I'm glad the issue is resolved for you. – MarsAtomic May 21 '13 at 02:30
  • Great solution to resolve this eclipse issue – Amit Thaper Oct 28 '19 at 20:34
4

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.

daniel_c05
  • 11,438
  • 17
  • 60
  • 78
  • 7
    I had already tried that but there is no ".R" in Quick fix menu. There is just "android.R" there. – JustWork May 20 '13 at 05:03
4

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

clearwater
  • 514
  • 2
  • 7
1

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.