312

I've found out that my R.java is never updated, so it doesn't contain information about my new resources, so I decided to delete it and thought that Eclipse would generate a new one. But that didn't happen, and I don't have R.java now. How can I regenerate one?

I'm using Windows 7.

From one of the comments: "Doing Project -> Clean is what caused the problem for me. Cleaning deletes R.java...and for whatever reason the plugin is not regenerating the file."

iankit
  • 8,806
  • 10
  • 50
  • 56
Aleksejs Popovs
  • 850
  • 2
  • 12
  • 18
  • my solution when cleaning didn't work: i needed to download Build Tools in SDK manager and then restart the system. I have ADT 22.3.0. – Moradnejad Dec 17 '15 at 14:52

64 Answers64

301

I found this happening to me with a broken layout and everything blows up. Relax, it's like that old mistake when you first learned programming C where you forget one semicolon and it generates a hundred errors. Many panic, press all the buttons, and makes things worse.

Solution

  • Make sure that anything the R. links to is not broken. Fix all errors in your XML files. If anything in the ADKs are broken, R will not regenerate.
  • If you somehow hit something and created import android.R in your activity, remove it.
  • Run Project -> Clean. This will delete and regenerate R and BuildConfig.
  • Make sure Project -> Build Automatically is ticked. If not, build it manually via Menu -> Project -> Build Project .
  • Wait a few seconds for the errors to disappear.
  • If it doesn't work, delete everything inside the /gen/ folder
  • If it still doesn't work, try right-clicking your project -> Android Tools -> Fix Project Properties.
  • Check your *.properties files (in the root folder of your app folder) and make sure that the links in there are not broken.
  • Right-click your project > properties > Android. Look at the Project Build Target and Library sections on the right side of the page. Your Build Target should match the target in your AndroidManifest.xml. So if it's set to target 17 in AndroidManifest, make sure that the Target Name is Android 4.2. If your Library has an X under the reference, remove and re-add the library until there's a green tick. This might happen if you've moved a few files and folders around.

What to do if R doesn't regenerate

This usually happens when you have a broken XML file.

  • Check errors inside your XML files, mainly within the /res/ folder
  • Common places are /layout/ and /values/, especially if you've changed one of them recently
  • Check AndroidManifest.xml. I find that often I change a string and forget to change the string name from AndroidManifest.xml.
  • Check that Android SDK Build-tools is installed. Window -> Android SDK Manager -> Tools -> Android SDK Build-tools
  • Make sure when you update the Android SDK Tools, you also update the Android SDK Platform-tools and Android ADK Build-tools. Build fails silently if they don't match.
  • If you can't find the issue, right click /gen/ -> Restore from local history... -> tick R.java -> click Restore. Even if it doesn't solve the problem, it will clear out the extra errors to make the problem easier to find.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Muz
  • 5,866
  • 3
  • 47
  • 65
  • 21
    this should be the top answer! – john.k.doe Jun 06 '13 at 19:39
  • 6
    Indeed, it made me realize my environment still needed an update to the tools. – woodleg.as Jul 04 '13 at 14:47
  • 11
    Window->Android SDK Manager->Tools->Android SDK Build-tools this is the solution for me, upvoted –  Jul 08 '13 at 14:40
  • Unbelievable that a **Android Tools -> Fix Project Properties** option even exists. What a FPORS. +1 on the answer. – Nate Aug 06 '13 at 08:06
  • This worked for me. Even though I wasn't using the menu/mail.xml it had an error and prevented R.java from being generated. I removed it and everything started working. – GothamNite Sep 20 '13 at 10:35
  • After trying those solutions, remember restarting eclipse. – Angelo Tricarico Jan 03 '14 at 14:19
  • 1
    I read dozens of answers and only this one mentioned checking for SDK Build-tools. I blindly let eclipse download and install all my Android tooling when it offerred to do so. It completely missed that part. – Justin Aquadro Jan 11 '14 at 08:26
  • yip this is the best answer. I somehow managed to import android.R it drove me up the wall. You just saved what little hair I had left. – Namphibian Jan 21 '14 at 08:09
  • Not the kind of problem I like to face alone at 2:40AM. I didn't hace those build tools. You covered everything, uppppp-vote. – Darko Maksimovic Feb 06 '14 at 01:40
  • Thank you! I needed to install more Android SDK tools. Now upon re-building the R.java file is generated. – JoJo Feb 25 '14 at 15:28
  • they key to this answer is that you cannot have ANY errors in your XML files... I went through this problem when I refactored a bunch of asset names, and i had one button i was still waiting on my designer for. i temporarily replaced the asset so the XML files would compile, then R got built, and my code errors came down drastically, and i was able to concentrate on what was still actually missing... – jesses.co.tt Jun 14 '14 at 00:08
  • 1
    Check console, may give you detailed error that need to be fixed, and then you can build. This helped in my case – david72 Dec 12 '14 at 00:26
  • 1
    Great list. I might add that the problem in my case was incorrectly named R.raw assets (eg. "1.ogg"). This didn't show up in Project Explorer (with red crosses), LogCat or Console, but it was listed in the Problems tab. – grooble Jan 17 '15 at 02:39
  • On OS X Snow Leopard revisions after 22 some built tool binaries such as aapt throw a segmentation fault – GregD Apr 30 '16 at 17:41
  • I had error in my manifest file -> @drawable/ic_launcher was not generated after project creation. After copy/paste default launcher icon it was fixed. – Eugen Oct 03 '17 at 10:08
