12

I downloaded the source for SpriteMethodTest, and I want to build it in Eclipse. So I Went:

File >> New >> Android Project >> Create Project From Existing Source >> SpriteMethodTest

It created the project alright, but the R class is not generated. Any file that references a resource in R says R cannot be resolved.

Importing android.R just results in R.drawable.background cannot be resolved. How do I generate R again?

cstack
  • 2,152
  • 6
  • 28
  • 47

10 Answers10

5

You are having an issue because you are importing Androids R, not your project specific R. Simply delete the import android.R;, then hover your mouse over the reference to the R that is giving the R cannot be resolved to a variable. From there you will have possibly a few different options on which R to import. You want to import the R that is in your package.

Tyler
  • 19,113
  • 19
  • 94
  • 151
4

The generated R resource file can only be generated by eclipse if the files it reads can compile, meaning errors in a file (such as the manifest file) prevent compilation. So, resolve your errors (listed in the 'Problems' tab) first, then save the changes and it should then be generated.

The reason it usually seems to be related to API versions is because different versions cause compatibility errors. This usually happens when people grab sample code and don't set the same API version as the code creator was using. Changing to the correct version resolves the errors and allows the R file to be generated.

Sogger
  • 15,962
  • 6
  • 43
  • 40
4
  • Create empty file inside "gen" folder and name it "R.java"
  • click Project --> Clean...
  • click Android Tools --> Fix Project...
draq
  • 41
  • 2
2

In my enviroment android SDK was missing the folder platform-tools and I had to start android and install platform-tools. Now it is working.

2

I had this exact same problem with SpriteMethodTest. It has nothing to do with package names or imports. As tarkeshwar says, if R is not being generated, then the problem is prior to compilation. In this case it's AndroidManifest.xml.

The last tag in the manifest is

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="4"/>

but android:targetSdkVersion is not a supported attribute. Change the line to

<uses-sdk android:minSdkVersion="3"/>

and that should allow Eclipse to generate the R class.

Steve Blackwell
  • 5,904
  • 32
  • 49
2

Had a similar problem, which seemed to be caused by the SDK versions as well:

  1. Project -->properties --> android: select the suitable API lvl and click OK. That caused the R class to finally be generated, how ever it will not compile yet.
  2. Delete the project and import it again (now it has the R class) and worked on first attempt.

*Happened when tried to check out the snake example code.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Nim
  • 21
  • 1
1

Running this in the project folder fixed it for me

 android update project -p .

I had "Build automatically" turned off when I created the new project, so certain files weren't created like they should have been

msEmmaMays
  • 1,073
  • 7
  • 7
1

Project -> Clean (select your project) -> Ok

This will trigger a re-build and so long as the project is configured as an Android project, R.java will be regenerated.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
1

Try the following:

  • Delete the gen folder
  • Project => clean
  • Right click => Android Tools => Fix project properties
  • Right click => Run as... => Android application
Macarse
  • 91,829
  • 44
  • 175
  • 230
1

R.java not being generated usually indicates problems elsewhere, for example: Manifest.xml having errors.

Fix those errors and rebuild. R.java should get generated then.

If if it still not there, do project -> android -> fix android settings.

tarkeshwar
  • 4,105
  • 4
  • 29
  • 35