In the Google group Android Beginners I frequently see messages that ask what the error "r cannot be resolved" means in Eclipse.
Eclipse generates the file R.java
for you using the aapt tool (Official Guide to the Android Asset Packaging Tool). R.java
contains a mapping to all the resources your application will use. Note that you should have the Build Automatically
option under the Project
menu checked on.
If this happens to you, make sure the files under res/
are valid (make sure they are not marked invalid in Eclipse). Since aapt
generates R.java
from the files in res/
if those files are mucked aapt
can’t work.
An example of an aapt generated R.java
file:
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.likethecolor.android.helloandroid; public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f040001; public static final int hello=0x7f040000; } }
While I use Eclipse for my Android development it is always good to know how things work behind the scenes. I’ve tried to find a more detailed description of how aapt
creates R.java
but can find nothing more than a vague description:
From: http://developer.android.com/guide/tutorials/notepad/notepad-ex1.html
The folders under
res/
in the Eclipse project are for resources. There is a specific structure to the folders and files underres/
. Resources defined in these folders and files will have corresponding entries in theR.class
allowing them to be easily accessed and used from your application. TheR.class
is automatically generated using the contents of theres/
folder by the eclipse plugin (or by aapt if you use the command line tools). Furthermore, they will be bundled and deployed for you as part of the application.
This is how one might run aapt
from command line (note that this command must be run from the top level of your project):
aapt package -m -J src -M AndroidManifest.xml -S res -I /platforms/android-2.0/android.jar
While doing a clean project and build project has always worked for me, this site suggested clicking on "Fix Project Properties" which appears under "Android Tools" when right/ctrl-clicking on the project in the package explorer.
Another possibility is that when creating a new source file your IDE (e.g., eclipse) may have added
import android.R;
That may be fine if you actually have a package called android
in which there is a class R
but that’s probably not the case. Removing that import
would solve the problem.
1 Comment