177

This site suggests:

if you run a clean on the project it should regenerate all the generated Java files, namely R.

...and...

In Eclipse, under the Project menu, is an option build automatically. That would help you build the R.java file everytime modifications are made. The Clean... option is also there under Project.

This site suggests another solution.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 1
    Who would have thought, thanks guys +1 here I had a problem with my layout and so R.Java was not being created. Great heads up! Cheers. –  Jan 27 '12 at 00:53
  • 101
    Doing Project->Clean is what caused the problem for me. Cleaning deletes R.java...and for whatever reason the plugin is not regenerating the file. – Cerin Jul 13 '12 at 15:51
  • 7
    I had this pop up when I linked against a library that required a different Android support API version than I'd installed. The dependencies didn't work and my R didn't get generated after that. As others have said, it shows up in the errors log, so check there. – mikebabcock Sep 28 '12 at 15:00
  • 1
    In my case, cleaning the project just **removes** `R.java`, in stead of regenerating it. http://stackoverflow.com/questions/13065425/cant-access-objects-on-the-gui – RobinJ Oct 25 '12 at 09:40
  • 96
    R.java doens't regenerate if you have some errors in the res directory. Removed errors -> solved problem in my case :) – JavaRunner Nov 15 '12 at 14:18
  • 1
    In my case, my image file name is default.png, and that block R.java from being regenerated. change it to splash.png fix the problem. It seems you can't use several reserved words as resource names. – LeonQiu Jan 29 '13 at 04:10
  • Whatever if I use the different workspace in Eclipse, especially imported from Github without pushing bin folder, does the bin automatically updates for changes all the time? If not, should I commit and push the bin folder for no errors once running the app? – David Dimalanta May 16 '13 at 02:22
  • 1
    @JavaRunner, same applies for AndroidManifest file. – hytromo Mar 31 '14 at 14:23
  • The error is always nearly 100% due to XML errors. Make sure the XML FILES ARE IN ORDER! :) – basickarl Jun 02 '14 at 22:14
  • The R file is still not generated, I agree to the Karl Morrison the error is always in the XML files, but HOW can i find it? Quick Lint Tool for Android??? – Erdinc Ay Feb 06 '15 at 14:16
  • R.java doesn't generate When i am trying Clean project. – Praveen Kumar Verma May 02 '15 at 21:44
  • R. java should come under gen folder? – Narendra Jaggi May 24 '15 at 17:51
40

