package R does not exist

I'm getting the dreaded package R does not exist, it's killing me. The code is fine I have apps in the market running the code.

Now it's effecting simple webview applications. o.0

R.string.app_name is error'd my main.xml and string.xml is fine. I'm sooo confused.

I re opened an older version to "borrow" some code and it was flooded with R. errors. I did a clean and nothing changed except for import R.android being added at the top.

I even loaded into Netbeans and I get the same thing. Clean is not working. Can I write the R.java file myself?

What is this R thing? isn't R. supposed to correspond to R.java


For anyone who ran into this, I refactored by renaming the namespace folders. I just forgot to also edit AndroidManifest and that's why I got this error.

Make sure you check this as well.


Just to make this simple:

import your.app.package.R;

Of course, replacing your.app.package with your app package.

In all the classes which use R resource references, remove any other import with .R, i.e. import android.R;


TL;DR, if you get the error "package R does not exist", possible reasons are

  • some error in the XML resource files
    -> fix XML errors
  • the current package is different from the R package (see package attribute in AndroidManifest.xml)
    -> import R class, e.g. import com.example.android.R;
    -> or use the appropriate package in your source, e.g. package com.example.android;
    -> or change the package attribute in AndroidManifest.xml to <manifest xmlns:android="..." package="com.example.android" ...>, if that's appropriate
  • the used R ids are from the system resources
    -> do not import android.R, but prefix the offending ids with android., e.g. android.R.layout.simple_list_item_2
    You may import android.R instead of prefixing the ids of course, but then you cannot import the application R class anymore and must prefix the application ids, e.g. com.example.android.R.id.main_menu.

The R class is generated automatically from the application's resources. It contains the ids for these resources and is contained in the package named in the <manifest> tag in the corresponding AndroidManifest.xml file.

If there are no errors in the resource XML files, the R.java source will be generated in a package subdirectory below gen/ and compiled.


There is another R class located in the android package. This android.R class contains some nested classes, which in turn contain ids and other values for system resources.


To use a class in Java, you must name the class with the whole package, e.g.

java.util.List<Object> list = new java.util.ArrayList<Object>();

or import the class and then use it without package

import java.util.List;
import java.util.ArrayList;
List<Object> list = new ArrayList<Object>();

You can also use a class without naming the package, if both the current class and the used class are in the same package, e.g.

package com.example.android;
public class A {
    /* ... */
}
package com.example.android;
public class B {
    public void useA() {
        A a = new A();
    }
}

Never, ever try to write the R class yourself!

Have you imported the right R class in your files?

Doing

import android.R;

instead of

import com.example.R;

seems to be the problem for a lot of people. After cleaning and building, my classes sometimes import the wrong one.


if you are developing in android studio and refactored the package name then you should change the package name in android manifest and app gradle file.

 applicationId "change here your package name"

and in manifest file

 package="change here your package name"