7

I have come across the "R cannot be resolved" error countless times but there has always been an easy fix. Sorry to trouble you but I'm stumped this time...

I recently decided to dual boot my computer into Windows 7/Ubuntu (previously Windows 7 only) and load Eclipse for my Android app development, however I came across the infamous R cannot be resolved error.

After a clean install of Ubuntu 12.10, I set up Google's ADT Bundle from the Android Developer website. I installed the Eclipse, SDK and ADB that were included in the package from the above download (ADT came pre-installed/setup). This is a special version of Eclipse provided by Google and includes the text "Android Developer Tools" when Eclipse is started.

Eclipse runs Java Projects fine, the SDK Manager seems to be set up correctly because I can launch it directly from Eclipse (I have also installed all of Android SDKs 1.5-4.2), AVD Manager almost works correctly (it launches and allows me to create new devices but will not start with the message "PANIC: could not open: some_device"), however any Android project will not compile with the error "R cannot be resolved to a variable".

I have tried using my existing Android projects from two of my old workspaces as well as creating a new workspace but they all resort to the same error.

Any advice? I would really like to begin working in a Linux-based environment and have spent several hours trying to get this to work...

I would highly appreciate anyone's ideas/advice/solutions/pity/etc.

EDIT: I'm starting to think this problem is probably connected to my other error when launching an Android Virtual Device where it states "PANIC: could not open: some_device". Sadly I have no idea what's causing this nor how to fix it either...

feztheforeigner
  • 317
  • 1
  • 4
  • 12

8 Answers8

4

I had the same issue. Fresh install of ADT Bundle, creating a new project using wizard, was giving me "R cannot be resolved to a variable" error. I tried everything explained in previous post.

In my particular case, it was that I had another copy of Android SDK tools and ADT Bundle was pointing to that one, instead of the one that comes with the bundle. I just changed that, and all projects were working again. Hope this helps.

fr4gus
  • 396
  • 7
  • 18
2
sudo apt-get install ia32-libs
Seven Yu
  • 21
  • 3
1

Some quick solutions to your problem might be,

  • 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.
  • 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. Turn it on and build your project.
  • Mistakes in your xml views could cause the R.java not to be generated. Go through your view files and make sure all the xml is right!
  • Remove, if any import android.R;
  • Restart your IDE, if it is just installed.
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • Thanks for the advice, I tried all of this to no avail. These are the solutions I usually try to fix this problem but none of it worked this time around... – feztheforeigner Dec 22 '12 at 11:15
  • see this link http://stackoverflow.com/questions/4932282/android-r-cannot-be-resolved-to-a-variable – moshe beeri May 04 '14 at 22:36
0

There are two solution for this, it will solve your problem.

1) Build your project.

2) Go to Project > Clean and the Clean your project.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
  • Yeah that's what usually fixes this for me but this time it didn't change anything... – feztheforeigner Dec 22 '12 at 11:12
  • @user1923337 If you are creating new android project and run it then there is no R file generated so it will compulsory to build your project manually or clean your project. – Dipak Keshariya Dec 22 '12 at 11:14
0

All other answers are correct, but make sure that any layout don't have any error inside, otherwise eclipse will not generate R.java file. So first try to solve any layout errors and then apply Clean Project and build one more time.

If error comes, remove gen and bin directory and one more time try to build project.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • I've tried using my own working projects as well as creating new ones from the New Android Application Wizard and they all result in the same error. I didn't think of deleting the gen and bin directories though, but that didn't work either. It appears Eclipse is not configured to automatically create an R file. I don't know how to fix that though, since ADT is properly installed (it came with this version of Eclipse baked in and when I tried to add it I was informed it was already installed). – feztheforeigner Dec 22 '12 at 11:21
0

I have had this issue with Eclipse, and it was because I had deleted the bin and gen directories, and Eclipse did not have permissions to recreate those directories. Fixed with chmod.

mjanes
  • 315
  • 2
  • 9
0

Make sure that all the SDKs are updated. In Eclipse go to Help -> Updates and use the SDK Manager to make sure everything is updated.

Jeff Wolski
  • 6,332
  • 6
  • 37
  • 69
-1

Restart your IDE. R - kind of resource handler - will be auto generated.