I had the same issue. When I checked it out I found that the name of the XML resource under layout was not having the correct naming convention. It had some capital letters. So I renamed it to make all letters lowercase and the magic worked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amit Kumar
  • 121
  • 2
  • 2
  • 2
    tnx...silly me thinking I could use upper case in my layout names – Kevin May 17 '11 at 20:15
  • worked for me too: the console reported this: [2010-09-22 16:19:40 - myproject] res\drawable-ldpi\downloadNow.png: Invalid file name: must contain only [a-z0-9_.] – sami Sep 22 '11 at 11:23
  • 1
    Thanks, this was my problem. I had an .xml file in the layout folder with a capital in it, and my entire R.java filed was missing/ would not generate because of it. The only way to know that was to realize that the naming convention didn't allow a capital; no errors were generated. – Lemmings19 Mar 09 '12 at 22:50
  • 1
    When naming resource files or attributes, try naming it as like u name a variable in Java. the R.class puts the resources i.e. id,drawable,xml file names with a public static final constant matters, so putting "-" , "special character" would lead to stop generating R, as those symbols may have other arithmetic means. – IronBlossom May 05 '12 at 20:14
  • 1
    Yep, this was the problem for me. A file in my res/xml referred to a layout file that didn't exist, and apparently this causes R.java to not be generated, so if I run Clean, that caused a missing R.java, resulting in hundreds of import errors... – Cerin Jul 13 '12 at 15:56
  • In my case, in strings.xml I wrote `didn't` instead of `didn\'t`. – AlexAndro Dec 14 '14 at 14:23
28

One reason the R.class can go missing suddenly is when there are errors in you XML files. For instance, when you add an XML file with uppercase letters in the name like myCoolLayout.xml which is not allowed. Or when you have references that don't point to existing files, etc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pawel
  • 71
  • 2
  • 2
17

In Eclipse, simply use Project --> clean to clean the project. The R.java is going to be automaticly (re)-created.

If for some reason that dosn't work: Make sure your layout.xml files don't contains errors. Eclipse seems to be a bit buggy here: sometimes it doesn't mark the errors within the XML nor the package explorer. In such a case: Take a look at the "console" or "problems" view after using "clean". All errors should be displayed there. Fix them and redo a clean.

NOTE: It is NOT neccessary to fix the errors you get because of a missing R file! Just fix the XML files and other project errors and use clean!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
omni
  • 4,104
  • 8
  • 48
  • 67
  • Make sure your resource file naming conventions are correct. @Masi thanks for your tip of looking into "console" for errors after clean. this helped me. – Bharath Booshan Mar 12 '12 at 18:50
10

Quick fix:

The package name in the manifest needs to be the same as the one in the /src folder, the /gen folder package will be automatically reproduced.

Detailed observation:

Observe the name of package in the /gen folder. In my case it was different than the one in the /src folder.

The package referenced in the manifest was that of the /gen folder.

I attempted to add a package with the name of the /src folder to the /gen folder too see what would've happened, but this did not solve the issue. I proceeded then to remove the package name that was not the same as the package name of the /src folder. This package with the name that did not correspond to the /src folder was recreated as soon as I removed it.

Since the package reference in the manifest corresponded to the one that was being senselessly recreated and that did not correspond with the package in the /src folder, this prompted to rename package = "oldPackage" with the package = "srcFolderPackage".

Community
  • 1
  • 1
9

Android has added in SDK build tool ADT 22 for the building mechanism. You just need to do the following steps.

  1. Update Android SDK Tool
  2. Update Android SDK Platform Tool
  3. Update Android SDK Build Tool
  4. Add the path of your build tool to the path variable. (path up to---- YOUR DIRECTORY-PATH\android-sdk\build-tools)

This will solve the issue.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mr.India
  • 674
  • 2
  • 9
  • 19
9

I had the same issue, and I finally found the problem: In the strings.xml, I did not keep the line <string name="app_name">program-name</string> which is present by default when creating a new Android project. I thought it was only used for the program HelloWord, which is the original program.

However, the AndroidManifest.xml refers to this value, hence:

=> A compile error;
=> the R.java isn't updated;
=> ID are not recognized...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Glaux
  • 61
  • 1
  • 2
  • the line in the string.xml is "name of the program". Sorry, i had a problem with my post. – Glaux Dec 04 '10 at 18:32
8

If your R.java isn't getting generated, one of the solutions is to delete the layout file named "blabla.out.xml".

