Home » Android » eclipse – R cannot be resolved – Android error

eclipse – R cannot be resolved – Android error

Posted by: admin March 10, 2020 Leave a comment


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. */
    public void onCreate(Bundle savedInstanceState) {

but Eclipse gives me the error

R cannot be resolved

on line



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

How to&Answers:

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


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


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.


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!


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.


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


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


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.


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.


Close all files, clean project, restart Eclipse.


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"

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


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;


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.


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


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


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.


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.


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.


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:

Hope this helps someone.


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


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


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.


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


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.


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


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.


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.


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

enter image description here


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.


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.


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