Search

Kotlin: The Solution to "The Android Problem?"

The Java programming language has progressed rapidly in the last few years. With the new release cadence, we’ve seen language features and enhancements introduced that make modern Java a much more enjoyable language to use, like local variable type inference (Java 10), switch expressions (Java 14), text blocks (Java 15), records (Java 16), pattern matching for instance (Java 16), and sealed types (Java 17). From our experience, these updates can make your code a lot more expressive and concise, so check them out if you haven’t already!


The problem that Java developers targeting Android face is that these shiny new features are off-limits. Android currently only supports up to Java 8 for development, and even that is a recent upgrade with the addition of library desugaring in the Android Gradle Plugin 4.0. For context, Java 8 was released in March 2014, over 8 years ago! For us at Camgian, this constraint means that all of our shared backend code is stuck on an 8-year-old release. Even with support for Java 11 language features in newer releases of the Android Gradle Plugin, Android still lacks implementations for the full suite of Java 11 APIs, making it unsafe to upgrade.


What are developers to do in the face of this problem? For a while we have tried the “sit around and wait” approach, hoping that Android would eventually allow us start using some of the new language features that make modern Java so appealing. Yet with each new Java release, my hopes have been fading. We are now at Java 18, a full 10 releases past where much of our code is sitting. Common Java tools such as Error Prone and Checkstyle have dropped support for Java 8 in their recent releases, making it even less desirable to continue to maintain a large Java 8 code base. As much as our team has invested in and enjoyed working in Java over the years, this situation has forced me to look elsewhere for answers.


When I started learning more about Kotlin this year, I didn’t realize I had stumbled upon a way out. Created by JetBrains in 2011, Kotlin is a programming language designed quite simply as a better alternative to Java. Learning from Java’s mistakes and building upon our contemporary understanding of programming language design, Kotlin compiles to bytecode just like Java and can run on any JVM. It’s also fully interoperable with existing Java code and libraries, meaning that you don’t have to scrap everything and start over! You can choose to start writing new code in Kotlin and slowly migrate your existing code over time. IntelliJ even has quick tools for converting entire Java files to Kotlin (Ctrl + Alt + Shift + K).


Developers who use Kotlin (myself included) really love it. Check out some of the syntax examples on the official website, and you’ll get a feel for how much cleaner and expressive it is than Java. Kotlin has caught on quickly in the last few years, with Google announcing it as their preferred language (over Java) for Android development in 2019. On top of that, most of the new Jetpack APIs and features for Android are Kotlin only, such as Jetpack Compose.


With Kotlin, you get equivalents for pretty much all of the new language features that have been added in recent Java releases. Kotlin also has its own set of really neat features that you won’t find anywhere in Java, such as default and named parameters, extension functions, operator overloading, a null-safe type system, and coroutines.

So if you’re in the same boat we are, I’d highly encourage you to check out Kotlin. It will help speed up and enhance your development, leaving the Java compatibility headaches in the past forever. With full Java interoperability, you can adopt it at your own pace, making it almost a no-brainer. Given the way Google has signaled its trajectory with the Android ecosystem, spending the time now to learn and begin using Kotlin should pay off significantly down the road.


And lastly, don’t get fooled into thinking that Kotlin is only an Android thing. Kotlin has applications in backend development (with first-class support in Spring 5.0 and with libraries like ktor), Gradle build scripts (with the Kotlin DSL), and even data science (see kotlin-jupyter). There’s Kotlin/JS and Kotlin/Native that expand Kotlin beyond the JVM. If you’re really ambitious, you can look into JetBrains’ Compose Multiplatform framework for sharing UI code between Android, Desktop, and Web. At Camgian, we are starting to explore Kotlin as a language for our entire engineering organization. If that sounds exciting to you, be sure to check out our job postings on the careers page!

83 views0 comments