After deleting this file, try cleaning the project from menu Project -> Clean.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shashank
  • 71
  • 1
  • 1
7

As a generalization of Glaux's answer, if you have any errors in the res directory, then R.java may not generate - even if you clean and rebuild. Resolve those errors first.

As an example: when you add an image file of say, "myimage-2.jpg", the system will consider this an error, since file names are limited to alphanumeric values. Do a refresh on your 'res' directory after adding any files and watch the output in your console window for any file name warnings.

Abel
  • 56,041
  • 24
  • 146
  • 247
JnBrymn
  • 24,245
  • 28
  • 105
  • 147
  • Exactly! This happens to me every once in a while when I rename strings in my strings.xml. This can create dangling references in my layout files. And THAT inhibits the production of R.java. Too bad there's no way to refactor XML IDs. – SMBiggs Mar 12 '12 at 22:43
6

If your OS is Ubuntu, I can provide some suggestion:

  1. Install or upgrade ia32-lib:

    sudo apt-get upgrade ia32-libs
    
  2. Check if you have the right permission on the aapt folder:

    cd ANDROID/adt-bundle-linux-x86_64-20130522/sdk/build-tools/android-4.2.2
    chmod 777 aapt
    
  3. Start Eclipse:

    sudo eclipse
    
  4. Run Project -> Clean in Eclipse

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nevin Chen
  • 1,815
  • 16
  • 19
  • excellent. and also it makes adb work. Just remind me I should install this when reinstall ubuntu. – Yeung Oct 07 '13 at 08:02
  • same solution is true for Debian amd64 – Markus Peröbner Dec 09 '13 at 15:51
  • This answer http://stackoverflow.com/questions/19523502/how-to-make-androids-aapt-and-adb-work-on-64-bit-ubuntu-without-ia32-libs-work provides fresher instructions, ia32-libs didn't exist on Elementary – Singagirl Mar 04 '16 at 08:28
6

You 100% have an error in an XML-file, but the XML verification does not show you the error. This is the reason why you need to check your XML files first!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Erdinc Ay
  • 3,224
  • 4
  • 27
  • 42
5

It is ALWAYS helpful to take a look at the Problems Tab in Eclipse. In my case, I was getting a "android unable to resolve target 'android-8'" error message that kept the R.java from being generated. So, I corrected the imported target to the one I was using in the default.properties file, then I performed a clean via Projects->Clean and voila! R.java is automatically generated! Hope it helps!

goseib
  • 737
  • 3
  • 12
  • 24
  • Thank's for pointing to the problems tab. Im new to android and eclipse, so didn't use it before and was geting frustated. It's first place to look when encountering this error. – Maadinsh Nov 02 '12 at 12:56
4

Make sure you are not importing

android.R;

Zain Ali
  • 15,535
  • 14
  • 95
  • 108
4

R.java will never be generated if there are any errors in the res folder. For example, in the drawable subfolder there are two files which have the same name, one is icon.png and the other is icon.html.

You can see some error in the Eclipse console log window which is saying "Resource entry icon is already defined.". After deleting icon.html, you can clean or just delete the gen folder. You will find that R.java is created.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yuxiaomin
  • 71
  • 1
  • 3
4

I've found that any file that has capital letters in the res folder will create this error. This happened to me with a PNG file I added and forgot about.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

Cleaning project, closing Eclipse, re-opening it and launching my project finally made my R.java to reappear... Hope it helps

Filippo Mazza
  • 4,339
  • 4
  • 22
  • 25
3

After reading through many posts and YouTube videos, I found that each of us have R.java missing for different reasons.

Here's how I fixed this in Eclipse:

  • Create R.java in gen folder manually and save.
  • After that go to Project and click "Clean"

The following message will display and your file will automatically be rewritten:

R.java was modified manually! Reverting to generated version!

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
Anup Singh
  • 313
  • 1
  • 5
  • 18
  • 1
    Anup ..I did that/// But only to be removed when I cleaned it...The problem is still unsolved for me! – ejjyrex May 28 '13 at 17:14
3

