R cannot be resolved - Android error

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

androideclipsecompiler-errorsandroid-resourcesandroid-sdk-tools

Answers

answered 9 years ago Josef Pfleger #1

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.

answered 9 years ago Will #2

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, if the problem is recreated than post here again and we'll go into more detail.

answered 9 years ago Maurizio Pozzobon #3

What Will said was right

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, if the problem is recreated than >post here again and we'll go into more detail.

but I've found out that there was another problem that was causing the first one. The tools in the SDK directory didn't have the permissions to be executed, so it was like the didn't exist for Eclipse, thus it didn't build the R.java file.

So modifying the permission and selecting "Build Automatically" solved the problem.

answered 9 years ago Dimitar Dimitrov #4

my project have include a r.java.at the beginning ,R.layout.main work good.But,after adding some code it doesn't work,and the error is R.layout.main can't resolved.what's the problem?

Look at your imports. Chances are that the line:

import android.R;

will be there. If that's the case, remove it, so that your project will resolve R not with the default Android Resources class, but with the one auto-generated from your /res/ folder.

answered 9 years ago Guy Starbuck #5

I had this problem as well. It turned out that I had inadvertently deleted the "app_name" string resource from the strings.xml file, which was causing a silent error. Once I added it back, the R class was generated successfully and everything was back up and running.

answered 8 years ago phil #6

In case anyone is interested (I might be saving your life here), I had the error, R.xml cannot be resolved, slightly different on a GLS project. Hmmmm. After looking in R.java, I found an auto-generated class, XML.java, (I think) was not there.

Solution? It needed a new folder in res: res\xml and a file called default_values.xml in there. Then all was OK.

Just in case you have not got that file, it's:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>

answered 8 years ago Luc #7

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.

answered 8 years ago user286516 #8

R.java is a file that the Android Eclipse plugins creates while building your application. R.java is created under the "gen" directory. This file is generated from the information in the "res" directory. If you run select "Project" -> "Clean..." on the Eclipse menu, it will remove and then regenerate the R.java file.

The problem "R cannot be resolved" happens when you change your package name in the AndroidManifest.xml file. It uses your Android package name to create a subdirectory under the "gen" directory where it stores the R.java file.

Eclipse may have problems executing clean, because it is confused about where the R.java file is when you have changed the Android package name. You can either rename the subdirectory under gen to match your new package name, or you can change your package name back to the old name. Do the clean and then change the package name to the new name you want. This works best if you stop Eclipse from trying to build while you are changing the package name. Under the "Project" menu uncheck the option to "Build Automatically" and also when the "Clean..." dialog asks if it should "Start a build immediately" uncheck the box so it doesn't try to build while you are changing the package name. After you have changed the name you can turn "Build Automatically" back on again.

Note that if your AndroidManifest.xml file package name does not match your Java package name, Eclipse will end up automatically adding an "import <your Android package name>.R;" line in all your .java files that have any references to R. If you change your AndroidManifest.xml package name, sometimes Eclipse does not update all of these added imports. If that happens, use the Eclipse refactoring (ALT + Shift + R) to change the import statement in one of your Java files to your new AndroidManifest.xml package name. It is best to do this while you have disabled "Build Automatically".

answered 8 years ago StrefanA #9

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.

answered 8 years ago TonyS #10

Check the XML file names. Be sure that they're all in lowercase.

Also make sure that any image resource names are also all in LOWER CASE. I had a capital letter in the name of my jpg file, and it caused the R unresolved error right across my project.

answered 8 years ago beppu #11

Yet another reason R.java might not get autogenerated is if you have directories like res/drawable-hdpi, res/drawable-mdpi, or res/drawable-ldpi.

1.6+ seems to be OK with these directories, but 1.5 doesn't want them. When I removed those directories, R.java started autogenerating for me again.

answered 8 years ago Malcom #12

Try to make your new XML layout file name lower case. For example, use my_file.xml instead of myFile.xml.

answered 8 years ago Vasu #13

Whenever you get

R cannot be resolved

then check for the /res directory and there must be some file that have some error in it and that is preventing the application from being built. For example, it may be a layout file or it may be due to some missing resource is, but you already defined it in the XML file.

If you have any additional, even unused (!) or unreferenced (!) images in a folder like res/drawables-mdpi which do not comply to the file naming conventions (may contain only [a-z0-9_.]), the R.java class might not generate, causing the chain of events all the other posts referred to. Hope it helps!

