从android-apt切换到annotationProcessor

来源:互联网 发布:java server error 编辑:程序博客网 时间:2024/06/06 01:10

项目中的EventBus ,Butterknife…等框架都采用了android-apt注解预编译的方式(在编译期生成代码,提高效率),Android Studio推出了官方插件annotationProcessor,并且可以通过gradle来简单的配置;所以apt的作者早已经宣布不再维护了,但是在项目中一直没有替换,最近抽空将apt,替换成了annotationProcessor

首先要确保Android Gradle插件版本是2.2以上

接下来开始修改配置

修改Project 的build.gradle配置

原来的配置android-apt方式

dependencies {        classpath 'com.android.tools.build:gradle:2.2.3'        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'    }

修改为annotationProcessor

dependencies {        classpath 'com.android.tools.build:gradle:2.2.3'    }

修改模块中的依赖

由于我们的项目是多组件多个模块,所以Eventbus,Butterknife依赖都添加在CommonLib中,以Eventbus为例:

android-apt方式

apply plugin: 'com.neenbedankt.android-apt'buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'    }}dependencies {    compile 'org.greenrobot:eventbus:3.0.0'    apt'org.greenrobot:eventbus-annotation-processor:3.0.1'//apt}

修改后annotationProcessor 方式,只保留dependencies 里面的引用并且把apt 换成annotationProcessor就可以了

dependencies {    compile 'org.greenrobot:eventbus:3.0.0'    annotationProcessor  'org.greenrobot:eventbus-annotation-processor:3.0.1'}

这里要注意的是

关于Eventbus的MyEventBusIndex

android-apt方式

apt  {    arguments {        eventBusIndex "com.xxx.xxx.MyEventBusIndex"    }}

修改后annotationProcessor 方式

defaultConfig {        javaCompileOptions {            annotationProcessorOptions {                arguments = [ eventBusIndex : 'com.xxx.xxx.MyEventBusIndex' ]            }        }    }

注意:如果采用在基础模块中配置的,一定要在使用的其他模块中配置,不然不能生成代码

annotationProcessor  'org.greenrobot:eventbus-annotation-processor:3.0.1'
阅读全文
0 0
原创粉丝点击