[英]When do I need to use Kapt in Gradle dependencies?
I have been installing dependencies in Gradle and I don't understand why sometimes I need to use kapt for libraries like lifecycle and room database to use @Something
annotations.我一直在 Gradle 中安装依赖项,但我不明白为什么有时我需要将 kapt 用于生命周期和房间数据库等库以使用
@Something
注释。 But in some libraries like Retrofit2 and Gson, I don't need use kapt and I can use annotations such as @SerializedName
?但是在像 Retrofit2 和 Gson 这样的一些库中,我不需要使用 kapt 并且我可以使用诸如
@SerializedName
注释?
Annotations (eg @Something
) are basically labels for code.注释(例如
@Something
)基本上是代码的标签。 You mark one part of the code so that some other code can find those markings.您标记代码的一部分,以便其他代码可以找到这些标记。
This " other code " is usually an Annotation Processor.这个“其他代码”通常是一个注释处理器。 It finds annotations and does something with code marked with those annotations.
它查找注释并使用标有这些注释的代码执行某些操作。 Eg it can generate new code (like Dagger, Butterknife, etc.).
例如,它可以生成新代码(如 Dagger、Butterknife 等)。
Depending on the way you introduce dependencies in your project, (depending on the keyword
you use - implementation
, api
, compileOnly
, runtimeOnly
, annotationProcessor
, kapt
, etc.), the dependency will be used by your project differently.根据您在项目中引入依赖项的方式(取决于您使用的
keyword
—— implementation
、 api
、 compileOnly
、 runtimeOnly
、 annotationProcessor
、 kapt
等),您的项目将使用不同的依赖项。
If you use annotationProcessor
, your dependency will not be packed within your app, but will be used during the compilation of your app.如果您使用
annotationProcessor
,您的依赖项将不会打包在您的应用程序中,而是会在您的应用程序编译期间使用。
You don't want to pack the compiler (the code that handles @AnAnnotation
) within your app, since it's just used to properly prepare the code of your app (and is never used within your application in Runtime).您不想在您的应用程序中打包编译器(处理
@AnAnnotation
的代码),因为它只是用于正确准备您的应用程序代码(并且从未在您的应用程序运行时使用)。
Think of it this way:可以这样想:
If you're going on a train and you need to print a train ticket, you don't want to carry a printer with you on the train.
如果您要坐火车并且需要打印火车票,您不想在火车上随身携带打印机。 After the printer is done printing the ticket, you take the ticket and go on the train.
打印机打印完车票后,您拿着车票上火车。 Printer has done its job already.
打印机已经完成了它的工作。 You can leave it.
你可以离开它。
If you mark some code with @AnAnnotation
you just want the library that handles that annotation to do its job and disappear.如果您使用
@AnAnnotation
标记一些代码,您只希望处理该注释的库完成其工作并消失。 Hence the special type of a dependency - annotationProcessor
.因此,依赖项的特殊类型 -
annotationProcessor
。
Now about kapt
.现在关于
kapt
。 This is simple.这很简单。 If you want to use Annotation Processors in projects with Kotlin code, just use
kapt
instead of annotationProcessor
.如果您想在带有 Kotlin 代码的项目中使用 Annotation Processors,只需使用
kapt
而不是annotationProcessor
。 Think of it as annotationProcessor
with Kotlin support.将其视为具有 Kotlin 支持的
annotationProcessor
。
Some libraries use @Annotations
differently.一些库以不同的方式使用
@Annotations
。 They do not cause any code to be generated in compile-time, but they use annotations in runtime.它们不会导致在编译时生成任何代码,但它们在运行时使用注解。
Those are usually reflection-based libraries that "look through" the code in Runtime.这些通常是基于反射的库,它们在运行时“查看”代码。 Just like Retrofit is looking through your
interface
when your app is executed.就像 Retrofit 在您的应用程序执行时查看您的
interface
。
That's why you include a library with @Annotations
normally within your application, and those annotations are packed within your apk for Runtime operation.这就是为什么您通常在应用程序中包含一个带有
@Annotations
的库,并且这些注释被打包在您的 apk 中用于运行时操作。
Summerizing:总结:
annotationProcessor
and kapt
keywords, are to help you specify how dependencies will be used in your project. annotationProcessor
和kapt
关键字用于帮助您指定如何在项目中使用依赖项。
If you want to introduce a library that uses annotations and generates some code, use kapt
not to "bloat" your apk with code that already has done its job, and will never be used again.如果你想引入一个使用注解并生成一些代码的库,请使用
kapt
不要用已经完成其工作的代码“膨胀”你的 apk,并且永远不会再次使用。
Usually, libraries interact with annotations in one of these two ways:通常,库通过以下两种方式之一与注解交互:
kapt
or annotationProcessor
.kapt
或annotationProcessor
。 Example: Retrofit, which accesses annotations using reflection and doesn't include an annotation processor.butterknife-compiler
module that contains the annotation processor.butterknife-compiler
模块。 You should use butterknife-compiler
as a kapt
or annotationProcessor
dependency, and not implementation
, api
or compile
, since you don't need the annotation processor during runtime.kapt
butterknife-compiler
作为kapt
或annotationProcessor
依赖项,而不是implementation
、 api
或compile
,因为您在运行时不需要注释处理器。 To answer your question, there's no generic way to know whether a library that relies on annotations comes with an annotation processor.要回答您的问题,没有通用的方法来了解依赖于注释的库是否带有注释处理器。 You should check the documentation for the specific library and follow installation instructions.
您应该查看特定库的文档并按照安装说明进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.