For me, I had linked v7 appcompat twice. Anyhow, in Eclipse, right click the project name in Package Explorer or Navigator, go to Properties, Android, and uncheck any duplicates in the Library section. You may need to Build --> Clean afterwards.

Shanker Kaura
  • 117
  • 1
  • 5
2

I had this problem. Accidentally I deleted this

xmlns:tools="http://schemas.android.com/tools"

which started causing build errors all over the project in my XML files as well as my Java files. As soon as I retyped what I deleted, it worked again :)

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
Mohamed El-Saka
  • 732
  • 10
  • 13
2

OK so it's clear that there can be a lot of causes for this problem. If you're on a 64 bit linux machine and you are just setting up the ADT for the first time, you may get this problem where R is not automatically generating. Check the console tab and you may see an error similar to:

 'No such file or directory' while attempting to get adb version from '/home/patrick/code/android-sdks/platform-tools/adb'

If that's the case you need to install ia32-libs, using something like:

 sudo apt-get install ia32-libs

See here for details: Android adb not found

Community
  • 1
  • 1
Patrick
  • 5,970
  • 4
  • 24
  • 21
2

My problem was that appcompat was not in the same directory as my project.

I found this out when compiling my app with Ant.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Martin Ždila
  • 2,998
  • 3
  • 31
  • 35
2

I've came across this problem a few times. I found that if I didn't import the package R through my application's name, for example, if my application had the package name example.test then I found that I had to import example.test.R in order to access any of the resources.

If this wasn't imported then the resources that where getting returned were the default resources with none of my own included.

With that said if you find that you are only getting a list of default resources then just check to make sure that you're importing application_package_name.R and not android.R.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Spider
  • 156
  • 2
  • 8
2

There's obviously no one final answer to this, but here's another one I don't see here already:

I've had R.java disappear after (attempting) a Team Synchronize, and there were conflict resolution files in the project (i.e., thingy.xml.mine,thingy.xml.r35, etc.). Deleting them/resolving the conflicts regenerated R.

Ben Mosher
  • 13,251
  • 7
  • 69
  • 80
1

I had the same problem. It turns out I had a circular reference; I changed a string name that was referenced in my layout.

I did a clean and rebuild and the R.Java file was not being recreated. I wasted two hours trying to figure out what was wrong. I eventually changed the layout item to some temporary text, did a clean project and the R.Java file was created.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris
  • 1
  • 1
  • 2
1

In my case, R files were not being generated because I had XML files in my res/ folder with the same name (for example, res/layout/filename.xml and res/values/filename.xml).

After I changed one of the filenames, my R files were generated again by the Build Automatically option.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin
  • 1
  • 1
1

When nothing seems to work correctly even though you think that everything is right, check your XML files one by one. The mistake is there 100%.

If you work with the GUI of the XML files everything seems OK, but when you enter textual XML files you see the mess inside. Eclipse is a bit buggy for this stuff.

Set BuildAutomatically as well, so after you fix your errors you will have your R.java file in the generated ones.... GOOD LUCK!!!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dragan
  • 500
  • 3
  • 11
1

This problem just came up with me as well and unfortunately none of the suggestions here helped me. After trying a lot of things, I finally found out what happened and hope it can help others.

Yesterday I downloaded a lot of new stuff using the Android SDK Manager. After that I updated the Android Eclipse plugin. Today, after an Eclipse "clean", the R file vanished and stopped being generated.

The reason was that, somehow, the list of available SDKs (under preferences-> android) was empty. After pointing it again to the right directory it reloaded and showed the available SDKs. When I closed the preferences and tried another build, everything came back to normal.

In the image attached you can see the list of SDKs already filled. While the problem was happening it was empty.

I hope this can help others!

Screen with SDKs correctly showing

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ederribeiro
  • 388
  • 1
  • 3
  • 15
1

Also make sure that you have the latest version of the ADT plugin, and open the Android SDK manager to update all SDK tools to the latest version. Check for updates in Eclipse itself as well. Non-matching versions caused the problem for me.

caw
  • 30,999
  • 61
  • 181
  • 291
1

My problem was inside a menu file. The compiler doesn't seem to warn you if strings which do not exist inside strings.xml are referenced in menu files. Check if your items in your menu XML files reference any strings which do not exist in your strings.xml. Usually in android:title.

