s.d
s.d

Reputation: 29436

Automatic Javadoc for android project on build

I want to enable automatic Javadoc generation for a library project on each build. I have 3 issues to tackle:

  1. How to integrate such an automated script in IDE's build process ? (Eclipse and IDEA).

  2. How do I exclude the auto-generated R and Build classes from docs processing, as they are always generated under the source's namespace.

  3. Pack and place Javadoc as a jar next to the build jar file.

The question is a bit broad, But I'm sure a good consolidated answer in one place will save the trouble for many.

Upvotes: 1

Views: 763

Answers (1)

s.d
s.d

Reputation: 29436

Two ways:

  • Use Maven : Maven is great, and has android build support too. But still with default android structure, everything doesn't works as effortlessly as default Ant builds. I hope in future Google introduces Maven structure.
  • Good old Apache Ant: the default ant build file lies in sdk_path/tools/ant/build.xml, this is imported into project's build xml. Hence custom targets can be added for some additional work. Best place to put them is in a custom_rules.xml file at project root and import in project's build.xml.

custom_rules.xml:

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

    <!-- IDE produces default jar as: out/production/classes.jar ,
    so we define a custom target to rename the jar, generate and package docs jar along
    with it  -->
    <target name="package_library" depends="init_pack_lib, check_lib_jar,   rename_lib_jar,generate_javadoc"/>

    <!-- init all properties to use-->
    <target name="init_pack_lib">
        <property name="real.out.dir" value="${basedir}/out/production"/>
        <property name="lib.gen.jar.file" value="${real.out.dir}/classes.jar"/>
        <property name="docs.dir" value="${real.out.dir}/Docs"/>

        <!-- we need to peek into manifest xml file-->
        <xmlproperty file="${manifest.abs.file}" collapseattributes="true"/>
        <property name="app.package" value="${manifest.package}"/>
        <property name="app.dist.name" value="${ant.project.name}_${manifest.android:versionName}"/>
        <property name="doc.package.names" value="${app.package}.*"/>
    </target>

    <!-- is classes.jar generated ?-->
    <target name="check_lib_jar">
        <available file="${lib.gen.jar.file}" property="lib.jar.found"/>
    </target>

    <!-- if so, give it a better name-->
    <target name="rename_lib_jar" if="${lib.jar.found}">
        <move file="${lib.gen.jar.file}"
              tofile="${real.out.dir}/${app.dist.name}.jar"/>
    </target>

    <!-- generate and package JavaDoc for project-->
    <target name="generate_javadoc" depends="check_doc_dir, create_doc_dir, clean_docs_dir">
        <javadoc packagenames="${doc.package.names}"
                 sourcepath="${source.absolute.dir}"
                 destdir="${docs.dir}"
                 verbose="false"
                />
        <jar compress="${jar.compress}" basedir="${docs.dir}" jarfile="${real.out.dir}/${app.dist.name}_JavaDoc.jar"/>
    </target>

    <!-- what about JavaDocs destination directory ? -->
    <target name="check_doc_dir">
        <condition property="docs.dir.not.exists">
            <not>
                <available type="dir" file="${docs.dir}" property="docs.dir.exists"/>
            </not>
        </condition>
    </target>

    <!-- make docs directory if it isn't there already-->
    <target name="create_doc_dir" if="docs.dir.not.exists">
        <mkdir dir="${docs.dir}"/>
    </target>

    <!-- wipe previous JavaDocs files-->
    <target name="clean_docs_dir" if="docs.dir.exists">
        <delete includeemptydirs="true">
            <fileset dir="${docs.dir}" includes="**/*"/>
        </delete>
    </target>
</project>

To create package, simply run ant package_library from project root. You can include these in a build target so, they run on every build.

Upvotes: 5

Related Questions