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

4

In my case, I had an error in my AndroidManifest.xml. Others have said that your XML files must be free from errors, but I was only looking in the res/ folder. Find and fix as many possible errors and the problem may well resolve itself.

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

1 Comment

This was the culprit in my case. ANY error in the manifest.xml file can cause this problem if R.java hasn't been built yet.
4

Change build target from 1.5 or 1.6 to 2.2 (API version 8), and check if you have the % character in string.xml. If yes, replace with %%.

1 Comment

xml file not well formed : had an apostrophe, fixing it cleared a million errors for R not resolved
4

I changed android:versionName in file AndroidManifest.xml from 1.0 to 1.1. That regenerated my R.java file, and I'm a happy camper. I hope this might help somebody.

Comments

4

Unfortunately none of the existing questions helped. Since my problem seems platform specific, this may only work if you are on Ubuntu 64 Bit (I am currently using Ubuntu 12). At first I did not see the "Problems" Window, which already hinted a solution, that on 64 Bit Environment you need the 32 Bit libs, which may be missing:

sudo apt-get install ia32-libs

I installed the package and rebuild my project, which fixed the issue for me.

Comments

4

restart your computer.
Nothing helped me except this way

Comments

4

Every time I come to this error and always been landed on SO. and end of the day the reason is same.

So Please Keep in mind always this small note

I found most of the time reason is - error in XML layout

If your XML isn't correct, then the R file is not generated after a build.

Comments

4

In my case, R.java was not generating because there was a problem in AndroidManifest.xml file wherein I had wrongly specified the name of an activity!

2 Comments

Correct. My Manifest had a wrong theme android:theme="@style/Theme.AppCompat.Light". I have removed this property from application tag, gone to Project->Clean menu and R.java was regenerated correctly.
xml file with error like layout or manifest will prevent eclipse from auto generating R res
3

My problem was that I left out the = mark from the strings in strings.xml e.g. <string name"dialog_grl... It did not appear to me even when the graphical layout of my .xml file could not show the strings. After a project clean all files got messed up.

Comments

3

just clean and rebuilt your project it sometimes help

Make sure that there is no error in your res/drawable folder in android.While naming images in this folder don't give numbers and capital letters.

Comments

3

Install all Android SDK Build-tools and remove import android.R in your Activity class. After click Project -> Clean

import android.R;

enter image description here

Comments

3

There are several reasons of R can't be resolved.

  1. Most of time this error occur for XML file fault. This error can be solved by Clean and rebuild your project, also you can restart your android studio.

  2. Sometime If you codeing some wrong thingin AndroidManifest, this error can be occur so Please check this.

  3. If your resources contain the same name or they have invalid characters, this error can be occur.

If you already check those and yet you are facing with same problem then go ahead:


This error can be occurs when Android Studio fails to generate the R.java file correctly, so you have to follow those steps:

Step 1: Safely delete the build folder from app directory.
Step 2: In Android Studio, File -> Invalidate Caches / Restart
Step 3: Build -> Clean Project Then Build -> Rebuild Project.

so build folder will be regenerated and hope so error is gone.


If Error still found then maybe you have performed refactoring the package name previously. This case you can press ALT + ENTER together for import the R.

Comments

3

Check with the class files imports above and make sure that there is no import of android.R, if it has then remove it and then press Ctrl + Shift + o and select the packagename.R import not the other one.

Comments

3

1) First of all close all the file.
2) Go to the Build -> Clean Project.
3) Go to the Build -> Rebuild Project.

This works for me.

1 Comment

I test your solution; but For me, Go to the Build -> Build Module "Module Name" did the trick and showed me the error in my Manifest file
2

If you are on a Mac, check if a .DS_Store has been added to a directory in /res (ls -al on the Terminal command line). Remove it and refresh/rebuild.

Comments

2