Stefan Anca
  • 1,386
  • 14
  • 23
1

For me, this error was caused when I tried to duplicate an existing res/menu instead of creating one with the wizard.

Hakim
  • 3,225
  • 5
  • 37
  • 75
1

All of these answers could not work if you use Maven. The solution for me was to add

<genDirectory>${project.basedir}/gen</genDirectory>

to the configuration section of android-maven-plugin.

Bevor
  • 8,396
  • 15
  • 77
  • 141
1

I found a solution why R.class is not made by Eclipse after making it again - 2 clean, build, etc.

The problem is here in strings.xml:

<string name="hello">Hello World, HelloAutoComplete!</string>

<string name="app_name">HelloAutoComplete</string>

These are by default created by Eclipse when you create projects.

Definitely you are changing the strings.xml for your own requirement. Sometimes you clear the string.xmls these two lines from your code:

It is making a problem in the AndroidManifest.xml file:

<application android:icon="@drawable/icon"  android:label="@string/app_name">
<activity android:name=".HelloAutoComplete" android:label="@string/app_name">

So it can't communicate with strings.xml.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aarifmkhan
  • 17
  • 2
1

I changed my layout XML file name and found out later that of the XML file (widget provider in this case) still refers to the old layout XML which doesn't exist, and that prevented the auto generation/correction of R class.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Howard
  • 1
  • 1
1

I want to highlight 糞_爺's answer.

I had renamed a color from "listcolor" to "color_list", but I didn't catch that one of the other layouts was referencing it. So (suddenly) all of my R.string.X and R.layout.X failed on the 'R'.

Eventually, I looked in the Console window and looked at the errors. I thought they were all references to R not being found, but actually, one was that 'listcolor' wasn't found. As soon as I fixed that, the other errors all went away.

Maybe a future version of the plugin for Eclipse will allow you to rename resources in these XML files and have it propagate to all references of it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jon
  • 198
  • 1
  • 2
  • 12
  • Yes, they desperately need to add that feature! The most recent Android tools for Eclipse (ADT 16) hint to me that this is coming in a future version. I renamed some Strings stored in `strings.xml` and saw my changes automatically propogate to at least one .java file referencing those Strings, but not all of them. The infamous "R cannot be resolved to a variable" error ensued. It's encouraging that a feature like this appears on the [Android tools wishlist](http://tools.android.com/contributing/wishlist).... – hotshot309 Feb 19 '12 at 19:48
1

For me, the problem was that I had an image in my res folder with an uppercase letter, that is, Image.png. Just put image.png.

All to lowerCase and that's it!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

I encountered this problem for the first time this afternoon. Even when I create a new project, R.java isn't generated. I changed the workplace, and everything works again.

Kevin Pope
  • 2,964
  • 5
  • 32
  • 47
ljz51
  • 1
0

I had the same issue. Turns out the reason was a few of my resources had capital letters in their filenames. I know it sounds trivial but just changing them to all small letters fixed it and retrieved my R.java file again.

Hope that helps.

xemacobra
  • 1,954
  • 2
  • 21
  • 21
0

Almost assuredly there is something wrong with the content that would be inserted into the genfile. Eclipse is not smart enough to show what the problems are or even indicate that there are problems!

Think about the last edit you made to any of the XML or image content - and try to 'rollback' your changes, manually if necessary.

I find that sometimes Eclipse does not like my file names for whatever reason, and I have to change them.

So add to the resources one by one assuring that it all 'works'. When something breaks, just try changing it a little bit until Eclipse accepts it.

You know it's working when the genfile appears - it will do so automatically if there are no problems.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jomamaxx
  • 111
  • 5
0

You can try one more thing.

Go to C:\Program Files (x86)\Android\android-sdk\tools>, run adb kill-server and then adb start-server. This worked for me, and I do not know the reason :-).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Varun
  • 1
0

Try checking the project.properties file. It might be the case that your Android JAR file in the build path doesn't match with the version mentioned over there.

It solved my problem as I had changed my target.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adithya
  • 2,923
  • 5
  • 33
  • 47
