Using Android build.xml in eclipse?
Asked Answered
R

6

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?

Reardon answered 27/2, 2011 at 20:34 Comment(0)
C
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.

Curule answered 1/6, 2011 at 13:12 Comment(6)
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?Reardon
You made my day, Tatu Lahtela!! Thanks for the great answer, that helped so much!! :-)Kidskin
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?Reardon
They appear if you add the row in option 1 and then remove it. Strange behavior indeed.Curule
In my distribution, the android_rules.xml file only exists in android-3, 4, 7, and 8.Emolument
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 <import file="${sdk.dir}... line just BELOW the <loadproperties srcFile="local.properties" /> 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 <import>, which now has things working in Eclipse AND on the command-line.Aluminize
I
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.

Intelligencer answered 2/6, 2011 at 17:43 Comment(2)
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?Reardon
After checking the ignore box had to open/close build.xml to make the error disappear.Dipteral
D
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.

Demurrage answered 25/10, 2011 at 8:33 Comment(1)
Seems to work. Actually, all you need is <target name="help" />Refugia
A
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.
Aerial answered 1/5, 2012 at 15:1 Comment(0)
U
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!

Unsphere answered 18/3, 2011 at 4:45 Comment(0)
E
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" />
Expertise answered 27/2, 2011 at 21:0 Comment(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.Unsphere

© 2022 - 2024 — McMap. All rights reserved.