R cannot be resolved - Android error

AndroidEclipseCompiler ErrorsAndroid ResourcesAndroid Sdk-Tools

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Solution 3 - Android

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!

Solution 4 - Android

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

Solution 5 - Android

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

Solution 6 - Android

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.

Solution 7 - Android

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

Solution 8 - Android

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.

Solution 9 - Android

Close all files, clean project, restart Eclipse.

Solution 10 - Android

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.

Solution 11 - Android

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;

Solution 12 - Android

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.

Solution 13 - Android

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

Solution 14 - Android

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\\._].

Solution 15 - Android

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.

Solution 16 - Android

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.

Solution 17 - Android

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.

Solution 18 - Android

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.

Solution 19 - Android

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

Solution 20 - Android

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.

Solution 21 - Android

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

Solution 22 - Android

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.

Solution 23 - Android

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

Solution 24 - Android

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.

Solution 25 - Android

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.

Solution 26 - Android

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

enter image description here

Solution 27 - Android

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

Solution 28 - Android

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>

Solution 29 - Android

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.

Solution 30 - Android

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

Solution 31 - Android

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.

Solution 32 - Android

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

Solution 33 - Android

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.

Solution 34 - Android

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.

Solution 35 - Android

restart your computer.
Nothing helped me except this way

Solution 36 - Android

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.

Solution 37 - Android

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!

Solution 38 - Android

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

Solution 39 - Android

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.

Solution 40 - Android

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.

Solution 41 - Android

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.

Solution 42 - Android

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.

Solution 43 - Android

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.

Solution 44 - Android

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!

Solution 45 - Android

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

Solution 46 - Android

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

Solution 47 - Android

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.

Solution 48 - Android

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.

Solution 49 - Android

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

Solution 50 - Android

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.

Solution 51 - Android

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.

Solution 52 - Android

  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.

Solution 53 - Android

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.

Solution 54 - Android

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.

Solution 55 - Android

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.

Solution 56 - Android

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

Solution 57 - Android

just clean and rebuilt your project it sometimes help

Solution 58 - Android

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

Solution 59 - Android

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

Solution 60 - Android

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

Solution 61 - Android

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

Solution 62 - Android

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 )

Solution 63 - Android

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.

Solution 64 - Android

  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

Solution 65 - Android

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.

Solution 66 - Android

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.

Solution 67 - Android

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

Solution 68 - Android

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

Solution 69 - Android

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.

Solution 70 - Android

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

Solution 71 - Android

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!

Solution 72 - Android

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

Solution 73 - Android

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

Solution 74 - Android

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.

Solution 75 - Android

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.

Solution 76 - Android

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.

Solution 77 - Android

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

Solution 78 - Android

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.

Solution 79 - Android

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.

Solution 80 - Android

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.

Solution 81 - Android

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"

Solution 82 - Android

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.

Solution 83 - Android

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.

Solution 84 - Android

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

Solution 85 - Android

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.

Solution 86 - Android

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.

Solution 87 - Android

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.

Solution 88 - Android

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.

Solution 89 - Android

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

Solution 90 - Android

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.

Solution 91 - Android

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.

Solution 92 - Android

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.

Solution 93 - Android

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

Solution 94 - Android

Right click on your project and click Refresh Or hit F5

Solution 95 - Android

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

Solution 96 - Android

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

Solution 97 - Android

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.

Solution 98 - Android

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.

Solution 99 - Android

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.

