19

An excerpt from the documentation :

Eclipse sometimes likes to add an import android.R statement at the top of your files that use resources, especially when you ask eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.

My question : WHY? Why does eclipse keep on doing this?

I have been developing Android applications using Eclipse for a quite some time now but I have never been able to understand why eclipse does such a thing.

When I use Ctrl + Shift + O to organize my import statements, import android.R gets added automatically. And all of a sudden my correct code is suddenly covered in red errors, saying that R cannot be resolved. It can get really scary for a beginner as he has no idea what he did wrong.

In another scenario, suppose there is something wrong with my layout files and R.java is not being generated, it says that R cannot be resolved, as R.java has not been generated due to the errors. As I move my cursor to any of the errors, it suggests me to import android.R.

After working on Android for quite sometime now, I know that never to import android.R, but what I have never been able to understand why eclipse keeps on suggesting it, as frankly speaking, adding import android.R never solved any problem of mine. It just added to the existing problems, which used to be really painful during initial days of development.

So, does anyone know the reason behind eclipse making the suggestion to make an incorrect import? Is it just a bug? I don't think it's a bug, as it would have got fixed at least after it was mentioned on the Android documentation.

If it's not a bug, then what is a real purpose of android.R? What does it exactly refer to?

Your opinions/experiences will be really helpful!

Thanks!

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
Swayam
  • 16,294
  • 14
  • 64
  • 102

3 Answers3

12

This is not a bug. There are a few instances where android.R can be helpful and solve problems.

android.R is an R.java file like the one you have in your own projects. The one in your projects (your.packagename.R) holds references to the resources you have under your /res folder like layouts, drawables, XML files, raw files, strings etc.

On the other hand, the android.R file holds references to certain default resources that Android has inbuilt, like simple_list_item_1.

Eclipse suggests this and auto imports this sometimes as if your project's R file hasn't been generated due to an XML error or something, your code will be referencing a file that doesn't exist. By importing android.R, eclipse makes sure your code references a class that exists. However, android.R is unlikely to have the same resources you did, and this will raise another set of errors.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • +1 for mentioning the default android resources. But then again,if I mmay ask, why does eclipse add it when my R.java is already there and I am using Cntrl+Shift+o to organise my imports ? – Swayam Mar 09 '13 at 11:17
  • Maybe suggestions are in alphabetical order, who knows. Probably this is the way the organizer works. – mihail Mar 09 '13 at 11:27
  • @swayam Eclipse *should* only add it when there is an error with your R.java. If it is adding it otherwise, it may be a bug. – Raghav Sood Mar 09 '13 at 11:29
  • One more point to remember is that only the application package will generate the R.java file containing all your project resources. So if you want to refer to these resources from other packages, you have to import yourapppackage.R.java. – faizal Jun 14 '14 at 06:04
  • I've found that when android.R is imported, it usually means there's something wrong with my manifest file or a resource file and therefore my R file hasn't been generated correctly, as the answer above states. – elliptic1 Jan 09 '15 at 19:59
  • When "import android.R" is added in Android Studio, file: MainActivity.kt (.kt is for Kotlin syntax), The code no longer compiles. The import got there after pasting some example code. When "import android.R" removed setContentView(R.layout.activity_main) can resolve the 'activity_main' reference. – flodis Jun 17 '19 at 09:25
3

Eclipse will also always try to automatically import android.R in your class if you rename your package name via android tools and not rename base package name in your code with the same name because he will assume you have two R's in your file.

LilkeMan
  • 31
  • 1
  • My project generates the R.java only for a particular package(the first one i created). In all other packages, I have to import that R class to refer to my layout resources. It seems the first package behaves like a mother package while the others are children. Any idea where this is specified so that i can change this? – faizal Jun 14 '14 at 05:44
  • Just realized it is determined by the package attribute of the manifest tag in the manifest file. – faizal Jun 14 '14 at 05:59
0

Try this...

Window -> Prefs -> Java -> Editor -> Save Actions

Uncheck "Organize Imports." Hopefully that will do it.

After this setting, it works for me.