Building Project Ant - PerfectCarl/androidannotations GitHub Wiki

What you should know before starting

AndroidAnnotations works by generating code at compile time.

AndroidAnnotations provides two jars:

  • androidannotations-X.Y.jar is needed to generate the code at compile time. There is no reason to keep this jar at runtime because:
    • Its code will never be executed at runtime.
    • It makes your APK size bigger than needed.
  • androidannotations-X.Y-api.jar only contains the code you need at runtime.

Prerequisites

  • This tutorial is based on the SDK v19. If you use another version, you may need to adapt this tutorial.

  • if you don't already have a build.xml file, you can easily generate one:

android update project --path "$PROJECT_ROOT$"

How to

  • Create a new folder at the root of your project (compile-libs would be a good candidate) and put androidannotations-X.Y.jar in this folder.

  • Put androidannotations-X.Y-api.jar in the $PROJECT_ROOT$/libs

  • Create a custom_rules.xml Ant file next to your build.xml Ant script. The build.xml script generated by the Android tools automatically imports custom_rules.xml if it exists. This enables you to customize the build, without having to modify build.xml, which can therefore be easily updated.

  • Add properties for the generated source folder in custom_rules.xml

    <property name="generated.dir" value=".apt_generated" />
    <property name="generated.absolute.dir" location="${generated.dir}" />
    <property name="java.compilerargs" value="-s &apos;${generated.absolute.dir}&apos;" />

Note: In some case you may have to replace .apt_generated by gen to make the whole thing works.

  • Override the -pre-compile target in custom_rules.xml
    <target name="-pre-compile">
        <mkdir dir="${generated.absolute.dir}" />
    </target>
  • Override the -compile target in custom_rules.xml

    • Open $ANDROID_SDK_ROOT$/tools/ant/build.xml

    • Locate the -compile target in this file:

<target name="-compile" depends="-build-setup, -pre-build, -code-gen, -pre-compile">
 ...
</target>
  • Copy the target and its content into custom_rules.xml

  • Modify the classpath when javac is invoked by adding a <fileset> node, and configure javac to generates the sources in a dedicated folder:

<target name="-compile" ...>
...
            <path id="project.javac.classpath">
                ...
+               <fileset dir="compile-libs" includes="*.jar"/>
            </path>
...
</target>
  • You should now be able to build you project using ant:
ant clean release

Next steps

Potential issues

  • If you put the two AndroidAnnotations jars in the $PROJECT_ROOT$/libs you will encounter the following error:
java.lang.IllegalArgumentException: already added: Lcom/googlecode/androidannotations/annotations/AfterViews;

androidannotations-X.Y-api.jar is a subset of androidannotations-X.Y.jar. So each class in androidannotations-X.Y-api.jar is present in androidannotations-X.Y.jar.

This error is thrown when the dx command is invoked and two classes with the same name and package name are detected. To prevent this error you have to move androidannotations-X.Y.jar file away from the $PROJECT_ROOT$/libs folder.

  • Anything else? Contact us on the mailing list, or create an issue, and we'll try to help you.

  • After upgrading the Android SDK, the content of $ANDROID_SDK_ROOT$/tools/ant/build.xml may have changed. Therefore, your ant build may be broken. The solution is to replace the content of the -compile target in custom_rules.xml with the new -compile target content defined in $ANDROID_SDK_ROOT$/tools/ant/build.xml

⚠️ **GitHub.com Fallback** ⚠️