android apt使用的填坑记录

你们都知道java的注解(Annotation) 包含了运行时注解和编译时注解。
运行时注解的例子:java

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectView {    
    int id() default 0;
}

编译时注解的例子:框架

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.CLASS)
public @interface InjectView {    
     int id() default 0;
}

他们之间的区别是RetentionPolicy不一样。
在开发编译时注解,咱们一般会写一个compiler的java module。以我本身的框架为例,有一个saf-injectview-compilersvg


屏幕快照 2016-12-18 上午11.30.03.png

今天要说的填坑是:这个compiler module在打成jar包,给其余项目使用须要注意的事项。ui

下面是个人打包脚本spa

task buildInjectCompiler( type: Jar) {    
    archiveName = 'SAF-inject-compiler-'+VERSION_NAME+'.jar'    //初始化资源路径集    
    
    from baseCompilerPath, injectviewCompilerPath    
    
    //去除路径集下部分的资源    
    exclude "**/BuildConfig.class"    
    exclude "**/Manifest.class"    
    exclude "**/R.class"    
    exclude "**/R\$*.class"    
    
    //只导入资源路径集下的部分资源    
    include "cn/salesuite/**/*.class"    
    include "META-INF/services/javax.annotation.processing.Processor"    
    
    destinationDir = file('build/outputs/')
}

最为重要的是这一句:.net

include "META-INF/services/javax.annotation.processing.Processor" 

是必需要添加的,虽然咱们可能使用 Google 的 auto-service 库能够自动生成 META-INF/services/javax.annotation.processing.Processor 文件,可是打包时必定要将这个文件打入jar包,不然没法使用咱们的自定义注解。code

最后,咱们看一下完成以后compiler包的状况,咱们已经把javax.annotation.processing.Processor文件打入jar包了。orm


屏幕快照 2016-12-18 上午11.58.38.png

本文同步分享在 博客“fengzhizi715”(JianShu)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。blog

相关文章
相关标签/搜索