Solution 100 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaurizio PozzobonView Question on Stackoverflow
Solution 1 - AndroidMichael LevyView Answer on Stackoverflow
Solution 2 - AndroidLucView Answer on Stackoverflow
Solution 3 - AndroidVasuView Answer on Stackoverflow
Solution 4 - AndroidDimitar DimitrovView Answer on Stackoverflow
Solution 5 - AndroidMichał KlimczakView Answer on Stackoverflow
Solution 6 - AndroidMaurizio PozzobonView Answer on Stackoverflow
Solution 7 - Androiduser286516View Answer on Stackoverflow
Solution 8 - AndroidWillView Answer on Stackoverflow
Solution 9 - AndroidsharikaView Answer on Stackoverflow
Solution 10 - AndroidgreenView Answer on Stackoverflow
Solution 11 - AndroidRobView Answer on Stackoverflow
Solution 12 - AndroidsatyrFrostView Answer on Stackoverflow
Solution 13 - AndroidSunil KumarView Answer on Stackoverflow
Solution 14 - Androiduser132014View Answer on Stackoverflow
Solution 15 - AndroidakimView Answer on Stackoverflow
Solution 16 - AndroidJosef PflegerView Answer on Stackoverflow
Solution 17 - AndroidTonySView Answer on Stackoverflow
Solution 18 - AndroidkpaxView Answer on Stackoverflow
Solution 19 - AndroidNazmul HasanView Answer on Stackoverflow
Solution 20 - AndroidGuy StarbuckView Answer on Stackoverflow
Solution 21 - AndroidEduardo DennisView Answer on Stackoverflow
Solution 22 - AndroidbeppuView Answer on Stackoverflow
Solution 23 - AndroidMalcomView Answer on Stackoverflow
Solution 24 - AndroidAaron DeRenardView Answer on Stackoverflow
Solution 25 - Androiduser389061View Answer on Stackoverflow
Solution 26 - AndroidGundu BandgarView Answer on Stackoverflow
Solution 27 - AndroidJaimin PrajapatiView Answer on Stackoverflow
Solution 28 - AndroidphilView Answer on Stackoverflow
Solution 29 - AndroidStrefanAView Answer on Stackoverflow
Solution 30 - Androiduser350617View Answer on Stackoverflow
Solution 31 - AndroidvkrisView Answer on Stackoverflow
Solution 32 - AndroidBigUserView Answer on Stackoverflow
Solution 33 - AndroidBrian LacyView Answer on Stackoverflow
Solution 34 - AndroidBernhard KircherView Answer on Stackoverflow
Solution 35 - AndroiddvrmView Answer on Stackoverflow
Solution 36 - AndroidswiftBoyView Answer on Stackoverflow
Solution 37 - Androiduser3024292View Answer on Stackoverflow
Solution 38 - AndroidethemsulanView Answer on Stackoverflow
Solution 39 - AndroiderdomesterView Answer on Stackoverflow
Solution 40 - AndroidMayank SainiView Answer on Stackoverflow
Solution 41 - AndroidXYZ_deveView Answer on Stackoverflow
Solution 42 - AndroidSheikh HasibView Answer on Stackoverflow
Solution 43 - Androidmac userView Answer on Stackoverflow
Solution 44 - Androiduser424531View Answer on Stackoverflow
Solution 45 - AndroidLenn DollingView Answer on Stackoverflow
Solution 46 - AndroidSiddharthView Answer on Stackoverflow
Solution 47 - AndroidRyhotView Answer on Stackoverflow
Solution 48 - AndroidBrajendra PandeyView Answer on Stackoverflow
Solution 49 - AndroidkolavalView Answer on Stackoverflow
Solution 50 - AndroidPratik DasaView Answer on Stackoverflow
Solution 51 - AndroidDevCatView Answer on Stackoverflow
Solution 52 - AndroidMEGHA DOBARIYAView Answer on Stackoverflow
Solution 53 - AndroidNoahView Answer on Stackoverflow
Solution 54 - AndroiddeadfishView Answer on Stackoverflow
Solution 55 - AndroidMike FahyView Answer on Stackoverflow
Solution 56 - AndroidoznusView Answer on Stackoverflow
Solution 57 - AndroidPramod YadavView Answer on Stackoverflow
Solution 58 - AndroidGelldurView Answer on Stackoverflow
Solution 59 - AndroidAfariaView Answer on Stackoverflow
Solution 60 - AndroidamuttschView Answer on Stackoverflow
Solution 61 - AndroidMahdiView Answer on Stackoverflow
Solution 62 - AndroidTanim rejaView Answer on Stackoverflow
Solution 63 - AndroidpestrellaView Answer on Stackoverflow
Solution 64 - AndroidVaishali SutariyaView Answer on Stackoverflow
Solution 65 - AndroidcowlinatorView Answer on Stackoverflow
Solution 66 - AndroidMansour FahadView Answer on Stackoverflow
Solution 67 - AndroidPraveenView Answer on Stackoverflow
Solution 68 - Android01wifi01View Answer on Stackoverflow
Solution 69 - AndroidSayan SilView Answer on Stackoverflow
Solution 70 - AndroidAshifView Answer on Stackoverflow
Solution 71 - AndroidArturskiView Answer on Stackoverflow
Solution 72 - AndroidEric CloningerView Answer on Stackoverflow
Solution 73 - AndroidsamiView Answer on Stackoverflow
Solution 74 - AndroidnoobView Answer on Stackoverflow
Solution 75 - AndroidRasTheDestroyerView Answer on Stackoverflow
Solution 76 - Androiduser742030View Answer on Stackoverflow
Solution 77 - AndroidGraham P HeathView Answer on Stackoverflow
Solution 78 - AndroidSanketView Answer on Stackoverflow
Solution 79 - AndroidlaphView Answer on Stackoverflow
Solution 80 - AndroidPiotrView Answer on Stackoverflow
Solution 81 - AndroidchekView Answer on Stackoverflow
Solution 82 - AndroidsurhidamatyaView Answer on Stackoverflow
Solution 83 - AndroidCF_MaintainerView Answer on Stackoverflow
Solution 84 - Androidchings228View Answer on Stackoverflow
Solution 85 - AndroidMuzView Answer on Stackoverflow
Solution 86 - AndroidDaniel MacikowskiView Answer on Stackoverflow
Solution 87 - AndroidnileshView Answer on Stackoverflow
Solution 88 - AndroidChris SimView Answer on Stackoverflow
Solution 89 - AndroidGautam TandonView Answer on Stackoverflow
Solution 90 - Androiduser285594View Answer on Stackoverflow
Solution 91 - AndroidWolfgang FahlView Answer on Stackoverflow
Solution 92 - AndroidGreat QuestionView Answer on Stackoverflow
Solution 93 - AndroidPiyush MattooView Answer on Stackoverflow
Solution 94 - AndroidMina GabrielView Answer on Stackoverflow
Solution 95 - AndroidNeha AgarwalView Answer on Stackoverflow
Solution 96 - Androiddhiraj kakranView Answer on Stackoverflow
Solution 97 - AndroidNewUserView Answer on Stackoverflow
Solution 98 - AndroidSJXView Answer on Stackoverflow
Solution 99 - AndroidAaron CampbellView Answer on Stackoverflow
Solution 100 - AndroidAkshay PaliwalView Answer on Stackoverflow