Android - How to regenerate R class?

Viewed 142063
26 Answers

In another stackoverflow question, somebody answered this : you must get rid of warnings and errors (syntax and links) in your xml files. This plus doing a clean / build project.

Project -> Clean, and choose the option to build the project.

Ok, I fixed it:

When I changed manifest to target 1.5 version, million errors appeared and only one wasn't related to inexistance of R class - in manifest file attributes "targetSdkVersion" and "maxSdkVersion" did not exist in sdk 1.5

Because of this R class was not able to generate.

To generate the R file again:

  1. Modify something in layout->activity_main.xml
  2. Save the file
  3. Project->Clean...

Just delete the R file located in gen directory, and then try Project->Clean. Then build your app again.

You can also right click on the project -> Android Tools -> Fix Project Properties. That should cause the R.java class to be re-generated.

In order to regenerate the R.class, you will have to try multiple techniques.

  • Remove any R imports from your java source files: import R.android
  • Make sure to open the "Problems" panel by going to Window->Show view->Problems
  • If you do have some errors in your xml resources, you will need to correct those errors. In my case I had to change my id attributes to include the "+" sign as follows: android:id="@+id/grp_txt"
  • Now proceed to do Project->Clean
  • Finally do Project->Close, Project->Open

The "+" sign indicates a new resource id and the forces the aapt tool to create a new resource integer in the R.java class.

Try to add a new "Android XML file" to, for example, the /res/layout folder. This might cause the plugin to to regenerate the R class.

You can just modify any xml files in /res folder and even just add a space and save, it will be regenerated.

You can just delete the R.java file from your project and it will regenerate assuming you have build automatically enabled. If you do not have build automatically enabled simply goto project->build all

In my case it was an improperly named XML file that caused my R file not to rebuild. This did not show up in the package explorer, but it was shown in the LogCat. Look out for any warnings there about your res files. No matter how many times you clean your project, the R file will not rebuild until those errors are taken care of.

Related