为什么范围注解在 Dagger 2 中保留 RUNTIME?

Why scope annotations have RUNTIME retention in Dagger 2?

Dagger 2 有编译时验证,那么为什么作用域注解有 RUNTIME 保留?为什么在运行时需要它们?

虽然我不确定,但我猜 Scopes 一定有运行时保留,因为 JSR 330's @Scope requires scopes to have runtime retention. (@Qualifier has the same restriction.) Both Dagger 1 and Dagger 2 advertise JSR-330 compatibility, and (as David 在评论中提到)有很多运行时依赖注入解决方案绝对需要运行时保留范围注释。

特别是对于 Dagger,我无法想象为什么它们实际上也会在运行时被读取,尽管我想一些外部库可以使用注释。


根据评论编辑 + Android multidex 警告

这不一定是问题,无论是遗留问题还是其他问题:许多 DI 系统在运行时配置并需要注释。 Dagger 值得注意的是它没有,但规范的编写方式并未允许运行时保留是可选的。这使应用程序开发人员可以用不同的 JSR330 框架(如 Guice 或 Spring)替换 Dagger,并且它使库开发人员能够使 DI 兼容的工具或框架与它们是编译时配置还是运行时配置无关。规范的清晰度(即没有实现选项)和灵活性(编译时或运行时)似乎值得不必要的注释保留成本和学习成本。

但是,这 可以 在大型 Android 应用程序中成为问题 因为 ; older versions of Android fail to load runtime annotations if the annotated class is in a secondary dex. See this bug linked from the keepRuntimeAnnotatedClasses docs 了解详情。虽然你可以认为这是一个 Dagger 问题,因为 Dagger 适用于大型 Android 应用程序,但它更像是一个 Android 注释处理的错误,为此 Dagger 需要从 JSR-330 规范中分离出来以便自行处理。