Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R class missing after creating android project from existing source [duplicate]

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?

like image 973
cstack Avatar asked Jul 12 '10 21:07

cstack


6 Answers

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.

like image 160
tyler Avatar answered Nov 13 '22 08:11

tyler


  • Create empty file inside "gen" folder and name it "R.java"
  • click Project --> Clean...
  • click Android Tools --> Fix Project...
like image 4
draq Avatar answered Nov 13 '22 09:11

draq


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.

like image 4
Sogger Avatar answered Nov 13 '22 08:11

Sogger


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.

like image 2
Steve Blackwell Avatar answered Nov 13 '22 10:11

Steve Blackwell


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.

like image 2
Raphael Villas Boas Avatar answered Nov 13 '22 10:11

Raphael Villas Boas


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.

like image 2
Nim Avatar answered Nov 13 '22 09:11

Nim