Saturday, 24 January 2015

The issue with 'R cannot be resolved to a variable' - Android Application Development

This small but mighty error gave me 4 hours of headache. Missing R.Java file in the ‘gen’ folder, and encountering ‘R cannot be resolved to a variable’ each time I try to run the application.

Every single article I read said everything you already there was to say.
  1. Check if you have import android.R ; and delete it. Next clean the solution and rebuild. If it still is missing, restart eclipse.
  2. Make  sure your SDK manager is up to date and install the latest SDK Build tools.
  3. Check all the XML file names in the res folder and make sure they are all in lowercase convention. Then clean/build and restart eclipse.
  4. Inspect the AndroidManifest.xml and other xml files to ensure there are no errors and the suggestions go on and on and on……...
I tried all these and more, deleted and recreated my whole project severally. Still no positive result. The R.Java file just refused to appear in the package under the ‘gen’ folder.

 In fact, as opposed to others who initially had this file but it later disappeared (hence right clicking the ‘gen’ folder and selecting ‘Restore from Local History’ could resolve their issue due to the fact that the R.Java file might have been deleted), mine NEVER was created at all, even after deleting and re-creating new projects

Now I had ADT version 23 with highest API level of 21 (Android 5.0.1).

On creating the project, I was provided with drop downs “Target SDK” and “Compile With” where from which I was to select an API level. Not selecting the highest API level in both instances made the issue with R.Java file not being created and anywhere R was referenced in the program, it could not be resolved.

So I had to SELECT THE HIGHEST API LEVEL I HAD Installed and voila!!!!! Problem solved. 

For Target SDK, select highest API you have installed

Similarly for Compile With, select highest API you have installed

I hope this gives you one less headache.

No comments:

Post a Comment