Home Android & Kotlin Books Kotlin Apprentice

A
Appendix A: Kotlin Platforms Written by Ellen Shapiro

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Now that you’ve learned about how to use Kotlin, you may be asking yourself: Where can I apply all of this knowledge?

There are many different platforms that allow you to use Kotlin as a programming language — read on to find out more info about what they are!

Kotlin on the JVM

In this book, we’ve discussed the fact that Kotlin was born to be compiled down to Java Virtual Machine bytecode. Kotlin’s interoperability with Java and compilation to JVM bytecode has been a major factor in its quick adoption.

Anything that runs Java can run Kotlin, and there are very few machines that can’t run Java.

There are a couple of key JVM-powered platforms wherein Kotlin’s adoption is rapidly increasing: Android and server-side.

Android

A huge driver of Kotlin’s adoption has been the ability to work around some of the limitations of Android and its various Integrated Development Environments (IDE).

Android-specific extensions

The Kotlin Android Extensions library, maintained by JetBrains, offers a number of ways to take advantage of code generation to make working with Android more convenient, safe and concise.

sharedPreferences.edit()
    .putBoolean("key", value)
    .putBoolean("another_key", anotherValue)
    .apply()
sharedPreferences.edit {
    putBoolean("key", value)
    putBoolean("another_key", anotherValue)
}

Domain-specific languages

Domain-Specific Languages, or DSLs, take code written in a given language and tailor it in a bespoke fashion to exactly the purpose for which you wish to use it. Kotlin makes creating DSLs fairly easy with its support for functions as parameters. Developers who use Kotlin (especially Android developers) have taken significant advantage of this.

findViewWithText("4").click()
findViewWithText("2").click()
findViewWithHint("Recipient").setText("foo@bar.com")
findViewWithText("Send").click()

Thread.sleep(1000)

findViewWithText("Success!")
payment {
    amount(4200)
    recipient("foo@bar.com")
}.send() {
    isSuccessful()
}

Kotlin on the server

While adoption of Kotlin on the server is not presently as wide as it is on Android, Kotlin is starting to make more and more moves towards wider server-side acceptance.

Kotlin to JavaScript

Kotlin can be cross-compiled (or “transpiled”) into Javascript, making it far easier to write type-safe web application code without having to give up some of the flexibility of JavaScript.

Kotlin/Native and Multiplatform

As you read about in Chapter 25, Kotlin/Native is a project from the Kotlin team that uses the LLVM compiler to create code that doesn’t need a virtual machine (such as the JVM) to run. This means that code written in Kotlin could be able to run even faster when it’s executing machine instructions for a specific OS and processor combination than it would by running against the JVM, since it avoids the extra step of having to be executed by a virtual machine. Running natively is also particularly helpful for code you want to run on iOS or macOS, since LLVM is the same compiler used for Objective-C and Swift on those platforms.

Where to go from here?

Here’s what we covered in this chapter:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.

Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:

© 2021 Razeware LLC

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Unlock Now

To highlight or take notes, you’ll need to own this book in a subscription or purchased by itself.