244

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?

MiguelHincapieC
  • 5,445
  • 7
  • 41
  • 72
Roger
  • 6,443
  • 20
  • 61
  • 88
  • 1
    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 Oct 16 '12 at 18:24
  • 1
    I've checked processes. In fact I'm not using background compile to avoid asych cases but still have the same problem. – Kostadin May 16 '13 at 12:38
  • Sometimes this issue may occur if you haven't install sdk build-tools in android – Sachini Samarasinghe Nov 22 '13 at 06:56
  • 6
    If your XML isn't correct, then the R file is not generated after a build. – Pramod Setlur Mar 06 '13 at 11:08

30 Answers30

175

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

Community
  • 1
  • 1
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
  • 11
    Yes, thanks! Appears there was also some error in the XML files, once I fixed that, clean/build worked! :) – Roger Oct 19 '11 at 17:08
  • 1
    Those are fun to find, huh? :) If you can't get `R.java` to build in the first place, it's usually a typo or other problem in an XML layout file. Using a capital letter in your XML filenames will also cause that problem. If `R.java` does exist but variables in it aren't recognized by just one Java file, for instance, note whether that file is in the same package as `R.java`--if not, that's why `R.id.*` is out of scope. – hotshot309 Jan 04 '12 at 02:19
  • 136
    You should add the answer to the answer, not links somewhere else because those links may die. – Jonny Dec 04 '12 at 04:22
  • 1
    If you change your package or activity name, delete the folders, in the "gen" folder or rename the packages in the gen folder and clean the project. – Luke Snowden Mar 28 '13 at 09:11
  • i found error in my layout xml like this : android:id="@id/" – onder Jun 18 '13 at 06:48
  • Make sure you don't have duplicate names too, e.g. "file1.dat" and "file1.bak" won't work !!! –  Dec 04 '13 at 19:47
  • To anyone who has this "R error": 90% of the time this means you have an error in your XML probably, so make sure to look up there! – Placeholder Sep 02 '14 at 12:10
  • The first link helped - only lowercase letters in resource names! – Amber Apr 29 '15 at 20:41
43

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.

robbycandra
  • 770
  • 7
  • 6
  • I changed the package name in the source folder, in the xml manifest was it correct, but not on the "start screen" of the manifest. thx – Gaeburider Nov 09 '13 at 12:55
  • If you change the package name in the xml you will have to re-import the R file from the new package – Oded Regev Apr 17 '14 at 15:37
10

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

Mayank Jaiswal
  • 12,338
  • 7
  • 39
  • 41
9

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

Community
  • 1
  • 1
macio.Jun
  • 9,647
  • 1
  • 45
  • 41
6

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.

Phileas Fogg
  • 151
  • 2
  • 4
  • This seems to be quite an issue if you get some code from a third-party and they don't tell you the API level. For anyone who is experiencing this problem I would ask them to check the API level is appropriate for the code. If the API level is not correct, you will need to change the Project Build Target to the correct API level and do a project clean. – Exile Jun 14 '13 at 10:24
  • 1
    In case you are importing a project from an earlier android project build target to an env with latest build tools, your project target is set to the earlier one by import. Selecting project build to recent android version availble on you dev env will solve the issue. Ex: For me, by import project build target was being set to Android 4.4.2. This was giving the error. I changed it to Android 5.0.1 and error got fixed. – Dexter Jan 22 '15 at 06:36
6

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

android developer
  • 114,585
  • 152
  • 739
  • 1,270
4

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/
matt burns
  • 24,742
  • 13
  • 105
  • 107
4

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" >`
  • I've been stuck on this problem for a few days now, this is the first time anyone addressing a problem in the manifest. Thanks! – scf Mar 24 '14 at 17:50
  • This solved my problem. Thanks. So if you changed your package, be sure to change it in the manifest file too. This problem is very misleading. – LEMUEL ADANE Feb 04 '15 at 06:26
3

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 _

Gray
  • 115,027
  • 24
  • 293
  • 354
vCillusion
  • 1,749
  • 20
  • 33
3

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

AJay
  • 1,233
  • 10
  • 19
2

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!

Shishir Gupta
  • 1,512
  • 2
  • 17
  • 32
1

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

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

Community
  • 1
  • 1
Scamparelli
  • 756
  • 1
  • 12
  • 28
1

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!

Eva Dias
  • 1,709
  • 9
  • 36
  • 67
Riddhi
  • 61
  • 1
  • 7
1

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

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

Vlad
  • 3,465
  • 1
  • 31
  • 24
1

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.

Nunser
  • 4,512
  • 8
  • 25
  • 37
1

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

Stepan Hruska
  • 151
  • 1
  • 6
1

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

Keerigan
  • 1,182
  • 1
  • 9
  • 17
0

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)

Community
  • 1
  • 1
Arsene Lupin
  • 343
  • 2
  • 11
0

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.

tavi
  • 594
  • 6
  • 15
0

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.

Enli
  • 209
  • 2
  • 5
0

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

MrT
  • 1
0

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.

Robert
  • 5,278
  • 43
  • 65
  • 115
0

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)

powlo
  • 2,538
  • 3
  • 28
  • 38
0

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

Community
  • 1
  • 1
Mario Peshev
  • 1,063
  • 8
  • 16
0

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.

Nino van Hooff
  • 3,677
  • 1
  • 36
  • 52
0

Project > Clean...

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

Kamil Szot
  • 17,436
  • 6
  • 62
  • 65
-1

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.

JFrogz
  • 1
-1

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.

Gene
  • 10,819
  • 1
  • 66
  • 58