answered 8 years ago akim #14

Simplest solution - Sometimes you just need to save the XML file you were working on to get the autogenerator to kick in.

Save the file (e.g. main.xml) then delete the R.java file and see if the regenerated R.java resolves the R resolve problem.

answered 8 years ago user350617 #15

So I have run into this problem multiple times when switching build targets. Usually doing a Project >> Clean worked for me. This last time, however, it did not. Finally I tried to open my default.properties file, located under the root project folder. I received an error message stating that it was out of sync with the file system. I actually deleted it and copied a coworkers version which allowed eclipse to rebuild my R file. I will paste what it looks like below. It is named 'default.properties'.

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
# 
# This file must be checked in Version Control Systems.
# 
# To customize properties used by the Ant build system use,
# "build.properties", and override values to adapt the script to your
# project structure.

# Indicates whether an apk should be generated for each density.
split.density=false
# Project target.
target=android-3

answered 8 years ago Aaron DeRenard #16

Often times this is because of the MinSDK version number you supplied when creating the project. Example:

If you want 2.1 to be the minimum, Android 2.1 is actually API Level 7.

You can see what I am talking about when you browse the SDK you downloaded and installed. Navigate to the place you installed the SDK to (C:\android-sdk-windows for example) and open the folder named "platforms". You will see something like "android-7" listed as a folder, and if you open that there is a source.properties file that, when opened with a text editor, will show you the corresponding platform version.

When you create a project, and you must select a "Build Target" API, the last column in that list named "API Level" shows the number you are looking for when populating the MinSDK setting.

This is probably one of the most common mistakes that results in the R.java file not being created under Project > gen > packagename > R.java.

answered 8 years ago user132014 #17

I just had this problem for the millionth time and realized what was causing it: I created an XML file with uppercase letters in the name. All your XML filenames in /res must match [a-z0-9\\._].

answered 8 years ago user389061 #18

Remove main.out.xml. I'm new to this and don't yet know what this file is used for, but removing it cleared the problem.

answered 8 years ago Michael Levy #19

After tracking down this problem as well, I found this note in the Android documentation:

http://source.android.com/source/using-eclipse.html

*Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.*

While going through the Android sample tutorials, I would often use the Ctrl + Shift + O command to "Organize Imports" and generate any missing import statements. Sometimes this would generate the incorrect import statement which would hide the R.java class that is automatically generated when you build.

answered 8 years ago Arturski #20

I had the same problem. In my case had to do with the layout XML file names. I had some of the file names with uppercase letters in them:

screenQS.xml - didn't work!

screenqs.xml - worked!

answered 8 years ago mac user #21

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.

answered 8 years ago sharika #22

Close all files, clean project, restart Eclipse.

answered 8 years ago vkris #23

I had the examples of Android 8 and was trying to use Android 7 SDK. When I closed the project and reopened the application folder and chose to use Android 8 SDK, it was able to find the R file. Hope this helps.

answered 7 years ago Eric Cloninger #24

Another way this can occur is if you start a new project from one of the samples. When you later decide to change the package name from com.example.android.foo to your own domain, you will need to modify several values in the manifest and in individual .java files.

If you're in Eclipse, find the package statement for the .java file and choose QuickFix. There may be several choices there, but the one you want is the one that indicates to "Move 'foo.java' to package 'com.youdomain.android.yourapp'. Save the file and it may autobuild or do as others have suggested and try "Project->Clean".

answered 7 years ago user424531 #25

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!

answered 7 years ago Lenn Dolling #26

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

answered 7 years ago Rob #27

This error can also be caused by adding an activity to a namespace that is different to the root namespace for your package.

For example, if com.example.myapp is the root namespace for your package, you can then add an activity to the com.example.myapp.activities namespace.

This will produce the "R cannot be resolved" error.

To fix the import the R in the default namespace in your activity should be:

import com.example.myapp.R;

answered 7 years ago kpax #28

This error cropped up on my x64 Linux Mint installation. It turned out that the result was a failure in the ADB binary, because the ia32-libs package was not installed. Simply running apt-get install ia32-libs and relaunching Eclipse fixed the error.

If your x64 distro does not have ia32-libs, you'll have to go Multiarch.

Check #4 and #5 on this post: http://crunchbang.org/forums/viewtopic.php?pid=277883#p277883

Hope this helps someone.

answered 7 years ago BigUser #29

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

answered 7 years ago seven #30

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.

answered 7 years ago sami #31

