82

Possible Duplicate:
R cannot be resolved - Android error

I am having a issue which I have had before and I don't understand the cause. My project and code is fine as it was running yesterday. I have open Eclipse today and the whole project looks like it contains errors.

I believe it is some sort of build path issue. I don't understand why it randomly occurs? But in the code where there is R.Layout.LayoutName there is an issue... can anyone help?

I have tried cleaning and building it, I have tried importing android.R but I didn't have this before, it then gives and error and says the layout file doesn't exist.

Community
  • 1
  • 1
Beginner
  • 25,463
  • 62
  • 148
  • 228
  • I have got two projects in eclipse, one is running fine and the other one isnt? – Beginner Feb 08 '11 at 11:09
  • 1
    Kindly check the following link http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error – chiranjib Feb 08 '11 at 11:14
  • 1
    this happens to me whenever I clean my project, it can stop me for days. there seems to be a dozens of different answers to this question involving changing settings deep in the project, which usually don't work and can cause other issues as well. – Androidcoder Sep 24 '13 at 14:30

8 Answers8

163

I think I found another solution to this question.

Go to Project > Properties > Java Build Path > tab [Order and Export] > Tick Android Version Checkbox enter image description here Then if your workspace does not build automatically…

Properties again > Build Project enter image description here

Emkey
  • 5,106
  • 8
  • 35
  • 55
  • 13
    This guy needs some votes. After half an hour of going through StackOverflow, this is the answer that worked for me and will probably work for many other people. Eclipse probably unchecks these automatically. – Andrew Latham Dec 30 '11 at 17:18
  • Totally agree with @AndrewLatham – rdp Mar 27 '12 at 10:27
  • 2
    Seriousy, this answer should be the accepted one. Either that, or make this a community wiki. – l46kok Nov 23 '12 at 13:20
  • Great, you saved my day! Thank you! – Dr.Luiji Dec 14 '12 at 12:27
  • Wow, this also worked for me. Eclipse and adt seem to conspire often to make the most subtle and absurd of errors. I don't know how reason alone could solve this one - thanks for your tip! – Anton I. Sipos Jan 08 '13 at 00:40
  • 2
    I agree with @AntonI.Sipos. I have no idea why this a) happens or b) fixes the issue but it does. Ensure you tick the target SDK verion AND the Android Dependencies. Previously I have removed and reimported the source into the workspace to fix the issue. It seems a 'clean' will trigger it. – Gusdor Jan 12 '13 at 22:11
  • After checking version go to Project > Clean or Project > Build – Joel Christophel Mar 14 '13 at 13:44
  • This is the correct answer – Alejandro del Río May 31 '13 at 16:27
  • thanks, this has worked nicely. – D_K Jul 14 '13 at 18:05
  • I tried this too, but the problem persists. There seems to be more factors to this problem I'm afraid. Really nice answer though, with pictures and all! – Roger Dec 08 '13 at 19:54
  • My android was unchecked. But, this did not solve the problem for me. Even after removing duplicate tags in my gui xmls and string xmls, the problem persists. – Trojan.ZBOT Jan 10 '14 at 07:14
  • This worked, but only after installing the Build Tools (Window/Android SDK Manager) – mpemburn Jan 21 '14 at 22:10
  • Does not work for me. "Build Project" is grayed out. – Brian Knoblauch Jul 14 '14 at 15:34
  • my layout's graphical view is visible now...so i upvoted, but its still showing that "R cannot be resolved to a variable android" i.e. my java file is not generating. – Sunishtha Singh May 07 '15 at 18:57
75

Are you targeting the android.R or the one in your own project?

Are you sure your own R.java file is generated? Mistakes in your xml views could cause the R.java not to be generated. Go through your view files and make sure all the xml is right!

codinguser
  • 5,474
  • 2
  • 31
  • 39
  • 2
    How do i check if the r.java class is being created? – Beginner Feb 08 '11 at 11:18
  • Look if your_package.R.java exists in your project after building. – codinguser Feb 08 '11 at 11:18
  • Also i believe the xmls are correct as the application has been working fine for weeks and now all of a sudden it is not – Beginner Feb 08 '11 at 11:19
  • No It is not there...why is this? – Beginner Feb 08 '11 at 11:23
  • Are you sure nothing at all has changed in the project? – codinguser Feb 08 '11 at 11:26
  • not at all, i was working on it yesterday and it was running, came back today and its not – Beginner Feb 08 '11 at 11:28
  • 3
    hmmm that is curious. Anyway, when I had such a problem once, I later realized my problem was in the xml files. Not necessarily a syntactic error which eclipse would detect, but a semantic one. Well, YMMV. – codinguser Feb 08 '11 at 11:43
  • 69
    I'm not sure why this is the selected answer - since it does not give a solution. – EasyCoder Apr 29 '12 at 15:05
  • 5
    @EasyCoder Mistakes in your files can prevent R.java from being generated. In that case, R will not resolve because it does not exist. – codinguser Apr 29 '12 at 16:50
  • 2
    normally your answer is right. if something wrong with xml, R is not generated. but in this case, the problem is yesterday was working, but today not working with all projects. all projects make error is not kinda xml problem i think – beginners Jul 24 '13 at 00:20
  • How to find WHERE in one of my xml files the error is? In Eclipse it is impossible! – Yar Apr 18 '14 at 11:31
  • In my case the launch icon was not generated. After copy/paste default icon it was fixed. – Eugen Sep 25 '17 at 16:39
21

I've fixed the problem in my case very easy:
go to Build- Path->Configure Build Path->Order and Export and ensure that <project name>/gen folder is above <project name>/src
After fixing the order the error disappears.

Eduard
  • 1,396
  • 14
  • 19
12

You want Clean Project Like this

click on

Projects>Clean>select your project

this will help to u

Parag Chauhan
  • 33,908
  • 13
  • 83
  • 95
12

check your R directory ...sometimes if a file name is not all lower case and has special characters you can get this error. Im using eclipse and it only accepts file names a-z0-9_.

j2emanue
  • 51,417
  • 46
  • 239
  • 380
2

Fought the same problem for about an hour. I finally realized that I was referencing some image files in an xml file that I did not yet have in my R.drawable folder. As soon as I copied the files into the folder, the problem went away. You need to make sure you have all the necessary files present.

Nagendra Rao
  • 6,406
  • 5
  • 47
  • 87
glenn
  • 21
  • 1
  • This worked for me. It's unobvious from the long list of errors that some of them are actually causing all the others. – Carlos Aug 08 '14 at 08:24
2

Agree it is probably due to a problem in resources that is preventing build of R.Java in gen. In my case a cut n paste had given a duplicate app name in string. Sort the fault, delete gen directory and clean.

user1164035
  • 1,081
  • 2
  • 7
  • 5
0

Save all layouts, and replace them from the previous version. Start to copy one by one and clean the project everytime, sometimes one of the layouts has something, that eclipse doesn't like. It worked for me.

jonaypelluz
  • 368
  • 4
  • 13