15

I downloaded few source codes from Google source codes.

I executed them on Eclipse but R.java file is not created and therefore I got many errors saying that R cannot be resolved. There is no main.xml in layout too. Some other XML files are there. How can I execute these programs?

Jonas
  • 121,568
  • 97
  • 310
  • 388
shashi
  • 159
  • 1
  • 2
  • 5
  • 3
    There are many similar questions on Stackoverflow. Please do a search, and read through the basic documentation. No Main.xml is not required. http://stackoverflow.com/questions/4516428/r-java-auto-generated-file http://stackoverflow.com/questions/2757107/developing-for-android-in-eclipse-r-java-not-generating – Cheryl Simon Jan 06 '11 at 19:55
  • 1
    FWIW, neither of those questions were answered entirely correctly. Eclipse can get itself in to a state where it does not generate an R.java file. In fact, my R.java was *removed* by the Project->Clean operation suggested by folks there. –  Feb 24 '11 at 04:32
  • Yes, clean will remove R.java but the next time you build it will be recreated. If it isn't, you might try exiting Eclipse and then going back in; sometimes Eclipse gets confused. – Scott C Wilson Apr 28 '11 at 20:45
  • What sources did you download? And copy-paste exact error messages. – Gilles 'SO- stop being evil' Sep 20 '11 at 08:15
  • IT REALLY freaks me out at the moment. Why isn't there a straight forward solution for this problem. I can't build the Project because of the R line errors, so no way it will be generated. – seb Aug 17 '12 at 20:34
  • If you have a number as a 1st character in a filename (e.g. an image resource), R.java will fail to generate until you change the name to something that starts with a letter. – alexismorin Nov 27 '12 at 21:39

11 Answers11

19

As long as you have a valid Android project, you can use aapt package to generate the R.java from resources. Here's example usage (all should be in one line):

aapt package --non-constant-id -f -m -M <abs_path_to_AndroidManifest.xml> 
  -S <abs_path_to_res_dir> -I <abs_path_to_platforms_android.jar> 
  -J <abs_path_to_dir_that_should_contain_gen_R_java) 
  --generate-dependencies

The above can be made scriptable using ANT by using the aapt target or by simply using an exec target.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
justadev
  • 1,049
  • 9
  • 10
  • 1
    Funny - generated it once like this, since then it also works in Eclipse. And yes, I know I should be using Android Studio but I don't want... – Torsten Römer Nov 27 '17 at 16:47
7

Presume you have Android sdk. If you have.. just clean the project. R.java will be autogenerated.

GSree
  • 2,890
  • 1
  • 23
  • 25
  • 3
    Cleaning the project deletes the R.java file from the project. – Navigatron May 08 '11 at 15:06
  • @Nuktus In theory yes. That is the way it is supposed to be. In practise(eclipse), you might see the R.Java is still present after cleanup. Worst of all, sometimes the import statement in the classes, point to incorrect R.Java (eg. from the library project) – GSree May 09 '11 at 03:27
  • That's probably because you have autocompile turned on. You clean everything, then autocompile regenerates it. – Edwin Buck Apr 13 '15 at 11:23
3

Hi I just faced the same problem. The file R.java is deleted an regenerated each time you clean the project. In my case there were some errors in one of .xml files and no R.java was generated. If you did not have Main.xml that might be the reason. anyway if you fix .xml problems R.java should be generated.

Filippo
  • 31
  • 1
2
  • Open Android SDK Manager
  • Tick Tools that there are 3 packages of Android SDK Tools , delete and install them again.
  • When they are finished, please refresh your project in Package Explorer.

You will see BuildConfig.java and R.java in /gen

I think you wanna need that I recommend this comment

Enjoy with your building app.

Andrei Sfat
  • 8,440
  • 5
  • 49
  • 69
  • To be precise, aadt is part of "Android SDK build-tools". Older sdks shipped it in the per-api-level "SDK platform" package. – Tobu May 19 '14 at 13:06
1

After few hours frustration finally I have decide to remove the red marks from the my file where it was not able to resolve the R.java as it was not actually available in my code. After removing the first red line from my activity file the project builds and generated the R.java automatically.

This is something really freaking thing for me as it goes without any reason and came back with the same thing.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Summved Jain
  • 872
  • 2
  • 18
  • 21
0

For future readers, restart adb then clean.

MikeC
  • 725
  • 8
  • 19
0

Correct all errors in the Xml layouts, if the errors is only in the id attribute, change to other name an save, then type the original values, this solution works for me.

0

I encountered this issue after adding a jpg file with a numeric file name into my drawable folder. I was getting a compilation error, " expected on line y" when trying to rebuild the project after cleaning it. You may want to check that you do not have a similar issue. Although I encountered this while using Android Studio, I am sure it would affect Eclipse as well.

Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
0

Cleaning the project, delete the R.java and regenerate it. If you have an error anywhere in your /res file (the resources) , the R will not be generated usually. It could be an error in the name of a picture , or anything ... fix your errors before. good luck ;)

ahmed_khan_89
  • 2,755
  • 26
  • 49
0

I have encountered similar problem and i changed the workspace folder and all errors disappeared. It's sort of simple solution but it works.

Anıl
  • 136
  • 5
0

If your build.xml includes the "-resource-src" target (which it will if you include $SDK_DIR/tools/main_rules.xml or lib_rules.xml) you can rebuild it with ant.

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83