Kotlin and Modern JVM Development
Abstract
Kotlin was created by JetBrains, the company behind the IntelliJ IDEA development environment, to fix the verbosity and pitfalls of Java while remaining fully interoperable with it (see The JVM and Java Ecosystem). The first commit was made in November 2010, the project was announced in July 2011, and version 1.0 shipped in February 2016. JetBrains built it partly for its own engineers and partly, as it said openly, to sell more IDE licenses. In May 2017 Google announced official support for Kotlin on Android, in the middle of its copyright fight with Oracle over Java, and in May 2019 declared Android development “Kotlin-first”. By 2024 more than 95 percent of the top 1,000 Android apps contained Kotlin code. JetBrains later extended the language to native code, the browser and iOS through Kotlin Multiplatform, while Russia’s invasion of Ukraine forced the company to close the St Petersburg offices where much of Kotlin had been written.
JetBrains and Java
JetBrains was founded in Prague in 2000 by three Russian programmers, Sergey Dmitriev, Valentin Kipiatkov and Eugene Belyaev, with most of its development in St Petersburg. Its main product, IntelliJ IDEA, became one of the two leading Java development environments, and in 2013 Google based Android Studio on it.
By 2010 JetBrains’s own engineers wrote huge amounts of Java, and Java was changing slowly. Java 7 had been delayed for years by the end of Sun Microsystems and its sale to Oracle; lambda expressions did not arrive until Java 8 in 2014. Other JVM languages existed. Groovy was dynamically typed. Scala, from Martin Odersky’s group at EPFL in Lausanne, had the features JetBrains wanted, but, as JetBrains development lead Dmitry Jemerov told InfoWorld in 2011, “its most obvious deficiency is very slow compilation.” JetBrains wanted a statically typed language that compiled as fast as Java, could be mixed with existing Java code file by file, and could be supported well by its own IDE.
The first commit to the Kotlin repository was made on 8 November 2010. In July 2011 JetBrains unveiled “Project Kotlin”, which had then been in development for about a year. Like Java, named after an Indonesian island, it took its name from an island: Kotlin Island in the Gulf of Finland, west of St Petersburg, the site of the naval town of Kronstadt. The lead language designer was Andrey Breslav.
A month later Jemerov published “Why JetBrains needs Kotlin”, which was unusually frank about motives. The first was productivity: JetBrains was “still writing all of our IntelliJ-based IDEs almost entirely in Java.” The second was sales: the language would be free and open source, but support for enterprise frameworks would stay in the paid Ultimate edition of IntelliJ IDEA, and the company expected Kotlin to drive purchases. The third was attention: “new programming languages is a topic that many people really enjoy talking about,” and a JetBrains language would bring people to JetBrains tools. JetBrains open-sourced the compiler under the Apache 2 license in February 2012.
Design for Java Programmers
Kotlin’s designers aimed at Java programmers and did not try to surprise them. The language took ideas from Scala, C#, Groovy and others, but chose a smaller and more conservative set.
- Null safety. As in Swift, the type
Stringcannot holdnull; a variable that may be null is declaredString?, and the compiler refuses to call methods on it until the code has checked. The safe-call operator?.and the “Elvis” operator?:make the checks short. This targets theNullPointerException, the most common Java runtime error and a direct consequence of what Tony Hoare called his “billion-dollar mistake”. - Less boilerplate. A
data classgets its constructor, accessors,equals,hashCode,toStringandcopygenerated:data class User(val name: String, val age: Int)replaces dozens of lines of Java. Properties replace getter and setter methods, and type inference removes most repeated type names. - Smart casts. After
if (x is String), the compiler treatsxas aStringwithout a cast. - Extension functions. A library can add a method to an existing class, such as
String.isEmail(), without subclassing or modifying it, which let Kotlin give Java’s standard library a modern interface without replacing it. - Functional features. Lambdas, higher-order functions and immutable
valdeclarations were part of the language from the start, years before Java 8. - Sealed classes and
when. A closed set of subclasses can be matched exhaustively, an approach borrowed from functional languages.
Kotlin compiled to ordinary Java bytecode and could call Java code, and be called from it, with almost no glue. A team could write its next file in Kotlin inside an existing Java project, run both through the same build, and convert old files one at a time; IntelliJ included a Java-to-Kotlin converter. That incremental path removed the main risk of switching languages, and it was the main reason companies tried Kotlin.
Kotlin 1.0 was released on 15 February 2016, with a promise of backward compatibility.
Coroutines
Kotlin’s most important addition after 1.0 concerned concurrency. Java programs that waited for networks or disks either blocked a thread, which was expensive, or used callbacks, which made code hard to read. Kotlin 1.1 (2017) introduced coroutines as an experimental feature: functions marked suspend can pause without blocking a thread and resume later, so asynchronous code reads like sequential code. Unlike C#’s or JavaScript’s async/await, the language provided only the low-level mechanism, and a library, kotlinx.coroutines, built the higher-level patterns on top. Coroutines became stable with Kotlin 1.3 on 29 October 2018, together with the library’s 1.0 release. The library adopted “structured concurrency”, an idea formulated by Martin Sústrik in 2016 and popularized by Nathaniel J. Smith’s Python library Trio, in which every coroutine belongs to a scope that cancels its children when it ends; Swift and Java later built in the same idea.
Google’s Endorsement
Android had used the Java language since its launch, but not Sun’s or Oracle’s Java platform: Google had written its own virtual machine and re-implemented the Java class libraries. In August 2010 Oracle sued Google over that re-implementation, and the case was still going through the courts in 2017 (it ended with a Supreme Court decision for Google in April 2021; see Android). Android developers, meanwhile, were limited to an older subset of Java’s language features.
At Google I/O on 17 May 2017 Google announced official support for Kotlin on Android. Christina Lee, an Android engineer and Kotlin advocate, later recalled: “I think we underestimate how wild of a notion that was at the time.” A language from a third party, controlled by a company in Prague and St Petersburg, became an official language of the world’s most widely used mobile operating system. JetBrains reported that new Kotlin users jumped after the announcement. In October 2018 Google and JetBrains announced the Kotlin Foundation, which protects and promotes the language and appoints its lead language designer.
On 7 May 2019 Google went further and declared Android development “Kotlin-first”: new libraries, documentation and samples would be written for Kotlin first. Jetpack Compose, Android’s new declarative UI toolkit, reached 1.0 on 28 July 2021 and can be used only from Kotlin. By August 2022 Google’s own code base contained more than 8.5 million lines of Kotlin. Wikipedia, citing Google, reports that by 2024 over 95 percent of the top 1,000 apps on Google Play contained Kotlin code.
Beyond Android
Kotlin spread to servers through Spring, the dominant Java application framework, which added Kotlin support, and through JetBrains’s own framework Ktor. Gradle, the main build tool for Android and the JVM, adopted Kotlin as an alternative to Groovy for build scripts.
JetBrains also wanted Kotlin to escape the JVM. Kotlin/JS compiled to JavaScript from version 1.1, and Kotlin/Native compiled to machine code through LLVM, so that Kotlin could run on iOS and embedded systems. Kotlin Multiplatform (KMP) let developers share business logic across Android, iOS, desktop and web while writing platform-specific user interfaces, a different approach from Flutter and React Native, which share the interface as well. Kotlin/Native and KMP were declared stable in November 2023, and at Google I/O in May 2024 Google endorsed KMP for sharing code between Android and iOS. JetBrains’s Compose Multiplatform extended the Compose UI toolkit to iOS and desktop.
Kotlin 2.0, released in May 2024, replaced the compiler front end with a rewritten one, called K2, which JetBrains said compiled code faster and made the language easier to extend.
People and Politics
Andrey Breslav led Kotlin’s design from 2010 until 2020, when he left JetBrains and Roman Elizarov became project lead. Elizarov announced in October 2023 that he was leaving JetBrains for personal reasons; the Kotlin Foundation then unanimously appointed Mikhail Zarechenskii, who had led the language evolution team, as lead language designer.
The language’s Russian roots became a problem in 2022. On 11 March 2022, two weeks after Russia invaded Ukraine, JetBrains announced that it was suspending all sales and research and development in Russia indefinitely. It closed its Russian offices, stopped building a new campus in St Petersburg and helped about 250 employees leave the country. Kotlin development continued from JetBrains’s offices elsewhere in Europe, but the island the language was named after now lay in a country its developers had left.
Dead End
The most instructive failure in Kotlin’s history was technical: the original memory model of Kotlin/Native.
On the JVM, Kotlin inherited Java’s garbage collector and threads. Kotlin/Native had to provide its own memory management, and its first design tried to rule out data races by construction. An object could be used by only one thread at a time unless it was frozen, made permanently immutable, before being shared. Mutating a frozen object raised an InvalidMutabilityException at runtime; touching an unfrozen object from the wrong thread raised IncorrectDereferenceException.
The design was sound in theory and a constant source of trouble in practice. It differed from the JVM rules that Kotlin programmers knew, so code that worked on Android crashed on iOS. Libraries had to be rewritten for it, and many common patterns, such as a cache shared between threads, became awkward. Multiplatform developers named it as the biggest obstacle to sharing code. JetBrains reversed course: a new memory manager with a tracing garbage collector and JVM-like sharing rules became the default in Kotlin 1.7.20 in 2022, freezing was deprecated, and the old memory manager was removed from the compiler in Kotlin 1.9.20. The episode cost the multiplatform effort several years, and it showed that compatibility with what programmers already know mattered more for Kotlin than a stricter model.
📚 Sources
- Kotlin official site
- Wikipedia: Kotlin (programming language): first commit 8 November 2010, July 2011 announcement, open source February 2012, 1.0 on 15 February 2016, name, Kotlin-first, KMP stable, 2.0, Google Play statistic
- Wikipedia: JetBrains: founding in Prague in 2000, founders
- InfoWorld, “JetBrains readies JVM language Kotlin” (July 2011): Jemerov on Scala’s compilation speed
- Dmitry Jemerov, “Why JetBrains needs Kotlin”, JetBrains Blog (August 2011)
- JetBrains Blog, “Kotlin 1.3 Released with Coroutines, Kotlin/Native Beta, and more” (October 2018)
- Android Developers Blog, “Android Announces Support for Kotlin” (17 May 2017) and JetBrains Blog, “Kotlin on Android. Now official” (17 May 2017)
- Android Developers Blog, “Celebrating 5 years of Kotlin on Android” (August 2022)
- 9to5Google, “Google teams up with JetBrains to form Kotlin Foundation” (4 October 2018)
- Android Developers Blog: Google I/O 2019, Kotlin-first
- Android Developers Blog, “Jetpack Compose is now 1.0” (28 July 2021)
- JetBrains Blog, “Roman Elizarov is the new Project Lead for Kotlin” (November 2020) and Andrey Breslav: bio
- JVM Weekly, “Major changes in Kotlin’s leadership: Roman Elizarov departs from JetBrains” (October 2023) and Kotlin Foundation, “Mikhail Zarechenskii named the new Lead Language Designer”
- JetBrains Blog, “JetBrains’ Statement on Ukraine” (11 March 2022) and Jobbatical: JetBrains relocation case study and JetBrains Blog, “Update on JetBrains’ Statement on Ukraine” (6 December 2022)
- Kotlin documentation: Migrate to the new memory manager and Touchlab: Kotlin/Native concurrency
- Wikipedia: Structured concurrency
- Kevin Galligan, “Why the Kotlin/Native memory model cannot hold” (ITNEXT)
- Image: Kotlin Island west side.jpg by Pavlikhin (CC BY-SA 4.0), via Wikimedia Commons