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;
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 --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