kotlin kapt支持_Android的Kotlin KAPT概览— Code-In Mobile

kotlin kapt支持

It has been three years since Kotlin was introduced to the Android world. Like many other programming languages, annotations are a needed and powerful tool, but surprisingly Kotlin was not compatible with Java’s Annotation Processing Tools. So let’s dive into what is KAPT for Android.

自Kotlin进入Android世界以来已经三年了。 像许多其他编程语言一样,注释是必需且功能强大的工具,但是令人惊讶的是Kotlin与Java的注释处理工具不兼容。 因此,让我们深入了解什么是适用于Android的KAPT。

Image for post

何时使用KAPT? (When to use KAPT?)

The first question that usually comes to our mind when talking about annotation processing in Android, especially coming from a Java background, is whether you should be using KAPT. The answer is, KAPT is the official tool for doing annotation processing in Kotlin, thus the answer is always. This is as long as you need annotation processing, otherwise, why add it?

在谈论Android中的注释处理时(尤其是来自Java背景的),通常会想到的第一个问题是您是否应该使用KAPT。 答案是,KAPT是Kotlin中进行批注处理的官方工具,因此答案始终是。 只要您需要注释处理,否则为什么要添加它?

将KAPT包含在Android项目中 (Including KAPT in an Android project)

To start using KAPT in an Android project, you should add it in the app gradle dependencies as you do with any other external tool. Use the following line to include it:

要开始在Android项目中使用KAPT,您应该像使用其他任何外部工具一样将其添加到应用gradle依赖项中。 使用以下行将其包括在内:

apply plugin: 'kotlin-kapt'

It is that simple, but there is a common error when adding this dependency to the project. The error says “Kotlin plugin should be enabled before ‘kotlin-kapt’”, the solution to this is simple, but when you have tons of lines of code it can be confusing. What this error means is that when adding the above line to your Gradle file, you need to add the Kotlin plugin first like this:

就是这么简单,但是在将此依赖项添加到项目时会出现一个常见错误。 该错误显示“应该在'kotlin-kapt'之前启用Kotlin插件”,解决方法很简单,但是当您有成千上万行代码时,可能会造成混淆。 该错误的意思是,将上述行添加到Gradle文件中时,您需要像这样首先添加Kotlin插件:

apply plugin: 'kotlin-android' 
apply plugin: 'kotlin-kapt'

If you switch lines 1 and 2 you will be getting the error above.

如果切换第1行和第2行,您将得到上面的错误。

Image for post

KAPT的用途是什么? (What is KAPT used for?)

The answer to this is straight, it is used for annotation processing in Kotlin. But if you are wondering this it probably means you are not familiar with annotation processing.

答案很简单,它用于Kotlin中的注释处理。 但是,如果您想知道这可能意味着您不熟悉注释处理。

Let’s first learn what are annotations. Annotations are the words you see at the top of files and methods preceded with an ‘@’ symbol. The most common example that you may have already seen is the @Override annotation. But there are many more, some of which can be directly interpreted by Kotlin, those are called built-in annotations. Others need an ‘Annotation Processor’, also known as KAPT, to be used in your code.

首先让我们了解什么是注释。 注释是您在文件和方法顶部看到的带有“ @”符号的单词。 您可能已经看到的最常见的示例是@Override注释。 但是还有更多,其中一些可以由Kotlin直接解释,这些被称为内置注释。 其他人则需要在代码中使用“注释处理器”(也称为KAPT)。

The definition of annotations in the Kotlin documentation states the following:

Kotlin文档中的注释定义如下:

Annotations are means of attaching metadata to code.

注释是将元数据附加到代码的方法。

https://kotlinlang.org/docs/reference/annotations.html

https://kotlinlang.org/docs/reference/annotations.html

If you want to know more about annotations the documentation above should be helpful.

如果您想了解有关注释的更多信息,那么上面的文档应该会有所帮助。

Image for post

好的,但是...注释处理器的作用是什么? (Ok, but… What does an Annotation Processor do?)

Annotation Processors date back to when Android was based on Java. There are many tutorials on annotations in Java because at some time in the past they became very powerful (which they are) and popular tools.

注释处理器的历史可以追溯到Android基于Java的时候。 Java上有许多关于注释的教程,因为在过去的某个时候它们变得非常强大(它们是)并且是流行的工具。

Whether it is in Java or Kotlin, annotation processors are tools that run through your code during compile time and generate code themselves for your app. This explains why they are so powerful, if you know how to use annotations, they can save time for you by generating code themselves.

不管是Java还是Kotlin,注释处理器都是在编译时贯穿代码并自行为应用生成代码的工具。 这就解释了为什么它们如此强大,如果您知道如何使用注释,它们可以通过自己生成代码来为您节省时间。

A great thing about KAPT is that if you have a project with both Java and Kotlin files, it can take care of both. The opposite is not true as APT (Java annotation processing tool) can’t interpret Kotlin annotations.

KAPT的一个优点是,如果您有一个同时包含Java和Kotlin文件的项目,则可以同时处理这两个项目。 相反,事实并非如此,因为APT(Java注释处理工具)无法解释Kotlin注释。

Image for post

结语 (Wrapping up)

Now you should have a clearer view of what KAPT for Android is. As you dive deeper into the world of annotations you will learn that you can create your own annotations and make your coding more practical and safe.

现在,您应该对Android的KAPT有更清晰的了解。 当您深入了解批注的世界时,您将了解到可以创建自己的批注并使代码更加实用和安全。

If you have any doubts don’t hesitate to leave a comment and I will reply as soon as possible.

如有任何疑问,请随时发表评论,我会尽快答复。

See you next time!

下次见!

Evana Margain Puig

伊凡娜·玛金·普伊格

翻译自: https://levelup.gitconnected.com/a-look-into-kotlin-kapt-for-android-code-in-mobile-46ca10d8cb63

kotlin kapt支持

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值