Thursday, June 19, 2014

Using Ivy + Ant for Android in Eclipse (Dependency management and building made easy)

Recently I discovered Apache Ivy, a dependency management system supporting Maven repositories, that can be integrated with your Eclipse IDE and also with your Ant build scripts. Since I found very little documentation on how to integrate these things and struggled myself quiet a bit, I decided to make a guide to help others with the same problems.

How to extend a Classpath entry in Ant

When dealing with Ivy in an Ant build script I wanted to simply add Ivy's classpath to the buildpath of my app instead of copying all the JARs around. The challenge boiled down to overriding, i.e. extending the default script's classpath entry named project.all.jars.path.

Here's what you can not do:

<path id="project.all.jars.path">
    <path refid="project.all.jars.path" />
    <path refid="ivy.build.path" />
</path>

Defining a path by referencing itself doesn't work because both these references would point to the same object. Here's what does work:

<pathconvert property="classpathPropBefore" refid="project.all.jars.path" />

<path id="project.all.jars.path">
    <path path="${classpathPropBefore}" />
    <path refid="ivy.build.path" />
</path>

It's simple if you think about it. All I did was write the old content of the classpathentry into the property classpathPropBefore and then redefined the classpath entry with it's old content plus my addition.