alibaba / ARouter

💪 A framework for assisting in the renovation of Android componentization (帮助 Android App 进行组件化改造的路由框架)
Apache License 2.0
14.42k stars 2.59k forks source link

feat: add arouter ksp support #1030

Closed JailedBird closed 1 year ago

JailedBird commented 1 year ago

CHANGED README: https://www.yuque.com/docs/share/e7ababf1-9a7b-4273-8552-073fa493d9b8?# 《ARouter ksp feature》

CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.

CLAassistant commented 1 year ago

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

JailedBird commented 1 year ago

CHANGED README: https://www.yuque.com/docs/share/e7ababf1-9a7b-4273-8552-073fa493d9b8?# 《ARouter ksp feature》

jitpack依赖接入文档 https://www.yuque.com/docs/share/bbfb89a1-3398-4eac-9d8f-11202818d218?# 《Arouter-ksp接入》

ihrthk commented 1 year ago

@JailedBird 您好好,我使用这个PR生成的ARrouter,把我们的项目进行KSP迁移 在删除apply plugin: 'kotlin-kapt'后,构建过程中遇到这个报错

Task :lib_app_business:bundleLibRuntimeToJarDebug FAILED

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.

==============================================================================

2: Task failed with an exception.

==============================================================================

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0. Use '--warning-mode all' to show the individual deprecation warnings. See https://docs.gradle.org/6.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 35s 667 actionable tasks: 6 executed, 661 up-to-date

ihrthk commented 1 year ago

@JailedBird 你好,我运行ARouter和自己的项目发现,无论是Kotlin代码还是Java代码,最终都交由KSP生成了Kotlin代码,为什么要这样处理呢?可不可以Java使用APT,而 Kotlin 使用KSP 呢?

image

JailedBird commented 1 year ago

以Arouter注解处理器为例(同时支持 ksp和kapt) 在模块A中,使用Arouter注解处理器, 只能导入 ksp Arouter或者 kapt Arouter其中一种, 不能同时使用 ksp Arouter + kapt Arouter;
可以这样理解:ksp Arouter和kapt Arouter处理过程是在2个上下文中, 如果他们同时存在一个模块,根本没法发收集所有的路由信息, 此外 ksp的设计是兼容java的, 因此 ksp可以替换kapt (而kapt是为kotlin文件生成java存根的); 在模块A、C中, 使用注解处理器Arouter, A中可以ksp Arouter(前提是注解处理器B支持ksp) B中可以使用kapt Arouter, 模块之间是隔离的关系, 这样是可以的

JailedBird commented 1 year ago

在纯Java模块中(无kotlin), 保持 annotationProcessor arouter-compiler 导入即可(文档中有说明), 无需使用ksp导入, 哈哈
一是是为了避免新的bug[bug可能来自ksp本身、也可能来自我的Arouter ksp实现], 总之纯的Java代码的模块就别动了
二是在纯Java模块中, 迁移为ksp本身并不能带来太大的收益

JailedBird commented 1 year ago

初步分析:看log貌似是 这个文件冲突 (Entry name 'com/alibaba/android/arouter/routes/ARouter$$Group$$HomePageAppDelegate.class' collided) 排查步骤: 1、 如果从kapt迁移到ksp, 请先clean项目; 否则可能同名的新旧文件可能冲突 2、 检查带有HomePageAppDelegate 关键字的Route注解,在不同的模块中是否存在如下类似的注解 @Route(path = "/HomePageAppDelegate/xxx") , 这样是不允许的,具体规则可查看这个 https://github.com/alibaba/ARouter/issues/419 3、 这应该是在 ksp处理之后阶段的报错, 在编译时启用 --info 编译参数可查看部分ksp的编译日志(gradlew assembleDebug --info) 4、 可以尝试从https://juejin.cn/post/7002805341943496717 文章分析 Entry name 'xxx' collided的错误原因

ihrthk commented 1 year ago

@JailedBird 原因查到了,应该是APT和KSP都处理了,造成了重复

APT生成的Java代码 image

image

KSP生成的Kotlin代码

image image

GXSZone commented 1 year ago

kapt可以用吗

JailedBird commented 1 year ago

针对你需要生成路由的模块, 你可以使用kapt xxx 生成java相关路由文件(走的是AbstractProcessor处理器), 也可以使用ksp xxx生成对应的kotlin相关路由文件(走的是SymbolProcessorProvider处理器), 同一模块内只能选择一种处理器,不同模块可以混用