0

I had a problem with my AndroidManifest.xml file and the R.java was not generated.

I think the solution is to check ALL of your XML files, everywhere!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Florin Vistig
  • 1,639
  • 2
  • 22
  • 31
0

I forgot to set a name attribute on a declare-styleable tag

<declare-styleable>
        <attr name="myView" format="reference"></attr>
</declare-styleable>

I fixed it with

<declare-styleable name="Theme">
        <attr name="myView" format="reference"></attr>
</declare-styleable>

I didn't got any errors

passsy
  • 5,162
  • 4
  • 39
  • 65
0

I had the same problem.

After checking Lint warnings I saw the warning The resource R.string.app_name appears to be unused..., so I deleted R.string.app_name.

This resource is required in AndroidManifest.xml although Lint warned me about an unused resource. I added "unused resource" to the manifest and everything went back to normal.

I hope this helps to someone.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vaske
  • 398
  • 1
  • 3
  • 13
0

I can't see it listed here, but I encountered the problem of my R.java being removed and not being automatic rebuilt.

This happened to me when I was dealing with switch statements and strings. Eclipse suggested that I change my projects compliance to 1.7 which promptly broke the project (R.java) as Android can't use this compliance.

Console:

[2013-03-12 17:26:33 - CrimeAtlas] Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties.

I fixed the error by using "Fix Project Properties" and then going through the log like suggested. I commented out the XML in files mentioned replacing them temporarily with a LinearLayout. When it's fixed, bam, it reappears (if "Build Automaticly" is on). I can then change the XML files back if there was nothing wrong.

Community
  • 1
  • 1
Jethro
  • 928
  • 1
  • 10
  • 20
0

There were no errors but three warnings where I had used hard coded text for button text attributes in my main.xml. Something like this:

<Button
    android:id="@+id/scan"
    .....
    android:text="Scan" />

After changing text attribute to android:text="@string/scan_device" the R.java was immediately generated.

So, it always occurs on my machine/Eclipse whenever there's any error/warning in one of the XML files.

IntelliJ IDEA doesn't have this problem for warnings though.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amar
  • 473
  • 1
  • 7
  • 20
0

See blog post Android Developer Tip: Regenerating R.java.

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 the 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.

In single sentence: Remove all unwanted files from the project and unwanted code from the manifest file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AKA
  • 29
  • 4
0

Solve the problem by changing the file project.properties: I add a target of android-17.

Yingpei Zeng
  • 515
  • 8
  • 10
0

If all answers fails, you can run the resource generation from command line: Go into your Eclipse project directory. Then run

aapt.exe package -f -v -m -S res -J src -M AndroidManifest.xml -I ANDROID_HOME/platforms/android-XXX/android.jar

Just change ANDROIRD_HOME and XXX with appropriate values. You should get on-screen information where the error is.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eldy
  • 498
  • 4
  • 7
0

What also causes this is if you have a scale9 drawable with the same name as another file:

  • img_file.9.png
  • img_file.png

Simply removing either one of the files fixes it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
basvk
  • 4,437
  • 3
  • 29
  • 49
0

There is also another situation that happened to me. It was when I added a string with an apostrophe in my strings.xml resource file, like

<string name="Today">Returns Today’s date</string>

It was fixed by changing the apostrophe for another type of apostrophe, like ´.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pvalle
  • 1,335
  • 1
  • 13
  • 20
0

It seems like all answers here keep repeating to check for errors in the XML and clean your project. Do it! But what if that doesn't work? Here's what worked for me:

If you build a project that generates a wrong R and you delete it, it won't regenerate so delete this project and create new one, make sure you do NOT check "build automatically" then you can copy your folders into the workspace and THEN you clean the project or check "build automatically" so it will generate your R properly.

Thiago
  • 746
  • 1
  • 10
  • 22
0

In my case I checked all previous the solutions but not generated R. I just removed my main.xml file from the menu folder and regenerated it again. I don't know why, but it is working for me now...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0

Here's one more answer that I found, after none of the previous 30 answers helped.

