1132

I just downloaded and installed the new Android SDK. I wanted to create a simple application to test drive it.

The wizard created this code:

package eu.mauriziopz.gps; import android.app.Activity; import android.os.Bundle; public class ggps extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

but Eclipse gives me the error

R cannot be resolved

on line

setContentView(R.layout.main); 

Why?

PS: I do have an XML file named main.xml under res/layout/.

5
  • For those who still experience some problem with eclipse not being able to update/generate R.java. A possible solution is by creating a dummy xml and just delete that xml. In my case this is the solution Commented Jun 25, 2012 at 10:59
  • Honestly, this sounds so stupid (and it is), but modifying the AndroidManifest.xml, then undoing that edit, then saving the file worked for me. If I cleaned the project after that, it broke again. And all this time I thought Xcode was the only IDE that had these kinds of mysterious errors. Commented Dec 22, 2012 at 18:12
  • 3
    Did you try right click on the project -> Android Tools -> Fix Project Settings? Commented Feb 1, 2014 at 10:59
  • 6
    Please check your XMl layout file, if any error in xml you can't generate R.java, and the error comes after bulid and clean also. Commented Feb 20, 2014 at 11:47
  • 1
    Just a tip: when I switched IDE from Eclipse to Android Studio this stopped happening to me. Commented Jul 17, 2014 at 12:13

108 Answers 108

1 2 3
4
0

I was facing the same problem, I googled and tried suggestions I found but none of them worked for me. I changed my workspace and it worked, but before changing your workspace try other workarounds you have. Any one of them might work for you.

Sign up to request clarification or add additional context in comments.

Comments

0

I would say "R can not be resolved" is something too general and can be misleading at times. I also had the same problem many times ago even when I first create a new project (which mean I did not mess with the code yet). Most of the time I just need to update my sdk manager (as some people have mentioned). But in my recent case, I found out that the path to my project file is too long (something like this E:\R&D\ANDROID\ANDROID BASIC\Folder 1\ Folder 2\..... \Folder n\MyProject). The folder names also contain spaces character. I thought it could be the case and indeed it's true, when I created a new project in another folder with shortest path possible, and none of the folders' names containing space character, it worked as normal.

Comments

0

What worked for me is installing "Android SDK Build-tools". Open the Android SDK Manager and install the Android SDK build-tools. It also helped to update the Andriod Development Platform in Eclipse: Help > check for updates

Comments

0

Right click on your project and click Refresh Or hit F5

Comments

0

Import R class with your package name. Do not import android R class.

Comments

0

reason R.java might not get auto generated is if you have directories like

res/drawable-hdpi, res/drawable-mdpi, res/drawable-ldpi. image name should be in a proper way 

Check the Image Name and check the and layout/any.xml and string.xml

Comments

0

This is a very old thread, I had a new finding today. I have created an activity, I put it in another package(by mistake). In the newly created class, eclipse was not able to resolve R.

I tried all the ways mentioned in many places but I failed to notice that I put it in a different package. After struggling for few minutes I noticed that I put it in wrong package.

If you are not keeping your Activity in the activity package, you will get this error.

Comments

0

If you work with eclipse: Sometimes eclipse supresses the error messages in the ressource files. Solution: Save a temporary ressource that has no errors in the layout folder. Build the project new. Then eclipse shows the correct error in the ressource file. Delete the temporary ressource.

Comments

0

Also, if you have extra files like "desktop.ini" in your res folders that are added by the OS but are totally unnecessary, try deleting them. This apparently was one of the things blocking the generation of R.java in an imported project of mine.

Comments

0

Each time I had a problem with R not been generated, or even disappeared, this was due to some problem in the XML layout file that prevented the application from being built.

R is a generated class. If you are using the Android Development Tools (ADT) it is generated whenever the project is built. You may have 'Build Automatically' turned off.

Comments

0

R is auto generated resource in android project some times it happens don't exactly know why but have you try build automatically option from project

=>remove other import of R file of other project or android.r =>always remember if you always face problem with auto generate id or auto deleting R file you can generate static id like string file and use that with @id instead of @+id

Comments

0

Oh God I am Linux (ubuntu) this is how I resolved it:

First thing the problem is permission problem I was not seeing any perission realted error but that was the problem. The tools were not have executable permission.

So just a a quick fix(If you know the exact file that need executable perission apply the ff command specifically to them.)

change to the dir you put your sdk, then:

sudo chmod +x -R . 

After that goto project->clean->select your project->clean

That worked for me!.

Comments

0

When running x64 Linux you may be missing some x32 build libs. Ex for Debian make sure you have libc6-i386 & zlib1g packages installed.

Comments

-1

This could be an issue caused by manually renaming the package in the AndroidManifest.xml

An alternative is to use the built in tool to rename the package, this will take care of most of the problems avoiding the R not found issue.

Right Click on the project Click on Android Tools Click on Rename Application Project Enter the new package name 

Then a pop-up will appear asking for changes to be applied, including the R workaround.

Source: Rename package name in android

Comments

-1

To resolve R

  1. Make your all XML file correct

and then Clean Project in Build menu

This is the simplest way to do it

Hope it hepls.

Comments

-2

I'm guessing you just copy and pasted the codes, or not. Either way check your AndroidManifest.xml file, see if the package name is correct.

Comments

-3

Check the content of your XML file. If layout_width or layout_height contain the match_parent element, then change it to fill_parent. It solved the problem for me.

Comments

-4

Press Alt+enter and Import

or

Ctrl+F9

and solved.

Comments

1 2 3
4

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.