Recently, I faced a similar situation with R that could not be resolved.

  1. Project -> Clean (it usually works, but this time it didn't).
  2. I tried to remove the gen/ folder (which automatically gets built) - nope.
  3. Dummy modifying a file. Nope again. By now I was going grey!
  4. I checked in the AndroidManifest.xml file if appName was present. It was - checked!
  5. Looked again at my res/ folder...

And this is where it gets weird... It gives an error, in some but not all cases..

I added the formatted="false" flag:

<resources>
    <string name="blablah" formatted="false">
    </string>
</resources>

Bingo!

Comments

2

Wow. I resolved my R issue by tracking down all possible errors in all XML files and related resources. I know that is a handful of layers, but simply you must:

  • Make sure any referenced resource image is actually there
  • Everybody already mentioned so much that this is just echo... Remove all Eclipse injected R.* imports if need be.
  • I like to restart Eclipse when I do the above.
  • Make sure all XML files are properly structured. Like, just do your stuff. Hahaha
  • Go through as many lines as you can. Get in the habit of hitting Ctrl + Shift + S often to get compiler results quickly changed.

Hahah. Sure it is nice to get the R Cannot be resolved fixed though...

Comments

2

For me, I removed "import android.R;" and it fixed all my problems. What a nightmare!!!

Comments

2

If cleaning the project doesn't work and if there is no problem with xml files(If it is a new project). Then go to Androidmanifest.xml,edit it and add some spaces between the lines, save it. Now clean the project, error will vanish.

This happens because of stale R.java file. When we edit android manifest file,R.java is created again.

Comments

2

To resolve this issue please go to java Build path->order and Export and select all checkbox and select ok and then clean and build project and import your package name R file(Ex com.demo.R). It works.

1 Comment

Could you please clarify this? I've fixed the spelling errors, but am still not 100% it makes sense.
2

I solved it for myself with Window->Preferences->Android and SDK Location: path to adt\sdk e.g. C:\adt\sdk

Comments

2

I think the problem is in layout.xml file or in import statement.

Be sure that you have not imported R.android., instead of import R.YOUR PACKAGE NAME..

It will work fine.

Comments

2

For me it was due to using 'private' android drawables:

[2015-11-26 18:59:48 - MyAndroidApp] 
/Users/myname/git/MyAndroidApp/res/menu/drawer_view.xml:24: 
error: Error: Resource is not public. (at 'icon' with value 
'@android:drawable/ic_menu_home').

The interesting thing is that this errors didn't show up in the Console until I removed the reference to the file using the icons (ugh!).

android:entries="@menu/drawer_view"

Now the solution was quite obvious, copying the icons into my project (res/drawable-XXXX/) and switching to local references.

@drawable/ic_menu_home

Hope this helps.

Comments

2

Just Restart Your Project

File>>invalidate caches /Restart

Note:

  1. Android automatically creates a .R file not need to import
  2. Import android.R.* << R.java file auto created by android Studio
  3. Android studio has lots of caches hold and that's why some time it's occurred

Comments

1

The real question is how to prevent the import android.R; from returning:

Window -> Prefs -> Java -> Editor -> Save Actions

Uncheck "Organize Imports."

That will do it.

Comments

1

Yet another reason this might happen: Tonight, I met the dreaded error, and eventually discovered an errant mouse drag apparently moved one of my own packages' classes into the hierarchy of another. There is truly no end to the number of ways you can screw yourself in the Eclipse environment.

Comments

1

My solution (on Eclipse Juno) was to click on "Fix Project Properities" by:

  1. Click right mouse button on project
  2. Choose Android Tools menu
  3. Click on Fix Project Properities

ps. First check if R library is not implemented and if all files in res/ folder are ok.

Comments

1

From the many answers that were offered here, what helped me is to change the order at the "Order and Export" at the java build path.

1.click on you project -> project properties

2.Java Build Path

3.Order and Export

Change the order of the Build class path to be

1.(your-package-name)/src

2.Android (version number)

3.Android Dependencies

4.(your-package-name)/gen

Comments

1

just clean and rebuilt your project it sometimes help

Comments

1

Sometimes it helps when you manually create R.java file just simple empty R.java.

Comments

1

In my case this error was because i imported a image to the folder /res, and the image had a invalid name. Can't believe that i spend almost a hour with this problem until i realized that the image had a invalid name :/

1 Comment

Hehe...I had imported .PNG which i changed to .png after this post :D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.