简体   繁体   English

我什么时候需要在 Gradle 依赖项中使用 Kapt?

[英]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 —— implementationapicompileOnlyruntimeOnlyannotationProcessorkapt等),您的项目将使用不同的依赖项。

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. annotationProcessorkapt关键字用于帮助您指定如何在项目中使用依赖项。

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:通常,库通过以下两种方式之一与注解交互:

  • Using reflection.使用反射。 Library code can query annotations at runtime to perform certain logic.库代码可以在运行时查询注释以执行某些逻辑。 These libraries would normally be packaged as a single artifact and would not require using kapt or annotationProcessor .这些库通常会打包为单个工件,不需要使用kaptannotationProcessor Example: Retrofit, which accesses annotations using reflection and doesn't include an annotation processor.示例:Retrofit,它使用反射访问注释并且不包含注释处理器。
  • Using an annotation processor.使用注释处理器。 Annotation processors are compiler plugins that get invoked before the main compilation step, can access annotations and the code around them, and perform tasks based on this input.注释处理器是编译器插件,在主要编译步骤之前被调用,可以访问注释及其周围的代码,并根据此输入执行任务。 Annotation processors usually come in separate artifacts since they contain code that is not needed at runtime, hence it shouldn't be packaged into your APK.注释处理器通常来自单独的工件,因为它们包含运行时不需要的代码,因此不应将其打包到您的 APK 中。 Example: Butterknife, which processes annotations during compilation and comes with a separate butterknife-compiler module that contains the annotation processor.示例:Butterknife,它在编译期间处理注释,并带有一个包含注释处理器的单独的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作为kaptannotationProcessor依赖项,而不是implementationapicompile ,因为您在运行时不需要注释处理器。

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.

相关问题 没有Kotlin代码时,我真的需要使用kapt吗? - Do I really need to use kapt when there is no kotlin code? 我是否需要添加 kapt “com.android.databinding:compiler:$compiler_version” gradle 条目? - Do I need to add kapt “com.android.databinding:compiler:$compiler_version” gradle entry? 什么是 Android kapt 及其用法? 添加依赖项时Gradle中的anootation处理器和kapt有什么区别? - What is Android kapt and its usage? What's the difference between anootation processor and kapt in Gradle when adding dependencies? 升级 kotlin 或 gradle 时出现 Room kapt 错误 - Room kapt error when upgrading kotlin or gradle 在gradle中使用相同的版本依赖项时出错 - Getting error when I use same version dependencies in gradle 如果依赖项在aar库中声明,我是否需要在应用程序的build.gradle中对同一库进行delcare? - if the dependencies were declared in the aar library, do I need to delcare the same library in the build.gradle of the application? 我们是否需要Android中的GoogleSignIn中的Gradle依赖关系? - Do we need Gradle Dependencies in GoogleSignIn in Android or is it a Deprecated approach? 在gradle和kapt中使用useBuildCache - Using useBuildCache in gradle and kapt Gradle:如何实际使用自定义依赖项配置或将外部依赖项交换为本地依赖项? - Gradle: How do I actually use a custom dependency configuration OR swap external dependencies for local ones? 当我在gradle配置中使用resourcePrefix属性时,如何在lint检查中排除依赖库? - when I use resourcePrefix attribute in gradle config, how to exclude dependencies lib at lint check?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM