Using ant to generate javadoc for your android project

This process describes the generation of an android project using ANT, which will generate the project and also generate the javadoc documentation

If you want to generate JavaDoc for your project directly from the command line, you can easily do it by adding some extra configuration to the ant build.xml script.

If you don’t have your ant build script in your project directory you can run the following to command to create it along with some of the necessary files required.

android update project -p <android_project_path>

The original ant build script is like this:

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

    <property file="local.properties" />
    <property file="ant.properties" />

    <property environment="env" />
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME" />
    </condition>

    <loadproperties srcFile="project.properties" />

    <fail
            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
            unless="sdk.dir"
    />

    <import file="custom_rules.xml" optional="true" />
    <import file="${sdk.dir}/tools/ant/build.xml" />

</project>

So we need to add the following to the ant build script, you can add all this above the line

<import file="${sdk.dir}/tools/ant/build.xml" />

This is the new configuration that you need to add to the ant script

<property name="docs.dir" location="javadoc" />
<property name="bin.dir" location="bin" />
<property name="source.dir" location="src" />
<property name="gen.dir" location="gen" />

<target
    name="javadoc"
    description="Generate JavaDoc documentation" >

    <xmlproperty
        collapseAttributes="true"
        file="AndroidManifest.xml"
        prefix="tm" />

    <mkdir dir="${docs.dir}" />

    <javadoc
        access="private"
        author="true"
        classpath="${sdk.dir}/platforms/${target}/android.jar"
        destdir="${docs.dir}"
        linkoffline="http://d.android.com/reference ${sdk.dir}/docs/reference"
        linksource="true"
        sourcepath="${source.dir};${gen.dir}"
        use="true"
        version="true" />

    <jar
        basedir="${docs.dir}"
        compress="${jar.compress}"
        jarfile="${bin.dir}/${tm.manifest.package}_${tm.manifest.android:versionName}_javadoc.jar" />
</target>

<target
   name="clean"
   depends="android_rules.clean" >

   <delete dir="${docs.dir}" />
</target>

Now we can run the following commands

  • ant clean

    Will clean the directories for the android project and also the javadoc directory as it’s overridden in the target

  • ant javadoc

    Will generate the JavaDoc in the ${docs.dir} It will also generate a jar file with the javadoc