Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

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!

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

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

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.

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.

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.

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

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!

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!

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

Make sure you are not importing

android.R;

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.

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.

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!

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.

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.

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 :)

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

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.

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

I found this out when compiling my app with Ant.

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.

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.

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

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top