超越Android:探索Kotlin的应用领域

by Adam Arold

亚当·阿罗德(Adam Arold)

超越Android:探索Kotlin的应用领域 (Going beyond Android: exploring Kotlin’s areas of application)

If you have written something in Kotlin, chances are that you wrote it for Android. Kotlin, however, has other areas where it can be useful. In the following series, we’ll explore in what other fields Kotlin can shine and discuss how you can take advantage of them.

如果您使用Kotlin编写过某些内容,则很可能是您为Android编写的。 但是,Kotlin在其他领域可能会有用。 在以下系列中,我们将探讨Kotlin在其他领域可以发挥的作用,并讨论如何利用它们。

Even though Kotlin started out as a language for the JVM, its creators have started to give us options for using the language on other platforms as well. The Kotlin Frontend Plugin is nothing new, but now we can go native or create multiplatform projects.

即使Kotlin最初是作为JVM的一种语言,它的创建者也开始为我们提供在其他平台上使用该语言的选择。 Kotlin前端插件并不是什么新鲜事物,但是现在我们可以使用本机或创建多平台项目了

What is also interesting is that on the JVM you can also use Kotlin on the backend with great effect. Using Spring with Kotlin is becoming easier with the advent of Spring 5 which has built-in support for Kotlin, but you can choose from a variety of technologies, like vert.x, RxKotlin or even tools written in Kotlin like Hexagon.

同样有趣的是,在JVM上,您也可以在后端使用Kotlin,效果非常好。 随着Spring 5的内置支持 ,Spring与Kotlin结合使用变得越来越容易,但是您可以选择多种技术,例如vert.xRxKotlin或什至是用Kotlin编写的工具(例如Hexagon)

后端的Kotlin (Kotlin on the backend)

As I have written before, I think that the interop between Java and Kotlin is quite seamless. This also means that using Kotlin in place of Java on the backend is rather easy. Apart from a few nuisances, you can pretty much start writing your new features in Kotlin within your Java project. Or if you just want to try it out, you can start by writing your tests with it.

正如我之前写的 ,我认为Java和Kotlin之间的互操作是相当无缝的。 这也意味着在后端使用Kotlin代替Java很容易。 除了一些麻烦之外,您几乎可以开始在Java项目中的Kotlin中编写新功能。 或者,如果您只想尝试一下,则可以开始编写测试。

If you look around, it seems that companies with a big slice of the backend pie also have the same thought: the new version of Spring has some features dedicated to Kotlin, and you can even use Kotlin to write your Gradle scripts using the kotlin-dsl.

如果环顾四周,似乎后端派中有很大一部分的公司也有相同的想法:Spring的新版本具有一些Kotlin专用的功能,您甚至可以使用Kotlin使用kotlin-编写Gradle脚本- dsl

What is interesting to note here is that you don’t need Kotlin support for any of these libraries, because the Java interop features of Kotlin are so good. In the next article in this series, we’ll explore how you can write backend code with and without libraries written in Kotlin. We’ll also look into how you can tinker with your existing programs written in Java.

这里有趣的是您不需要这些库中的任何一个Kotlin支持 ,因为Kotlin的Java互操作性非常好。 在本系列的下一篇文章中,我们将探讨如何使用和不使用Kotlin编写的库来编写后端代码。 我们还将研究如何修改Java编写的现有程序。

将Kotlin编译为Javascript (Compiling Kotlin to Javascript)

When trying to compile Kotlin to Javascript, you have two options: the kotlin2js plugin and the kotlin-frontend-plugin.

尝试将Kotlin编译为Javascript时,您有两个选择: kotlin2js插件和kotlin-frontend-plugin

The former is a simple way to turn your code to JS without the hassle of managing external dependencies. It just works out of the box and results in a .js file which you can copy to your static assets folder.

前者是将代码转换为JS的简单方法,而无需管理外部依赖项。 它开箱即用,并生成一个.js文件,您可以将其复制到静态资产文件夹中。

The latter is a little more involved, but it lets you use both Maven and npm dependencies.

后者要复杂一些,但是它可以让您同时使用Maven和npm依赖项。

With the use of these, you can easily go full stack — but they do not let you share code between your backend and your frontend project. Check out this TodoMVC implementation which I have written if you are interested in how this works.

使用这些功能,您可以轻松地进行完整堆栈处理-但是它们不允许您在后端项目和前端项目之间共享代码。 如果您对此工作原理感兴趣,请查看我编写的 TodoMVC实现。

走向本土 (Going native)

Have you ever tried running Kotlin in an embedded environment or compiled it into a single binary? Enter Kotlin Native which lets you do just that:

您是否曾经尝试过在嵌入式环境中运行Kotlin或将其编译为单个二进制文件? 输入Kotlin Native,您可以执行以下操作:

Kotlin/Native is an LLVM backend for the Kotlin compiler, runtime implementation, and native code generation facility using the LLVM toolchain.

Kotlin / Native是LLVM后端,用于LLVM工具链的Kotlin编译器,运行时实现和本机代码生成工具。

While this is still in a pre-release version (0.4 at the time of writing) Kotlin Native is a promising development. It tries to fill in the holes which are currently present, and lets you use Kotlin in some areas where it was not feasible to do so, like:

尽管它仍处于预发行版本中(在撰写本文时为0.4 ),但Kotlin Native是一个有前途的开发。 它尝试填补当前存在的漏洞,并让您在不可行的某些区域使用Kotlin,例如:

  • iOS applications (reusing code with Android)

    iOS应用程序(在Android中重用代码)
  • Embedded systems/IoT (e.g., Arduino and beyond)

    嵌入式系统/物联网(例如,Arduino和更高版本)
  • Data analysis and Scientific Computing

    数据分析与科学计算
  • Server-side and Microservices (low-footprint executables, utilizing the power of coroutines)

    服务器端和微服务(占用空间较小的可执行文件,利用协程的强大功能)
  • Game Development

    游戏开发

多平台项目 (Multiplatform projects)

While it is all well and good that you can now use Kotlin on a multitude of platforms, what good does it do if you can’t wire your different codebases together? With the release of Kotlin 1.2, you can now share code between platforms reliably.

虽然现在可以在多种平台上使用Kotlin很好,但是如果您不能将不同的代码库组合在一起又有什么用呢? 随着Kotlin 1.2的发布 ,您现在可以在平台之间可靠地共享代码。

This works by dividing your codebase into common and platform modules and by using the expect + actual API. This lets you define classes and functions which will be implemented on each platform. You can take a look at this video by Dmitry Jemerov, who is the Kotlin IDE Team Lead at JetBrains, to get a better understanding of this topic.

通过将您的代码库划分为通用模块和平台模块,并使用Expect +实际 API,可以实现此目的。 这使您可以定义将在每个平台上实现的类和函数。 您可以观看JetBrains的Kotlin IDE团队负责人Dmitry Jemerov的这段视频 ,以更好地理解这一主题。

结论 (Conclusion)

We’ve explored some areas where Kotlin can shine as well as how you can glue together your multiplatform projects. In the next articles, we’ll look into each of these options in a little more detail.

我们探索了Kotlin可以发挥作用的一些区域,以及如何将多平台项目粘合在一起。 在下一篇文章中,我们将更详细地研究每个选项。

Thanks for reading! You can read more of my articles on my blog.

谢谢阅读! 您可以在我的博客上阅读更多我的文章。

翻译自: https://www.freecodecamp.org/news/beyond-android-exploring-kotlin-areas-of-application-5d28ff66a446/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值