Engineers at Facebook parent Meta have detailed their multi-year effort to convert the codebases for its numerous Android apps from the Java programming language over to Kotlin.
That’s a large task for a company with thousands of developers and many large Android apps, including Facebook, Instagram, Messenger, Portal, and its Quest VR headset.
“Shifting away from Java, which we currently use for Android development, and over to Kotlin is not a trivial task,” notes Omer Strulovich, a software engineer at Facebook.
Also: Salary freezes and a return to the office? Not for software developers
The migration to Kotlin is underway, with Meta’s Android app repositories already consisting of 10 million lines of Kotlin code.
“Today, our Android apps for Facebook, Messenger, and Instagram each have more than 1 million lines of Kotlin code, and the rate of conversion is increasing. In total, our Android codebase has more than 10 millions lines of Kotlin code,” noted Strulovich.
Java is one of the top three programming languages, thanks to its dominance in enterprise applications, while Kotlin is a less popular but still top 20 programming language. Meta’s switch away from Java to Kotlin for Android app development is a milestone, given Meta’s scale and the size of its apps.
But, despite the benefits of Kotlin that motivated Meta’s switch, Strulovich outlines several significant disadvantages that make the move risky, at least for large apps.
Firstly, the large gap in popularity between Java and Kotlin leaves the latter with fewer tools, and those tools, having to account for Kotlin-Java interoperability, are complex. Despite the languages’ 100% interoperability, Meta has found it can’t remove all Java.
“Kotlin is a popular language, but compared with Java, the popularity gap is clear. Java is the world’s second or third most popular language (depending on how one measures this). This means fewer tools are available. Worse than that, all the Kotlin tools need to account for Kotlin and Java interoperability, which complicates their implementation,” notes Strulovich.
But Meta’s biggest concern is slower build times in Kotlin versus Java.
“We knew from the start that Kotlin’s build times would be longer than Java’s. The language and its ecosystem are more complicated, and Java had two decades of a head start to optimize its compiler. Since we own several large apps, the consequences of longer build times could negatively impact our developers’ experience.”
On that last point, Strulovich points to compile times after the HTTP client project, OkHttp, switched from Java to Kotlin in 2019. OkHttp is small, with only 24,000 lines of Kotlin. Its compile time in Java was 2.4 seconds compared to 10.2 seconds in Kotlin.
Meta engineers decided to convert almost all their code into Kotlin rather than writing new code using Kotlin and leaving most of the existing code in Java (the second approach is how the Linux kernel project and the Android Open Source Project are introducing Rust into their codebases).
Potentially good news for Kotlin adoption elsewhere is that Meta has released several in-house Kotlin conversion tools on GitHub that others might be able to use directly, or at the least could inspire others to find ways to automate the conversion.
As Facebook engineers detailed last year, while Kotlin adoption can be smooth, it is difficult at scale. Facebook uses its own Buck build system versus IntelliJ/Android Studio IDE, which use Gradle.
Strulovich outlines several open-source Kotlin tools that Meta contributed to, such as the “Pygments” library that it’s using to “bring the experience to par with Java”. Meta also built Ktfmt, a plugin for IntelliJ and Android Studio that provides a “deterministic Kotlin formatter based on the code and philosophy of google-java-format.”
Also: Small businesses need more help with tech. Here are five ways to get it
Strulovich says that, so far, Meta has on average seen an 11% reduction in the size of its codebase. The Google Home team reported a 33% reduction in its app’s codebase size when migrating new feature development to Kotlin.
“We have seen much higher numbers quoted online, but we suspect these numbers are derived from specific examples,” Strulovich said. “We are still happy about this number, as the lines removed are usually boilerplate code, which is less implicit than its shorter Kotlin counterpart.”
Kotlin was first developed by JetBrains, the maker of developer tools or IDEs like IntelliJ. Google has been using Kotlin for Android apps since 2017 and the pair have jointly supported the Kotlin language through the Kotlin Foundation since 2018.
In 2019, Google’s Android team adopted the “Kotlin-first” approach. Google has over 70 Android apps built at least partly with Kotlin, including Maps, Home, Play, Drive, and Messages. Google says, compared to Java, Kotlin is more efficient, produces safer code and a smaller codebase, and is 100% interoperable with the Java language – making the switch from Java codebases easier by allowing them to coexist.
Leave a Reply