I found my issue in the strings.xml file, although it is very very odd. I stripped down my app to bare minimum and this was the only thing that came to light. I can even replicate it on a brand new empty app.

In the strings.xml file, this compiles (and generates the R.java file):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test_My_App</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>

However, this is not:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello_world">Hello world</string>
<string name="app_name">My_App</string>
<string name="action_refresh">Refresh</string>
</resources>

The "funny" thins is that I removed the action_settings several days ago. I've cleaned my code since then and never had any issues. I added additional strings to the strings.xml file today and after cleaning the code the wheels came off. Also, if I re-arrange the order of the same 3 string lines, and clean my code, the R.java file does not get re-generated.

There's my solution to my particular issue.

I hope it helps somebody...

TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
0

Here's what worked for me that the other answers didn't:

  1. Create new Android Application Project,
  2. Copy all files and folders from old directory (except /gen) and replace!
  3. Refresh project root in Package Explorer.
  4. If still there, Clean again!

    Done. Hope this helps.

DarkReaper
  • 29
  • 6
0

If you use the Lint error checker it will identify spurious import of "R". Once the XML system gets hold of the wrong end of the stick all is lost!!!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user462990
  • 5,472
  • 3
  • 33
  • 35
0

First of all, check the Console output.
Then, check if your xml layout files names are lower-case only.
Correct xml errors.
Check Project properties -> Android for something unfamiliar

0

Don't delete these two lines from strings.xml:

android:icon="@drawable/icon" android:label="@string/app_name"

android:name=".HelloAutoComplete"

android:label="@string/app_name"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aarifmkhan
  • 17
  • 2
  • I think you mean that these lines appear in your `AndroidManifest.xml` file. There isn't much context here...but in any situation, if you are referencing a resource in your manifest or a layout .xml file, and you rename, move, or remove that resource, you need to fix the corresponding reference in the manifest or layout .xml file. Otherwise, you're going to have problems generating `R.java`. – hotshot309 Feb 19 '12 at 19:58
0

I had an error in AndroidManifest.xml file, so after fixing that error and hitting

Run->clean

it produce me R.java

user1324491
  • 147
  • 4
0

In my case, after endlessly shutting down the IDE, cleaning, trying to build, etc., the issue was a "untitled folder" inside my "res" folder that I probably added there by mistake.

I wish those kind of errors would be output by Eclipse, the way the Ant script did:

[null] invalid resource directory name: /Users/gubatron/workspace.android/my-project/res/untitled folder
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gubatron
  • 6,222
  • 5
  • 35
  • 37
0

Mainly the R.java file is relating to the .xml file and the Java. We know the XML tags, but what about Java, so for that we need to have an unique id for identifying the tag.

Here the R.java file is mainly related to the XML file with Java for getting the values.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
manu
  • 1
  • 1
0

This problem also happened to me when I was trying to build the Support4Demos from the SDK source code.

After fixing some problems in the resource files (for example, "fill_parent" renamed to "match_parent") I've discovered that the problem was in the manifest file: I unchecked by trial and error the "Define an <application> tag in the AndroidManifest.xml" checkbox, saved the changes and the R.java magically reappeared. Then I re-checked the box, saved the file, and the R.java was regenerated again.

I guess I came across an Eclipse shortcoming.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PJ_Finnegan
  • 1,981
  • 1
  • 20
  • 17
0

If your AndroidManifest.xml file is referencing String constants that you have stored in strings.xml, and you rename those Strings in strings.xml, you'll need to change them in the manifest to make everything kosher for a build.

The same would go for any layout .xml files that are referencing those constants you changed. Unfortunately, neither the Markers view nor the Problems view in Eclipse will tell you where you need to go to fix the issues--just that they can't find R.java. As mentioned in other answers, look at the Console to see where you need to fix your constant references, and then clean your project again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hotshot309
  • 1,718
  • 1
  • 18
  • 20
0
  1. Make sure your XML files are fine without any issues
  2. Make sure your manifest has no errors in it
  3. Make sure your strings have no problem in them

If any of them have issues, Eclipse might fail to build the R class... Fix the issues, clean & rebuild..

Li3ro
  • 1,837
  • 2
  • 27
  • 35