Making AndroidAnnotations work with IntelliJ IDEA

AndroidAnnotations is a very promising framework for removing a lot of boilerplate code for Android developers. The site contains good instructions on how to use it from maven and Eclipse, but the setup is not that straightforward in IntelliJ IDEA.

Since you found your way here, I guess I’m too late to save you the mandatory googling, but hopefully this post can reduce the trial and error I went through to get it up and running.

These instructions are for IntelliJ IDEA 11.1 and AndroidAnnotations 2.6, but they may apply to other versions as well.

Compile-Time Annotation Processing

First a few words on what the AndroidAnnotations framework does, to explain the need for setting things up (in opposite to just adding a new library jar). Since Java 5.0, there is the possibility to plug into the compilation process by writing annotation processors. These processors run before the actual compilation and can be used to generate code. AndroidAnnotations includes such a processor that will generate new Activities, Services and other classes depending on the annotations you use in your code. Our problem is to tell IntelliJ IDEA to run this processor as a step in the build process.

The Steps to Make It Work

These instructions assume that AndroidAnnotations is the only annotation processing framework you are using (except for the built-in ones). If you want to use other annotation tools in the same project, please read the note at the end of this article for a workaround prior to running through the steps below.

If you are working with a maven project, first follow the steps at AndroidAnnotations to configure your maven build, then build the project from the command line once.

1. Download and Unpack AndroidAnnotations

If you don’t use maven, download the newest versions from AndroidAnnotations and unzip it. You should end up with two jars, androidannotations-2.6.jar, androidannotations-2.6-api.jar, a source package, some javadoc and license files. If you use maven, these files can be found in your local maven repository (<home_folder>/.m2/repository/com/googlecode/androidannotations/androidannotations/2.6/) after you added AndroidAnnotations to your pom file and built it once.

androidannotations-2.6.jar contains the annotation processor and the androidannotations-2.6-api.jar contains utility classes that are used by the generated code. (There are some nifty classes in their for your own use, too!)

2. Registering the Annotation Processor in IntelliJ IDEA

Open up the settings page for IntelliJ and go to Compiler->Annotation Processors. Check the box to enable annotation processing, select Processor Path and fill in the path to androidannotations.jar.

Now, add each Android module in which you want to use AndroidAnnotations to Processed Modules list at the bottom and fill in aa-gen as the Generated Sources Directory Name.

The annotations settings page for enabling AndroidAnnotations in IntelliJ IDEA

3. Configuring the Modules

In the previous step, you told IntelliJ to process annotations in your module. Now, we need to be able to access the annotations from the module. Furthermore, we need to make the module aware of the generated code to avoid compilation problems.

Before continuing, tell IntelliJ to rebuild your project. (Build->Rebuild Project from the menu.)

First, open up the settings for the module. If you don’t use maven, add androidannotations-2.6-api.jar as a library on the dependency tab. If you use maven, AndroidAnnotations should already be added as a maven dependency. (If you are using maven and don’t see it as a dependency, make sure you added AndroidAnnotations to your pom file and re-import your project.)

Module dependency settings for using AndroidAnnotations

Finally, go to the Sources tab for your module and add the aa-gen folder as a source folder.

Module source settings to use AndroidAnnotations

After pressing the OK button, everything should be ready for you to start annotating your Android code.

Note
If you are using AndroidAnnotations together with other annotation processing tools, step 2 above won’t work. In this case, you can select the option Obtain processors from project classpath instead. Then, you add androidannotations.jar as a global library project dependency. Using maven, you can add com.googlecode.androidannotations:androidannotations:2.6 as the dependency. Finally, you need to go into your module dependency lists and change com.sun.codemodel:codemode:2.4.1 and com.googlecode.androidannoations:androidannotations:2.6 to the provided scope. (Note that the last one probably appears twice in the list.)

The downside of this approach is of course that androidannotations will be added as a dependency to all your modules in the project.