My R file disappeared and wouldn't generate - 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_.]

So it turned out that resource names cannot have capital letters - confirm that they are all small letters and contain . and _ only

answered 7 years ago neeraj t #32

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.

answered 7 years ago noob #33

I faced the same problem after putting some graphics in the drawable folder. There was some error and when I tried to clean, the R.java file was not there.

So I carefully checked and found that there was some files with uppercase file names and some files are having the same name but different file extensions. I renamed those files and had a unique name in lowercase for every file. Remember Android cares about the name of the file, not the extension.

Maybe this can help to you or other readers.

answered 7 years ago Noah #34

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.

answered 7 years ago Brian Lacy #35

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.

answered 6 years ago RasTheDestroyer #36

Of the many possible problems that could cause this, this is what fixed it for me.

Right click on project -> properties -> Java Build path-> source.

Then add the gen and src folders. For some reason, Android wanted me to only have the overall folder.

answered 6 years ago CelticParser #37

I had tried all of the above with no prevail. It turned out PhoneGap was causing a conflict with the Android SDK. After uninstalling PhoneGap, the Resources file started regenerating again. Another project, needed PhoneGap so I re-installed, and once again, the Android project (a different project) stopped auto-generating the R file - Build Automatically was checked, did the Clean, and restarted Eclipse - no dice. I removed PhoneGap and it was working once again. This was PhoneGap v 1.5 with the MDS 1.1 plugin for Eclipse.

answered 6 years ago erdomester #38

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.

answered 6 years ago Siddharth #39

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

answered 6 years ago Graham P Heath #40

Make sure if you're looking for R.drawable.* that the file exists in your drawable* folders.

answered 6 years ago Sanket #41

Solution:

Project clean after Eclipse restart. They are auto generated, otherwise you are XML inner any component (like button) after Ctrl + Shift + O. It autogenerated the R file and then the error was solved.

answered 6 years ago laph #42

One shoudn't try to import android.R. Try to fix all errors that appear in XML files, including the manifest file. When all errors are cleared, R.java will reappear.

answered 6 years ago Lei Leyba #43

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.

answered 6 years ago Piotr #44

In my case I was trying to convert my project into Maven. After while(and thousands of random errors which were saying NOTHING) I tried to undo all of operations. What I didn't notice .project file was changed and it wasn't visible inside Eclipse.

Only reverting .project file to before-maven version helped me fixing this error.

answered 6 years ago deadfish #45

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.

answered 6 years ago Mike Fahy #46

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.

answered 6 years ago chek #47

For me it was because of wrong java compiler version. Project Properities -> Java Compiler, enable "Enable project specific settings" and choose your "compiler compliance level"

answered 6 years ago oznus #48

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

answered 6 years ago Pramod Yadav #49

just clean and rebuilt your project it sometimes help

answered 5 years ago Ryhot #50

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.

answered 5 years ago surhidamatya #51

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.

answered 5 years ago satyrFrost #52

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.

answered 5 years ago Brajendra Pandey #53

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.

answered 5 years ago Bernhard Kircher #54

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.

answered 5 years ago CF_Maintainer #55

This worked for me.

I had no 'import android.R' in the brand new project the eclipse wizard created. the default xml layout file was lower case as well.

Problem was eclipse was not generating R.java. No amount of cleaning/restarting was having any effect.

I commented the setContentView and getMenuInflator calls and the make succeeded after which Eclipse generated R.java.

Once R.java was generated, I was able to uncomment setContentView and getMenuInflator.

answered 5 years ago Mayank Saini #56

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+shft+o and select the "packagename.R" import not the other one.

answered 5 years ago Michał K #57

And another thing which may cause this problem:

I installed the new ADT (v. 22). It stopped creating gen folder which includes R.java. The solution was to also install new Android SDK Build Tools from Android SDK Manager.

Solution found here

answered 5 years ago chings228 #58

i tried most method above , but not work my case

finally i found out the adb lost

for mac os , go to android sdk directory

type ./adb start-server

answered 5 years ago Muz #59

Check that the library projects that you've imported into the app are all open.

And for some reason, if you haven't added anything to the activity, like onCreate(), it won't understand what R is.

answered 5 years ago Dawid Drozd #60

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

answered 5 years ago dvrm #61

restart your computer.
Nothing helped me except this way

answered 5 years ago Daniel Macikowski #62

I have got this same problem today. I've tried to perform all sdk update using SDK manager . After finishing and restarting IDE problem has been resolved.

