All articles

Set Up AndroidAnnotations

This article will briefly explain how to setup AndroidAnnotations in an existing project to simplify your code. For more details, you can check the official git repository wiki

Why Use AndroidAnnotations

AndroidAnnotations will considerably reduce the time you spend developing as well as the boilerplate code you need to write. Moreover, with some cool features as annotations for events, it’ll offset the fact that we can’t use lambda (at least without a tool like retrolambda) in android development.

Step 1: apt plugin

First, you need to add the apt plugin to your classpath, this plugin can be used for a lot of library using annotation processing, it’ll take care of configuring everything for your build. If you want more details, check the official website 

Add the following classpath line to your project root build.gradle file

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.1'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}

Now, apply the plugin in your module’s build.gradle

apply plugin: 'com.neenbedankt.android-apt'

Step 2: setup AndroidAnnotations

Configure annotation processor

To setup AndroidAnnotations, you first need to pass arguments to the annotation processor, add the following code to the end of your module’s build.gradle

apt {
    arguments {
        resourcePackageName android.defaultConfig.applicationId
        androidManifestFile variant.outputs[0]?.processResources?.manifestFile
    }
}

Add dependency

Define a constant below the apply lines in your build.gradle file:

def AAVersion = '3.3.2'

You can now add the dependency in your block:

dependencies {
    // Other dependencies, app-compat, ...
    // Android annotation
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

You’re done! You can now start using AndroidAnnotations. For more examples and explanations, I recommend reading the documentation at https://github.com/excilys/androidannotations/wiki

Important note

The first time you want to build and run your project (can also be the case after a clean ), you’ll need to make your project, it’ll run the annotation processing. I had a hard time figuring this the first time, I couldn’t run my project, and impossible to start the annotation processor manually.

The versions I used for the library and the apt plugin are the current latest version, but you’ll want to check the current latest version when you read this article.