Tuesday, November 17, 2009

Android Developer Tip: Regenerating R.java

I've just spent half an hour trying to fathom how to get back my auto-generated R.java that magically disappeared at some point.

The usual ways of resolving issues such as clean build, restarting IDE, and wiping the screen clean with a damp cloth didn't work.

Googling around did reveal a lot of similar complaints and revived a stark deja vu feeling. The hilarious thing is that once the R guy is gone, a good chunk of your code goes red thus adding to the insult.

Rather than continuing the discovery of all the conceivable strains of the issue found in the wild, I decided to leverage the tried-and-true approach of staring at the code. Having done so for a while I spotted a few red guys under res folder. They were a leftover of my current redesign work that I was about to delete before things went awry. Sure enough, as soon as I hit Del on them the darn R thing automagically reappeared.

The moral of the story is there's just so much magic Google can do for us. Bad resources (e.g. layouts with errors) are not treated gracefully by Google Android Eclipse plugin and can deceive it into deleting R.java with no sound reason.

5 comments:

ejr003 said...

Thanks for sharing, just saved me a few hours of frustration! I was trying to rename my package and R disappeared. Reappeared after correcting references to the old package name in the XML layouts.

Borys Burnayev said...

I can't believe I'm commenting on my own post to thank myself for sharing.

A few months in, and I totally forgot how to rectify the darn issue.

Now you can imagine how satisfying it can be to find your own post that hits the nail squarely!

This time it was an error in AndroidManifest.xml.

Live and learn...

Paul said...

Thanks for the hot tip. I'm an android newbie and I didn't have a clue what was going on until I found the telltale red glimmer next to a resource.

Alerion said...

Thanks! Was scratching my head over this one as well!

Andreas Lymalm said...

Yes, thank you very much :D