在build.gradle文件中经常看到annotationProcessor、android-apt这下代表的是什么
随着一些ButterKnife,dagger等的开源注解框架的流行,APT的概念也越来越被熟知。
什么是APT?
APT(Annotation Processing Tool)是一种处理注释的工具,它对源代码文件进行检测找出其中的Annotation,使用Annotation进行额外的处理。 Annotation处理器在处理Annotation时可以根据源文件中的Annotation生成额外的源文件和其它的文件(文件具体内容由Annotation处理器的编写者决定),APT还会编译生成的源文件和原来的源文件,将它们一起生成class文件。
annotationProcessor
annotationProcessor就是APT工具中的一种,他是google开发的内置框架,不需要引入,具体的使用方法:
dependencies {
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
android-apt
android-apt是由一位开发者自己开发的apt框架,随着Android Gradle 插件 2.2 版本的发布,Android Gradle 插件提供了名为 annotationProcessor 的功能来完全代替 android-apt ,自此android-apt 作者在官网发表声明证实了后续将不会继续维护 android-apt ,并推荐大家使用 Android 官方插件annotationProcessor。
但是很多项目目前还是使用android-apt,如果想替换为annotationProcessor,那就要知道android-apt是如何使用的。
android-apt的官方文档的翻译
在Project下的build.gradle中添加android-apt
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
在Module中build.gradle的配置
apply plugin: 'com.neenbedankt.android-apt'
...
dependencies {
apt 'com.squareup.dagger:dagger-compiler:1.1.0'
compile 'com.squareup.dagger:dagger:1.1.0'
}
annotationProcessor与android-apt的使用方法非常类似的,推荐使用google自己开发的annotationProcessor,配置简单,而且google自己维护的框架各方面的性能都是有保证的