您的位置:首页 > 移动开发 > Android开发

Making AndroidAnnotations work with IntelliJ IDEA

2014-05-05 18:53 489 查看
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.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.





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.)





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





After pressing the OK button, everything should be ready for you to start annotating your Android code.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: