23

I have an android project which has an Ant buildfile. It works great via the command-line:

rascher@coltrane:~/git/$ ant
Buildfile: build.xml
    [setup] Android SDK Tools Revision 8
    [setup] Project Target: Android 2.1-update1
    [setup] API level: 7
    [setup] 

    ... etc etc etc ...

But when I try to use this in eclipse, build.xml has a red-X.

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyWonderfulProject" default="help">

<project is underlined with the error: Default target help does not exist in this project

It seems like everybody else on the internet has this issue, and it seems to be caused by the fact that build.xml is using directives that come from the multiple nested android-specific files that this buildfile imports.

I have other projects in my workspace that use Eclipse's build mechanism, so I know that my environment is able to compile, run, and deploy Android applications without an issue. But this buildfile is giving me headaches.

What is the fix?

poundifdef
  • 18,726
  • 23
  • 95
  • 134

6 Answers6

13

Option 1:

Import the Android ant template file in the build.xml file. Assuming you have sdk.dir defined in local.properties and pointing to your Android installation directory, add the following under the project element:

    <import file="${sdk.dir}/platforms/${target}/templates/android_rules.xml" />

Option 2:

To actually use the Android ant targets in the Eclipse ant perspective, the above will not help. There seems to be some general level incompatibility that I haven't figured out to resolve completely, but to get things working at a satisfactory level though you can:

  • Import the project build.xml as a a builder. Project properties -> Builders -> import.
  • Do not use option 1. Instead ignore the warning about the missing template. Global properties -> Ant -> Editor -> Problems. Add build.xml to names.

This will have an impact on all projects and ignore actual errors, so it's not a perfect solution. But you can use the Android build options inside Eclipse, as well as from the command line.

Tatu Lahtela
  • 4,514
  • 30
  • 29
  • This is a step in the right direction! Follow-up: now the red-X's go away when I view build.xml. But in the Ant perspective it still has an error: `Cannot find ${sdk.dir}/platforms/${target}/templates/android_rules.xml imported from ...`. When I hover over the variables in the editor, the correct values pop up. But the Ant perspective doesn't seem to do that substitution. Help? – poundifdef Jun 02 '11 at 01:57
  • You made my day, Tatu Lahtela!! Thanks for the great answer, that helped so much!! :-) – Bryan Jun 27 '11 at 23:19
  • This almost works! The Ant perspective does not show the targets in my build.xml so that I can double click on an individual target. Do you get the same behavior? – poundifdef Jul 02 '11 at 02:55
  • They appear if you add the row in option 1 and then remove it. Strange behavior indeed. – Tatu Lahtela Jul 02 '11 at 11:55
  • 3
    In my distribution, the android_rules.xml file only exists in android-3, 4, 7, and 8. – Jeff Axelrod Nov 11 '11 at 20:32
  • I have Option 1 working in Eclipse for me, but this needs to still work on the command-line for others in my team who aren't using Eclipse. I put the line and the {sdk.dir} was substituted from the command-line, as you'd expect. But the {target} is still not substituted, though. Rather than try to fix that I just added an optional="true" attribute to the , which now has things working in Eclipse AND on the command-line. – tobych Dec 10 '11 at 05:09
6

This doesn't fix the problem, but it will make Eclipse stop getting in your way. Go to Window > Preferences > Ant > Editor, and select the Problems tab. Check the ignore all buildfile problems box.

mpontillo
  • 13,559
  • 7
  • 62
  • 90
len
  • 178
  • 1
  • 3
  • 10
  • This almost works! The Ant perspective does not show the targets in my build.xml so that I can double click on an individual target. Do you get the same behavior? – poundifdef Jul 02 '11 at 02:56
  • After checking the ignore box had to open/close build.xml to make the error disappear. – Asahi Apr 16 '12 at 08:29
6

There is one target called 'help' defined in the ${sdk.dir}/tools/ant/main_rules.xml like below,

<target name="help">

    <echo>Android Ant Build. Available targets:</echo>
    <echo>   help:      Displays this help.</echo>
    <echo>   clean:     Removes output files created by other targets.</echo>
    <echo>   compile:   Compiles project's .java files into .class files.</echo>
    <echo>   debug:     Builds the application and signs it with a debug key.</echo>
    <echo>   release:   Builds the application. The generated apk file must be</echo>
    <echo>              signed before it is published.</echo>
    <echo>   install:   Installs/reinstalls the debug package onto a running</echo>
    <echo>              emulator or device.</echo>
    <echo>              If the application was previously installed, the</echo>
    <echo>              signatures must match.</echo>
    <echo>   uninstall: Uninstalls the application from a running emulator or</echo>
    <echo>              device.</echo>
</target>

copy this one to your build.xml and it should works.

Michael Wang
  • 9,583
  • 1
  • 19
  • 17
2

I has same issue and all i did was goto on the eclipse

Window->Preferences->Ant->Editor->Problems

and click

Ignore All Build File Problems.
kleopatra
  • 51,061
  • 28
  • 99
  • 211
1

This isn't really an answer, but for what it's worth, I was able to make the problem go away by removing the eclipse project from the workspace, deleting .* in my project directory and starting over. Eclipse FTW!

forksandhope
  • 501
  • 5
  • 12
0

If your build.xml has its own help target rather than relying on the imported one, then you need to put that target before the line

<taskdef name="setup" classname="com.android.ant.SetupTask" classpathref="android.antlibs" />
NickT
  • 23,844
  • 11
  • 78
  • 121
  • 1
    I have a similar issue, and have no self-defined help target. the help target from android_rules.xml should be used, but even pasting it into the build.xml file doesn't resolve this error. (eclipse 3.6, android skd 10, target is android-7) building with ant on command line works perfectly. – forksandhope Mar 09 '11 at 16:31