[android] 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

This question is related to android

The answer is


In my case I realized that I was creating multiple packages in the project. Within the Android Manifest I found that the provider name was incorrectly holding the value of MyContentProvider instead of .provider.MyContentProvider. My main package (with the UI) was co.companyname.database.provider instead of co.companyname.database.

Then on the import statements for the affected class I simply right-clicked and asked Android Studio (0.8.6) to optimize my import statements. I also noted that if you search for .R in the AS search bar at the top right corner of the IDE you can find an auto generated R.java file that is part of your package. At this point you don't have to change anything. Studio should fix up the project after you correct the Android Manifest file and rebuild.

One other item is that in one class I was doing making use of the toString().length() to evaluate a string but swapped those out for TextUtils.IsEmpty (stringVal); Can't think of anything else that I did to resolve the issue.

Hope this helps someone.

NB - All of this is with AS 0.8.6

P.S.

R.java is auto-generated so read the header: /* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */


I run into this problem every time I switch computers and try to import old projects from files transferred via a USB.

My workaround is to just create a new project and copy (drag) the files into the IDE (currently using Netbeans) and rebuild. This is an annoyance and a bit of a hacky solution, but it fixes the problem.

If this happens to you, worst case is you might have to backup your files, delete the project, and just drag the files in there and recompile. Hopefully it won't come to that but that would be your nuclear option.


I had: "error: package R does not exist" and assumed javac didn't have access to R.java.
So I appended %PROJ_LOC%\gen to sourcepath, and it worked! SOURCEPATH=%PROJ_LOC%\src;%PROJ_LOC%\gen

I'm not using Android Studio or Ant (or XML).


Below are some technics which you can use to remove this error:-

  1. Clean your project, Buidl->clean Project
  2. Rebuild project, Build -> Rebuild Project
  3. Open manifest and check is there any resource missing
  4. Check-in layout to particular id if missing add it
  5. If the above steps did not work then restart android studio with invalidating the cache

Below are worked for me.

  1. In AndroidManifest.xml file, check whether the package name is correctly typed.
  2. import com.example.appName.R; (import package _name.R;)

If the first step is not worked try the second one.


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.


The R class is Java code auto-generated from your XML files (UI layout, internationalization strings, etc.) If the code used to be working before (as it seems it is), you need to tell your IDE to regenerate these files somehow:

  • in IntelliJ, select Tools > Android > Generate sources for <project>
  • (If you know the way in another IDE, feel free to edit this answer!)

I forgot to add the "name" of my "package" at the top of my java class. Check if yours is actually added.


Delete import android.R; from all the files.. once clean the the project and build the project.... It will generate


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();
    }
}

I suppose it used to work but now it doesn't..

This can happen if you change PACKAGE name inside your MANIFEST file

<manifest
   package="com.example.android...."

If you want to change package name

  1. Change the package name manually in the manifest file.
  2. Click on your R.java class and the press F6. It will allow you to move the class to other package, and all references to that class will be updated.

I just ran into this error while using Bazel to build an Android app:

error: package R does not exist
                + mContext.getString(R.string.common_string),
                                      ^
Target //libraries/common:common_paidRelease failed to build
Use --verbose_failures to see the command lines of failed build steps.

Ensure that your android_library/android_binary is using an AndroidManifest.xml with the correct package= attribute, and if you're using the custom_package attribute on android_library or android_binary, ensure that it is spelled out correctly.


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.


If this error appeared after resolving merge conflicts, simple Build -> Clean project could help.


When I experienced this, I created a new project, and copied the files from my old project to my new one.

  1. Start a new project
  2. Add the old files into the new project (copy and right click on Layout, drawables,com.packagename,...) to paste my old projects files

However, I still had the same problem: R does not exist. Then I realised that I had copied and pasted Manifest from my notepad into Android Studio manifests and I hadn't changed the package name in the manifest file.

Therefore, once I changed the package name in the manifest to the new project name, the new project I'd created worked fine.


Sometimes, it gets solved when you just re-import the project into Android Studio.


Wrong path: Project "cashes" the pointers to the images and if you have made name changes to the path for example Refactored package or folders. Then just simply write the R letter again and the editor suggests the right path and it will work. In this case your filenames are right and the content that is referred in the R. pointer is present but the path is wrong, thus clean and build won't work.


Sample: My package is com.example.mypc.f01, to fix error you add line below to Activity:

import com.example.mypc.f01.R;


I solve My problem:

package R does not exist

.

Goto AndroidManifest.xml file and changed the minSDKVersion="17" from 19


R.java is an autogenerated file, it usually does not get compiled if you have errors in xml file.

No You cannot write your own R.java file, it has to be generated by the resource compiler.

Check for errors in all your xml files.


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"

If you encounter this error in Android Studio:

Check the Gradle module file(s) -> defaultConfig -> applicationId setting.

Check the AndroidManifest.xml -> manifest package tag.

Check the package declarations in all src java files.

Sync, Clean then Rebuild with Gradle.

NEVER build your own R file. R is auto-generated from your source java files and build settings.


Just got rid of the error in my project. I had imported a sample project, I had changed the package folders but I had not updated the package in one of the java files.

Most likely cause of this error 'package R does not exist' is that the package name in Java or manifest file is incorrect. Make sure it is correct and in sync and then do a clean build.


Another case of R. breakage is Annotations Processing.

Some processing could refer to classes located in certain folders or folder levels. If you move required classes to other location, you can get R. as an unknown class.

Unfortunately the error-pointer to this problem is not evident in Android Studio.


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;


No benefit of writing R.java yourself because it is changed on every build,

Just remember to use R.something for example : R.raw.filename from inside classes that has package value similar to the package value inside the AndroidManifest.xml

because R. resources IDs are defined for the current application or library and they are all checked if exist on compile time, so does not make sense to allow them to be accessed from "code" that does not target the current application or library and can be used in other places.


You may try

Android Studio top menu > File > Invalidate caches / restart...

Click on invalidate cache / restart button to restart your project

Resolve R.xxxx.xxx again if some of the fragment class R class didn't resolve properly

then rebuild your project

Hope it helps


What files are you importing into the files getting the R error?

My understanding of the R file are that they are automatically generated reference lists to all attributes within the Android app. Therefore, you can't change it yourself.

Are you using Eclipse to build this project? Were the older projects getting these errors made before updating Eclipse? What are the references that are getting errors?

Check to make sure that you've not imported another R file from other copied code in another app.