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.

14
  • 2
    Please check your res/values folder is there any error ?
    – OMAK
    May 20, 2013 at 4:32
  • Can you post the imports for the Class that is giving you issues?
    – daniel_c05
    May 20, 2013 at 4:33
  • @user1911784 No, there is no error there.
    – JustWork
    May 20, 2013 at 4:33
  • 4
    Hope my G+ post helps you out. Check: plus.google.com/u/0/101756163023567589496/posts/TWTi1w63Zdg May 20, 2013 at 5:08
  • 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. Jul 31, 2013 at 8:07

6 Answers 6

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

1
  • 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 6, 2014 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.

2
  • +1 Great words!! Cheers Jul 19, 2013 at 8: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, 2015 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.

5
  • 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, 2013 at 4:32
  • Quoted from your own post: "and res folder was empty."
    – MarsAtomic
    May 20, 2013 at 5: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, 2013 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, 2013 at 2:30
  • Great solution to resolve this eclipse issue Oct 28, 2019 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.

1
  • 7
    I had already tried that but there is no "<my_package_name>.R" in Quick fix menu. There is just "android.R" there.
    – JustWork
    May 20, 2013 at 5:03
4

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

0
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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.