Seeing “R cannot be resolved to a variable,” is one of the most annoying and recurring problems when developing in Android. Below are the problems I’ve run into and their solutions.

No problems; everything in working order:

HoverDroids-Android-R-Cannot-Be-Resolved-Eclipse-1

Let’s start with a working project (picture above). Notice the following:

  • src/package name is com.example.project
  • gen/package name is EXACTLY the same
  • MainActivity is in src/com.example.project
  • While src and gen are different folders, they have the same packages and any files within the same package can see any other file in that package without having to import the class. Consequently, MainActivity can automatically see R.java and can therefor call R.id.container without errors.

Problem 1: Bad path single package

HoverDroids-Android-R-Cannot-Be-Resolved-Eclipse-2

The image above shows the result of a bad path from MainActivity to R.java. Notice the following:

  • red x on Project Folder, src folder, com.app.project package, and MainActivity.java in the package explorer on the left
  • no red x on any folders or files in the gen folder.
  • Clearly this means the root of the problem is in MainActivity and not with R.java
  • Looking at MainActivity on right side, the R. is underlined in red for any reference to R.java (e.g. R.layout.activity_main and R.id.container)
  • MainActivity is in com.app.project package while R.java is in com.example package, so it cannot automatically see R.java. Since it cannot automatically see R.java it must provide the path to R.java but hasn’t done so using import.

Solution 1:

The first solution is to rename one of the packages to that of the other. When I run into this problem it’s because I renamed my main package and forgot to rename the package in /gen. So, rename /gen/com.example.project by opening the manifest file as shown below. This should automatically rename the package in the package explorer. If it doesn’t, just delete the entire /gen folder and let android recreate it.

HoverDroids-Android-R-Cannot-Be-Resolved-Eclipse-3

Solution 2:

When you don’t want to rename the package in /gen (Problem 2 is a good example), then it’s required that MainActivity has the following line: import com.example.project.R;

HoverDroids-Android-R-Cannot-Be-Resolved-Eclipse-4

Problem 2: Bad path multi-package

HoverDroids-Android-R-Cannot-Be-Resolved-Eclipse-5

The image above shows the result of a bad path from SecondaryActivity to R.java even though MainActivity isn’t experiencing the same problem. Notice the following:

  • red x on Project Folder, src folder, com.app.project.secondaryActivity package, and SecondaryActivity.java in the package explorer on the left
  • no red x on any folders or files in the gen folder.
  • Clearly this means the root of the problem is in SecondaryActivity and not with R.java
  • Looking at SecondaryActivity on right side, the R. is underlined in red for any reference to R.java (e.g. R.layout.activity_main and R.id.container)
  • SecondaryActivity is in com.app.project.secondaryActivity package while MainActivity and R.java are in com.app.project package, so it cannot automatically see R.java. Since it cannot automatically see R.java it must provide the path to R.java but hasn’t done so using import.

Solution:

Use Solution 2 from Problem 1.

Problem 3: Wrong path specified… android.R

HoverDroids-Android-R-Cannot-Be-Resolved-Eclipse-6

The image above shows the result of android.R being imported rather than com.app.project.R Notice the following:

  • red x on Project Folder, src folder, com.app.project package, and in the package explorer on the left
  • no red x on any folders or files in the gen folder.
  • Clearly this means the root of the problem is in MainActivity and not with R.java
  • Looking at MainActivity on right side, the R. is NOT underlined while activity_main and container are
  • MainActivity is in com.app.project package and so is R.java, so no need to import anything since MainActivity can already see R.java
  • Problem is that the code thinks R.layout.activity_main and R.id.container should be in android.R and not com.app.R because of import android.R

Solution:

Delete import android.R

Note: When using auto-complete (e.g. hitting spacebar during R.layout.cont to get R.layout.container), unexpected suggestions appear. This is a result of importing android.R so delete it and the correct suggestions should appear again.

Problem 4: No R.java doesn’t exist after cleaning

First ensure that the problem isn’t a path issue as described above. If all paths are correct then try to select Project Tab->Clean ->Name of project.

If /Gen/com.app.project/R.java does not immediately reappear then an xml file or manifest file have an error – even if Eclipse doesn’t show it! So, go through recently changed files and make sure everything stacks up. Here are some common problems:

  • Didn’t close a view. Every view needs to start and close. For example or
  • Missing quotes. Very common the second quote gets deleted and results in something like
  • Bad drawable names or xml file names. Both MUST be lowercase, number, and no symbols except underscore

Problem 5: R.java shows after cleaning but won’t update

Again, ensure there isn’t a path problem. Then, if a clean regenerates R.java but won’t update it. Very likely the name of a value or enum is not allowed and must be fixed. Look at the R.java file and see what fields have errors and look for un-allowed characters. For example, maybe there is a dash instead of an underscore.

Conclusion

Well, that’s the end of my experience with this problem. Hopefully one of these solutions helped. Let me know if you’ve seen another case and have a solution.