liujingxing / rxhttp

🔥🔥🔥 Based on OkHttp encapsulation, support Kotlin Coroutines、RxJava2、RxJava3; 30s to get started.
https://juejin.im/post/5ded221a518825125d14a1d4
Apache License 2.0
3.74k stars 457 forks source link

ksp/kapt/annotationProcessor用法及区别 #447

Open liujingxing opened 1 year ago

liujingxing commented 1 year ago

1、用法

//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版本列表

2、相同点

ksp、kapt、annotationProcessor都是注解处理器,用于在编译期间,查找相关注解,生成相关代码

3、不同点

4、如何选择

如果你要项目未集成kotlin,那没得选,只能用annotationProcessor, 如果集成了kotlin, 则可以选择kspkapt, 但ksp弥补了kapt的缺陷,且编译速度更快,肯定就选择kspkapt是时候退出历史舞台了

HoneyWorld commented 1 year ago

大佬你这更新速度忒快了,我这本地kotlin还是1.5的,这更新的我想用只能升级Androidstudio版本,但又怕项目出问题,一直不敢动

liujingxing commented 1 year ago

大佬你这更新速度忒快了,我这本地kotlin还是1.5的,这更新的我想用只能升级Androidstudio版本,但又怕项目出问题,一直不敢动

我就当你是在夸我了,平均一个月更新一次,也还好;现在很稳定了,后续更新肯定会放缓