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.
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.
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.
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!)
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
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.
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.
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 aworkaround 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 andunzip 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 andfill 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.
相关文章推荐
- android应用全屏模式的切换
- androidpn 引用的其他jar和源码下载地址
- Android Animation动画(很详细)
- 【转载自sqlite官网】SQLite Android Bindings
- Android尺寸单位总结
- android 操作SD卡上的文件
- Android 多线程操作之 Handler 与 HandlerThread
- Android通过Cursorloader获取联系人列表
- Android Bitmap详细介绍
- 改变ViewPager的翻页速率
- android 输入法WINDOW_FOCUS_CHANGED|MSG_CREATE_SESSION|MSG_BIND_INPUT|MSG_START_INPUT|MSG_BIND_METHOD消息
- 人生中的第一篇博文来了!!!
- 深入探讨BroadcastReceiver
- 通付盾推出Android应用安全加固服务(效果实测)
- Android点击EditText文本框之外任何地方隐藏键盘的解决办法
- Android平台向SD卡写文本文件
- android 自定义Toast显示风格
- Android EditText 取消复制粘贴功能、取消横向全屏编辑功能
- Android图片压缩
- Android中日期操作