answered 5 years ago nilesh #63

There are two ways that R will an error

1) Problem with your layout file ie xml file

2) If you update adt then the R problem will occure. - you have to check properties for the project - goto->Properties ->Java Build Path -> Order and Export and check for all the lib .

It will solve the problem.

answered 5 years ago Chris Sim #64

first of all look at your gen file if it contains your package name and R.java or no. if it was empty maybe you don't have permission to create new files so exit eclipse than right click, run as administrator and clean your project and everything will be correct.

otherwise you should have an error in your XML file maybe or check the other answers.

answered 5 years ago Gautam Tandon #65

I tried most of the options on this thread (and many others) and nothing worked for me. Finally this one worked so I thought to share it with everyone...

I have multiple versions of Eclipse IDE on my machine (because I also work on non Android related projects). Accidentally I created my Android project using a workspace that was being used by another (non-Android) version of Eclipse. Due to this I guess something weird was going on and my R.java was never getting generated!

To fix this, all I did is created a new workspace and magically R.java started getting generated and everything went smoothly...

So while you are trying all other options, give this one a shot too. And hope it'll work for you...

answered 5 years ago kolaval #66

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

answered 5 years ago YumYumYum #67

This works.

Step 1:

Download https://www.dropbox.com/s/7nwlrg05ke05tat/GalleryImageview.zip

Step 2:

Load eclipse and load the project > Run and Test will work on the fly

Step 3:

Update here if anything is wrong.

Enjoy. Happy coding.

answered 5 years ago Pratik Dasa #68

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.

answered 5 years ago XYZ_deve #69

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.

answered 5 years ago sunil kumar #70

Make sure you installed the Android build tool form sdk manager

project right click properties-> Java BuildPath select Library and add android-support.jar the follow these step.

Go to Project->Properties->Java Build Path than select Order and export tab. Set android-support .jar library checked and up it into top of the list. And clean and rebuild..It works for most of the cases

enter image description here

answered 5 years ago Afaria #71

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

answered 5 years ago Eduardo Dennis #72

You may need to update SDK tools. Relaunch Android SDK Manager again and install a new item: Android SDK Build-tools.

enter image description here

answered 5 years ago Wolfgang Fahl #73

I followed the advice to install the platform build tools. Then The message

<package>/gen already exists but is not a source folder. Convert to a source folder or rename it.

appeared in the console. So I followed the advice and set "gen" as a source folder. This fixed the issue.

answered 5 years ago amuttsch #74

I had the same issue, but couldn't find out what xml file caused the error.

Thanks to this post I installed build tools 18.1.1 instead of 19 and it showed me the defective file. It seems that version 19 doesn't show the actual error.

Hope it helps someone :-)

answered 5 years ago Great Question #75

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.

answered 5 years ago Piyush Mattoo #76

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

answered 5 years ago Mina Gabriel #77

Right click on your project and click Refresh Or hit F5

answered 4 years ago Neha Agarwal #78

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

answered 4 years ago swiftBoy #79

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.

answered 4 years ago Kenji #80

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

answered 4 years ago dhiraj kakran #81

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

answered 4 years ago Tanim reja #82

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 )

answered 4 years ago pestrella #83

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.

answered 4 years ago NewUser #84

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.

answered 4 years ago warmth #85

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

answered 4 years ago user3024292 #86

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!

answered 4 years ago SJX #87

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.

answered 4 years ago Aaron Campbell #88

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.

answered 4 years ago Vaishali Sutariya #89

  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

answered 4 years ago cowlinator #90

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.

answered 4 years ago Mansour Fahad #91

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.

answered 4 years ago Praveen #92

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

answered 3 years ago Akshay Paliwal #93

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.

answered 3 years ago Ajay Pandya #94

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

answered 3 years ago dsharew #95

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

answered 3 years ago 01wifi01 #96

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

answered 3 years ago ethemsulan #97

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

answered 3 years ago ArteQ #98

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

answered 3 years ago DevCat #99

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.

answered 2 years ago Gundu Bandagar #100

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

enter image description here

answered 2 years ago Nazmul Hasan #101

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

answered 2 years ago Shubham #102

Press Alt+enter and Import

or

Ctrl+F9

and solved.

answered 1 year ago Jaimin Prajapati #103

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

answered 1 year ago Rajat Mittal #104

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.

answered 10 months ago Sayan Sil #105

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.

answered 2 months ago Hasib Akter #106

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 powered by Disqus