Archive‎ > ‎Fall 2009‎ > ‎Course Project‎ > ‎Buddy Suite‎ > ‎Tutorial #2‎ > ‎

XML and ANT

XML

XML is a document format used for encoding data in a structured fashion.

Usually, a XML document begins with declaring some information about itself, as in:
    <?xml version="1.0" encoding="UTF-8"?>

A XML document is structured as a sequence of tags.
A tag has one of the following formats:
  • <tagName [property1 property2 ... ] />
  • <tagName [property1 property2 ... ] > content </tagName>
A property has the following format:
    name="value"

The content of a tag is a string, as in 
    <tag>This is the content<tag/>
or a sequence of tags, as in
    <tag> <tag1 p1="val1" /> <tag2 p2="val2">second tag</tag2></tag>

ANT

Ant is build tool for Java programs, similar to make.

Ant requires a build.xml file that specifies the build process.

A build.xml consists of a sequence of targets. 
A target contains task elements. When the target is executed, its tasks are executed in the order in which they appear in the target.
A target may depend on other targets. When the target is executed, these targets are executed first.

Here is a task that runs "android list avd":
    <exec executable="android">
       <arg value="list" />
       <arg value="avd"/>
    </exec>

Here is a target that simply creates the "bin" folder for the build outputs:
  <target name="init">
    <mkdir dir="bin"/>
  </target>

Here is a task that compiles the Java source files from "src" folder and puts the compiled classes into the "bin" folder:
    <javac srcdir="src" destdir="bin" />

Here is a target for compiling a program:
  <target name="compile" depends="init" description="compile the program" >
    <javac srcdir="src" destdir="bin"/>
  </target>

Here is a build.xml file with default target "compile":
<project name="ProjectName" default="compile">
  <target name="init">
    <mkdir dir="bin"/>
  </target>
  
  <target name="compile" depends="init" description="compile the program" >
    <javac srcdir="src" destdir="bin"/>
  </target>
  
  <target name="clean" description="clean up" >
    <delete dir="bin"/>
  </target>
</project>

Having a build.xml file as the above one in a project root folder, you can compile the project by running
    ant
or running the "compile" target, as in
    ant compile

You can clean the project (i.e., remove the "bin" folder) by running the "clean" target, as in
    ant clean

If you want to know more about Ant, here is a comprehensive Ant tutorial.

For your first milestone, you will need to add a new target (e.g., milestone1) to the build.xml file generated by "android create project ..." (see tutorial 1), as we show below:

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

    <property file="local.properties"/>

    <property file="build.properties"/>

    <property file="default.properties"/>

    <path id="android.antlibs">
        <pathelement path="${sdk-location}/tools/lib/anttasks.jar" />
        <pathelement path="${sdk-location}/tools/lib/sdklib.jar" />
        <pathelement path="${sdk-location}/tools/lib/androidprefs.jar" />
        <pathelement path="${sdk-location}/tools/lib/apkbuilder.jar" />
        <pathelement path="${sdk-location}/tools/lib/jarutils.jar" />
    </path>

    <taskdef name="setup"
        classname="com.android.ant.SetupTask"
        classpathref="android.antlibs"/>

    <setup />
    
    <target name="milestone1" depends="debug">
      <echo>Compile and run deliverable 1</echo>        
        <exec executable="${adb}" failonerror="true">
            <arg value="-s" />
            <arg value="emulator-5554"/>
            <arg value="install" />
            <arg value="-r" />
            <arg path="${out-debug-package}" />
        </exec>
        <exec executable="${adb}" failonerror="true">
            <arg value="-s" />
            <arg value="emulator-5556"/>
            <arg value="install" />
            <arg value="-r" />
            <arg path="${out-debug-package}" />
        </exec>
        <javadoc sourcepath="src"
                 destdir="javadoc"
                 packagenames="sweng.buddySuite.*" >
          <classpath>
            <pathelement path="libs/junit.jar"/>
            <pathelement path="${sdk-location}/platforms/android-1.5/android.jar"/>
          </classpath>
        </javadoc>   
        <junit haltonfailure="true" fork="true" forkMode="once" printsummary="true">
          <classpath>
            <pathelement path="${out-classes}" />
            <pathelement path="libs/junit.jar"/>
          </classpath>
          <test name="sweng.buddySuite.JUnitTest"/>
        </junit>
    </target>
</project>

The above target (i.e., "milestone1") is sufficient to compile and run your project (on any machine) for your first milestone.
The "debug" target (predefined in the Android SDK), on which "milestone1" depends, is executed first; this target compiles your project.
The "exec" tasks above (re)install the project on the two running emulators.
The "javadoc" task generates the javadoc documentation.
The "junit" task runs the JUnit tests.

Notes: 
If you choose different names for the project, packages or JUnit tests, do not forget to change them accordingly in your build.xml file.
If your project is not generated from command line (i.e., it is generated from Eclipse) do not forget to add the following line to your local.properties/default.properties file
sdk-location=<path to your Android SDK>

By running
    ant milestone1
you compile the project, install it on the two running emulators, generate the javadoc documentation and run the JUnit test(s).

Before you run "ant milestone1":
  • You need to make sure that the JUnit jar file is in the "libs" folder of your project.
  • Ant's "bin" folder and Android SDK's "tools" folder are in your PATH environment variable (see tutorial 1).
  • Create two AVDs, as in: 
    • android create avd --name AVD1 --target 3
    • android create avd --name AVD2 --target 3
  • Run two emulators, as in:
    • emulator -avd AVD1
    • emulator -avd AVD2