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.x , RxKotlin或什至是用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/