By Maurizio Pozzobon


2009-05-19 21:02:20 8 Comments

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

30 comments

@Mayank Saini 2013-05-03 10:52:35

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.

@Hasib Akter 2018-05-25 18:34:22

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.

@seven 2011-09-21 08:29:56

It is worth checking in AndroidManifest.xml. The attribute package has the correct value.

That is:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="your.correct.package.name"
   ...

After you change that, the R.java will be re-generated.

@Eugene van der Merwe 2012-01-28 23:41:58

My original package name was com.xxxxxdroid.router. I created a new version of my app com.xxxxxdroid2.router. Once I had done this I had to update the package name in AndroidManifest.xml, but only much later after I had started to copy and paste old classes across. Thanks for the pointer!

@GHz 2012-05-20 02:39:08

this fixed my issue, I was working in an xml file, glad I remembered which one, I've got loads of them... thanks...

@Sayan Sil 2017-09-14 13:26:15

When you have just opened your project, it takes a while for android studio to load things up. It happens a lot with me. I just sit back and watch a youtube video for 5 minutes while it is done loading.

By then the cant resolve symbol R is gone! :-)

If you have checked and re-checked everything mentioned above, made sure that the project has completed building, I'd suggest you to patiently wait... If not clean and rebuild the project.

@Rajat Mittal 2017-07-16 08:24:38

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.

@Jaimin Prajapati 2017-03-28 07:59:45

First check is there any error in any xml layout or not, if then resolve it first.

Otherwise remove junit dependency from project and rebuild the project.

enter image description here

@Shubham 2016-07-22 10:01:40

Press Alt+enter and Import

or

Ctrl+F9

and solved.

@Nazmul Hasan 2016-04-02 01:36:28

You may need to update/install SDK tools. Relaunch Android SDK Manager again and install a new item: Android SDK Build-tools.one by one delete,fix which one work for you.enter image description here

@Gundu Bandgar 2016-03-29 06:28:20

Just go to Android Top menu list. click on Build Menu, in under Build click on Rebuild Project.

enter image description here

@01wifi01 2015-06-11 06:10:18

a better and more promising solution

  1. close Eclipse and open your Workspace directory.
  2. look for a folder named ".metadata" and delete that folder.
  3. Open Eclipse.

the Error should go away :)

@DevCat 2015-11-27 01:09:42

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.

@ArteQ 2015-08-26 23:39:40

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

@ethemsulan 2015-06-26 20:56:47

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

@dsharew 2015-04-28 17:22:09

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

@Ajay Pandya 2015-03-05 13:26:46

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

@Akshay Paliwal 2015-02-04 07:05:30

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.

@Praveen 2014-09-09 05:02:51

It worked for me with Right Click>Android Tools>Fix project properties and some time aftr restarting Eclipse.

@satyrFrost 2013-02-15 09:32:11

Along with the great suggestions in the previous answers, make sure your Android target is set:

  1. Right-click on your project
  2. Choose Properties
  3. Choose Android in the left menu
  4. Tick a box next to the appropriate Project Build Target.
  5. Click Apply and OK

Edit: A year later I found another cause. I had a .jpg image in my drawable folder with the same name as a .png image. Referencing this image in my code must have confused the program and it gave the "R cannot be resolved" error.

@Mansour Fahad 2014-08-08 04:28:12

Make sure that all your resources files doesn’t have upper case extension. I found out that I saved some images with upper case extension .PNG. I changed it to .png and everything got back to normal.

@cowlinator 2014-07-28 23:05:11

I solved my issue with this by putting the resource files directly in the directory specified by Android. I had mistakenly believed that R.raw could contain the entire file directory of my resources. All files must have R.raw as their direct parent directory.

@Vaishali Sutariya 2014-07-17 10:28:29

  1. Close all files, clean project, restart Eclipse.

or

  1. R is an automatically generated class that holds the constants used to identify your resources. If you don't have an R.java file (it would be gen/eu.mauriziopz.gps/R.java in Eclipse with the 1.5 SDK) I would recommend closing and reopening your project or going to Project > Build all (and selecting "Build Automatically" while there as recommended by Josef). If that doesn't work than try making a new project

@Aaron Campbell 2014-05-30 18:48:36

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.

@SJX 2014-04-21 09:41:46

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.

@user3024292 2014-04-20 23:28:18

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!

@Junior M 2014-06-14 19:50:11

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.

@macio.Jun 2014-07-18 19:52:49

xml file with error like layout or manifest will prevent eclipse from auto generating R res

@warmth 2014-04-10 20:43:23

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

@NewUser 2014-03-28 04:04:10

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.

@pestrella 2014-03-18 16:44:47

Some answers suggest restarting the computer. I have found that restarting the adb server is enough -- this is particularly true if you've just updated the Android SDK.

Restart adb via a linux shell:

$ cd $ANDROID_SDK/platform-tools
$ ./adb kill-server
$ ./adb start-server

You may need to restart Eclipse so it can reconnect to the restarted adb server. You may need to do a clean/build if Eclipse doesn't compile projects automatically.

@Tanim reja 2014-03-03 07:56:13

Same thing happen to me , and i passed a day to fix this. really this is a stupid error. i am just shearing my experience, may be it would helpful to you . First look carefully when you type this setContentView(R.layout.activity_to_do_list); hear your starting point R.l then eclipse should open a suggestion for you look like thisenter image description here

you import the second one not layout-android.R . the second one is created in your project hear com.example.gridtest is the project package name . then focus your imported section in your code enter image description here

ok look this import com.example.gridtest.R; its important . If you already import this android.R then remove it. Thank you hope it works . ( you don't need to do this always but if you face this kinda problem then do this, hope it would help )

@dhiraj kakran 2014-03-01 13:34:39

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

@Kenji 2014-02-25 09:29:53

I had this problem many times and I found that some times "R wont resolve" accrue because:

  1. some bad named Resource have exist like: an image with name My imge.png
  2. or some error in Resources like error in xml files or Error in defining Resource in your forms that won't exist.

in these case you even can't import your own package R to your project. so Go to your resource folder and search for 2 above points.

this will work

Related Questions

Sponsored Content

13 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

41 Answered Questions

[SOLVED] Is there a unique Android device ID?

89 Answered Questions

[SOLVED] Close/hide the Android Soft Keyboard

77 Answered Questions

5 Answered Questions

[SOLVED] Android App development - WebView is not working

  • 2011-03-25 11:04:00
  • Abhishek Jain
  • 3505 View
  • 8 Score
  • 5 Answer
  • Tags:   android

1 Answered Questions

listView doesn't appear at all. android

  • 2014-08-10 09:22:03
  • user3856461
  • 658 View
  • 0 Score
  • 1 Answer
  • Tags:   android listview

2 Answered Questions

[SOLVED] Getting an exception when app starts

  • 2011-05-22 18:43:12
  • Gareth
  • 2954 View
  • 3 Score
  • 2 Answer
  • Tags:   android

17 Answered Questions

[SOLVED] "Debug certificate expired" error in Eclipse Android plugins

1 Answered Questions

[SOLVED] Force close onClick of ImageButton, can't figure out why

2 Answered Questions

[SOLVED] Problem Writing Files problem in Android Emulator

  • 2010-11-10 14:07:14
  • RobD
  • 3341 View
  • 1 Score
  • 2 Answer
  • Tags:   java android io

Sponsored Content