This Post Has 13 Comments

  1. Thanks for this article, it’s a great help, however…

    I’m trying to get it to obtain the annotations processor from the classpath, but when I follow the note concerning that above, IntelliJ warns that ‘annotation processing without compilation was requested, but no processors were found’, and if I specify ‘com.googlecode.androidannotations.AndroidAnnotationProcssor’ in the ‘Processor FQ Name’ field, it’s consistent in that the warning becomes an error for that particular class not being found.

    I’d like to be able to use this project structure on several different computers, the processor path must apparently be specified as an absolute path, so the best I can do is copy the jar to the project root which at least has its own button in the finder (to save hunting around for the .m2 directory, etc).

    As it happens, I have a maven parent project containing the Android application project (for the app and Robolectric unit tests) and an instrumentation tests peer project – I’ve tried adding the annotations processor Maven dependency to the parent as well as the app project, marking it for export, and setting the scope to ‘compile’ instead of ‘provided’ but… no dice, sadly.

    Any clarification on setting up like this? Your note implies that you have that working.

    Thanks!

  2. Hi Tim,

    the instructions for using the annotations processor from the project classpath were wrong. The dependency should not be added to the module classpath, but to the global project classpath and then the scope should be set to provided in the modules. I’ve updated the instructions, and they are correct now, although stils terse.

    Personally, I actively avoid mixing the setup of IDEs together with the actual code. I never check in IDE-specific files to my VCS, for example. The reason being different people are using different IDEs or versions of IDEs. Even when I am doing projects on my own, I’m switching between IDEs and different versions of them. It feels more stable to keep the IDE setup separate from the code.

    That being said, I hope the updated instructions helps you with your setup!

  3. Hi Albin,
    Thanks for your post, following your steps I managed to get AA 2.6 and roboelectric working in intellij.

    However (althought it worked half a month ago with 2.7-snapshot) with AA 2.7 final I cannot make AA work. It shows the following error “Exception thrown while constructing Processor object: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy”

    Do you have any hint to solve this?
    Thank you in advance

    1. Hi Estornino,

      it’s a bit messy with AA 2.7 and IDE support, I’m afraid. This goes for both Eclipse and Intellij. The problem is that the annotation processing tool needs the AA-api jar as well as the code-model jar from Sun. The solution is to add all three of them in the processor path in step 2 above. You do this by separating the jars with colon (:) on linux and osX and semicolon (;) on windows. Something like:

      <home_folder>/.m2/repository/com/googlecode/androidannotations/androidannotations/2.7/androidannotations-2.7.jar:<home_folder>/.m2/repository/com/googlecode/androidannotations/androidannotations-api/2.7/androidannotations-api-2.7.jar:<home_folder>/.m2/repository/com/sun/codemodel/codemodel/2.4.1/codemodel-2.4.1.jar

      These are from my maven repo. If you downloaded them somewhere else, just use that path.

      If you’re not using maven, you could download the 2.7-bundle from the AA homepage. Then it’s enough to specify the androidannotations-2.7.jar and androidannotations-api-2.7.jar separated by colon (or semi-colon on windows).

      I don’t know why this wasn’t a problem in 2.6, since the same dependencies were present there.

      Thanks for poking me about this problem. I’ll try to get around to update the article.

      Good luck!

      1. Hi Albin, thank you very much for your advice. I’m using maven, in fact I moved from eclipse to Intellij motivated by the poor support that gives eclipse to it.
        As you point, in version 2.6 and either version 2.7-snapshot prior to 2.7 final this dependency problem didn’t exist

      2. The behavior changed between 2.6 and 2.7 because I changed the behavior :) . Basically, in 2.6, androidannotations.jar always contained its dependencies, even when you used Maven, which was obviously wrong. We now publish a bundle zip that contains an androidannotations.jar that contains its dependencies, and the maven artifact androidannotations.jar that doesn’t contain its dependencies, for Maven users. Now that those dependencies are gone, you need to add them manually when you use the androidannotations.jar file coming from the repo. One tip could be to download the bundle and use this special jar that contains its dependencies.

        The real fix though would be that IntelliJ understand Dependency Management when it comes to annotation processing.

  4. Good articale!
    Thanks!

  5. I use the lastest version of android studio (0.5.4).The configeration of Android Annotation is different from your post.With the config ,it doesn’t generate the class ending with ‘_’.The config page is different from your post. Counld you tell me how to config Android Annotation in the latest version of Android studio.Thanks.

Leave a Reply

Close Menu