Open liujingxing opened 1 year ago
//annotationProcessor无需依赖任何插件,ksp/kapt选择对应的插件即可 plugins { // id 'kotlin-kapt' id 'com.google.devtools.ksp' version '1.8.0-1.0.9' } //ksp/kapt/annotationProcessor 传参方式如下 //ksp传参方式 ksp { arg("rxhttp_rxjava", "3.1.1") } //kapt传参方式 kapt { arguments { arg("rxhttp_rxjava", "3.1.1") } } //annotationProcessor传参方式,需要在android.defaultConfig标签下使用 javaCompileOptions { annotationProcessorOptions { arguments = [ rxhttp_rxjava: '3.1.1', ] } } dependencies { //三选一 ksp 'com.github.liujingxing.rxhttp:rxhttp-compiler:x.x.x' //kapt 'com.github.liujingxing.rxhttp:rxhttp-compiler:x.x.x' //annotationProcessor 'com.github.liujingxing.rxhttp:rxhttp-compiler:x.x.x' }
对于ksp1.8.0-1.0.9版本, 1.8.0是kotlin版本,1.0.9是ksp的真实版本,如果想使用其它版本,请点击ksp版本列表
1.8.0-1.0.9
ksp、kapt、annotationProcessor都是注解处理器,用于在编译期间,查找相关注解,生成相关代码
annotationProcessor仅会检索Java代码,且没有用到注解时,不会工作;生成的代码在build/generated/ap_generated_sources目录下
annotationProcessor
build/generated/ap_generated_sources
kapt是Kotlin时代的产物,会检索kotlin/java代码,但kotlin的一些专有属性拿不到,如const关键字、伴生对象等在代码层面无法判断,同样的,没有用到注解时,不会工作;生成的代码在build/generated/source/kapt目录下
kapt
const
build/generated/source/kapt
ksp则是最新的注解处理器,编译速度号称是kapt的2倍有余,且完全弥补了kapt/annotationProcessor的缺陷,但纯Java项目用不了,必需要集成kotlin;生成的代码在build/generated/ksp目录下
ksp
kapt/annotationProcessor
build/generated/ksp
如果你要项目未集成kotlin,那没得选,只能用annotationProcessor, 如果集成了kotlin, 则可以选择ksp或kapt, 但ksp弥补了kapt的缺陷,且编译速度更快,肯定就选择ksp,kapt是时候退出历史舞台了
大佬你这更新速度忒快了,我这本地kotlin还是1.5的,这更新的我想用只能升级Androidstudio版本,但又怕项目出问题,一直不敢动
我就当你是在夸我了,平均一个月更新一次,也还好;现在很稳定了,后续更新肯定会放缓
1、用法
对于ksp
1.8.0-1.0.9
版本, 1.8.0是kotlin版本,1.0.9是ksp的真实版本,如果想使用其它版本,请点击ksp版本列表2、相同点
ksp、kapt、annotationProcessor都是注解处理器,用于在编译期间,查找相关注解,生成相关代码
3、不同点
annotationProcessor
仅会检索Java代码,且没有用到注解时,不会工作;生成的代码在build/generated/ap_generated_sources
目录下kapt
是Kotlin时代的产物,会检索kotlin/java代码,但kotlin的一些专有属性拿不到,如const
关键字、伴生对象等在代码层面无法判断,同样的,没有用到注解时,不会工作;生成的代码在build/generated/source/kapt
目录下ksp
则是最新的注解处理器,编译速度号称是kapt
的2倍有余,且完全弥补了kapt/annotationProcessor
的缺陷,但纯Java项目用不了,必需要集成kotlin;生成的代码在build/generated/ksp
目录下4、如何选择
如果你要项目未集成kotlin,那没得选,只能用
annotationProcessor
, 如果集成了kotlin, 则可以选择ksp
或kapt
, 但ksp
弥补了kapt
的缺陷,且编译速度更快,肯定就选择ksp
,kapt
是时候退出历史舞台了