Skip to content

Building Project Ant

matboniface edited this page Jan 11, 2012 · 11 revisions

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 v16. If you are using an other version, you will certainly need to adapt this tutorial.

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

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 the androidannotations-X.Y.jar in this folder.

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

  • Override the -compile target defined in the imported ant script in your build.xml :

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

    • Locate the -compile defined target in this file, something like that :

<target name="-compile" depends="-build-setup, -pre-build, -code-gen, -pre-compile">
 ...
</target>
  • Copy the target and his content into your own build.xml before this statement
<import file="${sdk.dir}/tools/ant/build.xml">
  • Modify the classpath when javac is invoked by adding a fileset :
<javac ... >
  ...
  <classpath>
    ...
    <fileset dir="compile-libs" includes="*.jar"/>
  </classpath>
  ...
</javac>
  • At this time, you should be able to build you project using ant :
ant release

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.

Wiki

Using AndroidAnnotations

Questions?

Developing AndroidAnnotations

Misc

Clone this wiki locally