I would like to fix this error
R cannot be resolved to a variable
I looked up many answers, but I could not get the right one;
Simple Solution:
Check whether in gen[generated Java Files], under your project package, there is a R.java file.
If no,
If yes,
If R is missing
I've seen many answers suggesting to "fix errors in your XML files" but only giving general hints and tips on how to fix the XML.
Look at the "Problems" tab in Eclipse. Ignore all "R cannot be resolved to a variable". Look at what remains. For me, it looks something like this:
Unparsed aapt error(s)! Check the console for output.
I quote from http://developer.android.com/tools/building/index.html#detailed-build :
The Android Asset Packaging Tool (aapt) takes your application resource files, such as the AndroidManifest.xml file and the XML files for your Activities, and compiles them. An R.java is also produced so you can reference your resources from your Java code.
Now, open the "Console" tab. Window -> Show View > Console
This should show you the exact error, in which file it occurred, and at what line number:
[2014-05-13 08:27:05 - MyFirstApp] /Users/alain/code/experimental/android/adt_workspace4.4/MyFirstApp/res/menu/main_activity_actions.xml:4: error: Error: No resource found that matches the given name (at 'icon' with value '@drawable/ic_action_search').
[2014-05-13 08:27:05 - MyFirstApp] /Users/alain/code/experimental/android/adt_workspace4.4/MyFirstApp/res/menu/main_activity_actions.xml:4: error: Error: No resource found that matches the given name (at 'title' with value '@string/action_search').
In my case, the first error is because I am referencing ic_action_search but I have not yet put ic_action_search.png in my res/drawable-*dpi directories. The second error is because I am referencing a string "action_search" but I have not yet defined it in res/values/strings.xml
It would have been nice if the ADT plugin actually took you to the line and file where the error is if you click on the error in the Console output, but for now, you have to do it manually.
The problem with Eclipse is that is is not stopping the build when aapt fails but continues to the next stage of the build, which is the Java Compiler. The command-line tools do not have this problem:
To generate build.xml:
$ android list targets
For me, I have:
Available Android targets: ---------- id: 1 or "android-16" Name: Android 4.1.2 Type: Platform API level: 16 Revision: 4 Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in Tag/ABIs : default/armeabi-v7a, default/mips, default/x86 ---------- id: 2 or "android-19" Name: Android 4.4.2 Type: Platform API level: 19 Revision: 3 Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in Tag/ABIs : default/armeabi-v7a
Choose your target from the above list and use it in the next command:
$ android update project --target <your_target_id> --path /path/to/android/project
Now that build.xml is generated, you can build your project using one of these:
$ ant debug
$ ant -verbose debug
$ ant -debug debug
If the build fails at the aapt stage, it correctly does not generate R.java, but also it correctly does not continue to the Java Compiler stage. The console output will show you (just like in the Eclipse Console View) the filenames and line numbers where your XML errors are.
and here is how to turn on line numbers in Eclipse:
http://www.mkyong.com/eclipse/how-to-display-line-numbers-in-eclipse/
Preferences -> General -> Editors -> Text Editors -> [x] Show line numbers
for me, the best solution to suggest to people that have problems with the "R" file would be to try the next steps (order doesn't matter) :
update ADT & SDK , Eclipse and Java (both 1.6 and the latest one).
EDIT: now the ADT supports Java 1.7 . link here.
remove gen folder , and create it again .
do a clean-project.
right click the project and choose android-tools -> fix-project-properties .
right click the project and choose properties -> java-build-path -> order-and-export. make sure the order is :
Android 4.4 (always the latest version)
Android private libraries
android dependencies
your library project/s if needed
yourAppProject/gen
yourAppProject/src
make sure all files in the res folder's subfolders have names that are ok : only lowercase letters, digits and underscore ("_") .
always make sure the targetSdk is pointed to the latest API (currently 19) , and set it in the project.properties file
try to run Lint and read its warnings. they might help you out.
make sure your project compiles on 1.6 java and not a different version.
restart eclipse.
I do my projects in FlashBuilder 4.7 and it gerenerates a row in strings.xml:
<string name="action_settings">Settings</string>
Once i deleted it, i got the same errors. When returned it in place everythings worked again.