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.jarandroidannotations-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.jarcontains 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值