R.java
is a file that the Android Eclipse plugins creates while
building your application. R.java
is created under the "gen"
directory. This file is generated from the information in the "res"
directory. If you run select "Project" -> "Clean..." on the Eclipse
menu, it will remove and then regenerate the R.java
file.
The problem "R cannot be resolved" happens when you change your
package name in the AndroidManifest.xml
file. It uses your Android
package name to create a subdirectory under the "gen" directory where
it stores the R.java
file.
Eclipse may have problems executing clean, because it is confused about
where the R.java
file is when you have changed the Android package
name. You can either rename the subdirectory under gen
to match your
new package name, or you can change your package name back to the old
name. Do the clean and then change the package name to the new name
you want. This works best if you stop Eclipse from trying to build
while you are changing the package name. Under the "Project" menu
uncheck the option to "Build Automatically" and also when the
"Clean..." dialog asks if it should "Start a build immediately"
uncheck the box so it doesn't try to build while you are changing the
package name. After you have changed the name you can turn "Build
Automatically" back on again.
Note that if your AndroidManifest.xml
file package name does not match
your Java package name, Eclipse will end up automatically adding an
"import <your Android package name>.R;" line in all your .java
files
that have any references to R. If you change your AndroidManifest.xml
package name, sometimes Eclipse does not update all of these added
imports. If that happens, use the Eclipse refactoring (ALT +
Shift + R) to change the import statement in one of your Java files to
your new AndroidManifest.xml
package name. It is best to do this
while you have disabled "Build Automatically".