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?

4
  • 4
    Note that android.R is a completely different class from the.package.name.of.your.app.R Commented Jul 13, 2010 at 4:23
  • hi, I have the same problem. I also create new project from existing source. The source I use is sample code from Android SDK. R is missing. Did you fix it? Commented Jun 10, 2011 at 15:03
  • No, I never actually did. I've never had much luck with Eclipse. Commented Jun 10, 2011 at 22:36
  • @Emerald214: I had the same problem too, and I posted my solution below. Commented Aug 22, 2011 at 5:50

10 Answers 10

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.

Sign up to request clarification or add additional context in comments.

Comments

4
  • Create empty file inside "gen" folder and name it "R.java"
  • click Project --> Clean...
  • click Android Tools --> Fix Project...

Comments

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.

Comments

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.

Comments

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.

Comments

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.

Comments

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.

Comments

1

Try the following:

  • Delete the gen folder
  • Project => clean
  • Right click => Android Tools => Fix project properties
  • Right click => Run as... => Android application

4 Comments

Hmm... None of this works. The gen folder always remains empty.
After deleting it and making a "Fix project properties" what does Eclipse say?
It doesn't say anything.
Where did you get the SpriteMethodTest src code?
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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.