Android SDK: Error in an XML file: aborting build

I just started Android programming using the Android Development Tools (ADT) plugin for the Eclipse IDE, and while experimenting with layout weights, I came across a weird error after running the program.
The console dump showed this :

2011-09-25 14:21:10 - HelloLinear] Error in an XML file: aborting build.
[2011-09-25 14:21:11 - HelloLinear] res\layout\main.xml:0: error: Resource entry main is already defined.
[2011-09-25 14:21:11 - HelloLinear] res\layout\main.out.xml:0: Originally defined here.
[2011-09-25 14:21:11 - HelloLinear] C:\Users\Mukul\workspace\HelloLinear\res\layout\main.out.xml:1: error: Error parsing XML: no element found

Also, I noticed that there was a new XML file called main.out.xml created in the res\layout\ folder.
The reason you might get this error is that when you run the program you might have the main.XML file open as the active file. Eclipse (presumably) tries to run the XML file as the android application rather than the class file.  The solution to this is pretty simple

1. Delete the main.out.xml file (generated during runtime). You’ll find it in the res\layout folder
2. After you are done editing the main.XML file, save it and switch the view to the java file which is accessing the main.xml that you’ve edited
3. Run the program and voila! – the error disappears

Update: Here’s a fix that does not require you to switch views every time: Eclipse -> Window -> Preferences -> Run/Debug -> Launching -> Launch Operation -> ‘Always Launch the previously launched application’. You still need to delete the xml out files that were created and make sure you have  run the correct configuration at least once. Credit for this goes to Gnac of Stack Overflow

3 Responses to “Android SDK: Error in an XML file: aborting build”