243
votes

In Eclipse, I've created a project from a source and now it shows errors - "R cannot be resolved to a variable". From what I found here, I had cleared and rebuilt the project, but still the R file doesn't appear in the /gen folder.

Any ideas?

30
Before you do anything, check the background processes in eclipse. You will get this message if the workspace is still being built. This is especially relevant for those of us who develop on less powerfull computers.Stephen
If your XML isn't correct, then the R file is not generated after a build.Pramod Setlur
I've checked processes. In fact I'm not using background compile to avoid asych cases but still have the same problem.Kostadin
Sometimes this issue may occur if you haven't install sdk build-tools in androidSachini Samarasinghe

30 Answers

176
votes

Dont worry. First you may clean the project, then run the project. If this does not work then follow the following links:

43
votes

If Clean/Rebuild Project doesn't work try to check our package name in AndroidManifest.xml.

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.

10
votes

For me the error got fixed by making some changes in Android SDK Manager.
Whatever be the latest API level available, install its "SDK Platform". For me latest API level available was 16, so I installed its's SDK Platform as shown in the image below. It works fine now.

Screenshot of Android SDK Manager after fixing the problem
Cheers, Mayank

9
votes

Did you just update both sdk and adt(from 21 to 22), then you need to install a new item: Android SDK Build-tools

Refer to: Eclipse giving error, missing R.java file after recent update

6
votes

For me somehow the Project properties; Android; Project Build Target was not set. I chose a Android version there (e.g. 4.2) and it fixed it.

6
votes

for me, the best solution to suggest to people that have problems with the "R" file would be to try the next steps (order doesn't matter) :

  1. update ADT & SDK , Eclipse and Java.

  2. remove gen folder , and create it again .

  3. do a clean-project.

  4. right click the project and choose android-tools -> fix-project-properties .

  5. right click the project and choose properties -> java-build-path -> order-and-export. make sure the order is :

    • Android 4.3 (always the latest version)

    • Android private libraries

    • android dependencies

    • your library project/s if needed

    • yourAppProject/gen

    • yourAppProject/src

  6. make sure all files in the res folder's subfolders have names that are ok : only lowercase letters, digits and underscore ("_") .

  7. always make sure the targetSdk is pointed to the latest API (currently 18) , and set it in the project.properties file

4
votes

For me, there was a missing slash at the end of my SDK. In Eclipse > Preferences > Android > SDK Location

For example, change

/home/matt/android-sdk-linux

to

/home/matt/android-sdk-linux/
3
votes

I got similar problem and solution I found out was in resources any of the file aka background, sound etc must not contain capital letters or any special symbol other than _

3
votes

your android manifest must start with correct package

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="HERE - Correct package NAME"
    android:versionCode="1"
    android:versionName="1.0" >`
3
votes

What you really need to do it just install "build-tools" from sdk manager, and r.java will be generated automatically.

2
votes

I had the same error. Error was that, I had placed a file in res/raw folder with invalid filename. As soon as I corrected the file name to a valid one, error was resolved!

Allowed characters for naming a file:-

1. a-z
2. . _
3. 0-9
4. no capital letters

Capital letters were the issue in my case!

1
votes

Sometimes you can accidentially add an import to "R", so if at the top of your code you see some weird import about that that you did not add yourself, delete the reference, and everything should go back to normal

1
votes

My problem was strange and took some time to find. Somehow the package of the src file changed so that the last entry in the package was deleted. So for example initially my class MyActivity.java was in package com.abc.client.test.app but after I added a user permission, the app got removed and the package was renamed to com.abc.client.test. I don't know how it happened. Renaming the package and putting the java file in the correct place fixed the problem.

1
votes

I found this problem when trying to run the Notepadv3 programme from Googles 'first lesson'..

For me it was an issue with the xml file, due to the API level I was using. I renamed each case of Match_Parent to the older type of Fill_Parent. Oh and if you have already auto-built the project then you need to delete 'import android.R' on NoteEdit.java and Notepadv3.java, clean the project (click 'Project', then 'clean...') before saving.

Solved it for me. Came from this post Android NDK r4 san-angeles problem

Can't believe Google dosen't warn of this problem - I have been setting up another PC to start programming again and even with a bit of prior knowledge this was a pain.. How do you set the API when importing an existing project??? Can't see where there is any dialogue option when you 'File' 'Import' etc ..

Scamparelli

1
votes

Check the androidmanifest.xml file and layoutfolder xml files. They should be created properly as the starting and ending clause in xml should be placed properly. Update the files, clean and build. And all set!

1
votes

I had this problem and none of the other guides helped, and then I realized I didn't have the java jdk installed on my system. If you haven't done this either go download the version corresponding to the version of eclipse you installed (x86 or x64)

1
votes

It is possible you have an error in your *.xml files: layouts and etc.

1
votes

Try to modify AndroidManifest file.

For example add space and delete this space. After this OPERATION, save project. Resources will be refreshed. It can help.

1
votes

I solved the problem with resolving R resource on fc19.x86_64 and ADT v22.0.5-757759 by installing additional libraries after Fedora and ADT upgrade.

yum install ld-linux.so.2
yum install libstdc++.so.6
yum install libz.so.1

Those libraries are required by adb (Android Debug Bridge version 1.0.31).

Then restart Eclipse and perform project clean Project->Clean check project you would like to clean.

Hope it helps :)

0
votes

Project > Clean...

usually regenerates R.java file located in gen/com.mypackage

0
votes

In my case, R.java wasn't getting generated because of a hierarchical parent error, which in turn was cured by updated one or more Eclipse plugins (Go Eclipse-->Help-->Check for Updates)

0
votes

I got this error when I added a js file to the res folder.

An error indicator was set on the res folder icon. In all the classes where I used R I got this error.

I moved the js file to the assets folder and everything worked.

0
votes

Problem was eclipse was not generating R.java. To resolve this issue please go to project->clean... and select your project and select ok and then clean and build project and import your package name R file(Ex com.demo.R). It works.

0
votes

This can happen when the android naming convention isn't followed. Check if you have placed a file/photo into one of the android folders and make sure that the file name contains only lower case letters

0
votes

I know this is an old question but I just solved my own version of it and perhaps this might help someone.

After two days of tearing my hair out with this, I finally got around it by deleting the raw folder, then recreating it and dropping the file(s) back in.

After that, another Project > Clean and it at last compiled.

0
votes

I got this when I renamed a project. Although the project is renamed, some references are not. Clearing lint, and then rerunning lint helped. (Right click project > Android Tools > Clear Lint Markers, then Run Lint)

0
votes

My setup was broken after a recent update where the SDK build tools need to be installed separately after the SDK installation. So build tools update + Eclipse restart fixed this for me.

Credit goes to the Android reply here

0
votes

When other solutions fail, select your project and delete it. MAKE SURE YOU DO NOT REMOVE FILES FROM DISK.

Then use file>import>existing android code into workspace, and select the location of your project.

-1
votes

I been having this and i did trace it back to the layout files.. It happend to me again however i could not find noting in the layout files. I looked in the string.xml and there was it.. one of the string names started with cap and that was causing the problem.

-1
votes

Fix any XML formatting errors in the XML Files in your /res/menu folder.

Might be a compile time error from the XML file being improperly formatted.