Programming languages: why Meta is moving its Android apps from Java to Kotlin
Engineers at Meta, Facebook’s parent company, detailed their multi-year effort to convert the codebases of its many Android apps from the Java programming language to Kotlin.
It’s a big task for a company with thousands of developers and many great Android apps, including Facebook, Instagram, Messenger, Portal and its Quest VR headset.
“Getting away from Java, which we currently use for Android development, and Switching to Kotlin is not a trivial task“, notes Omer Strulovich, software engineer at Facebook.
Migration to Kotlin is underway, with Meta’s Android app repositories already comprising 10 million lines of Kotlin code.
“Today, our Android apps for Facebook, Messenger, and Instagram each have over a million lines of Kotlin code, and the conversion rate is increasing. In total, our Android code base has over 10 million lines of Kotlin code. Kotlin code,” Strulovitch noted.
Java is one of the top three programming languages, thanks to its dominance in enterprise applications, while Kotlin is a less popular but still 20 best programming languages. Moving Meta from Java to Kotlin for Android app development is a big step, considering the scale of Meta and the size of its apps.
But, despite Kotlin’s advantages that prompted the change to Meta, Strulovich outlines several significant drawbacks that make the move risky, at least for large apps.
First, the wide popularity gap between Java and Kotlin leaves the latter with fewer tools, and those tools, having to take Kotlin-Java interoperability into account, are complex. Despite 100% language interoperability, Meta discovered that it could not remove all of Java.
“Kotlin is a popular language, but compared to Java, the popularity gap is clear. Java is the second or third most popular language in the world (depending on how you measure it). That means fewer tools are available. Worse than that, all Kotlin tools have to consider interoperability between Kotlin and Java, which complicates their implementation,” notes Strulovich.
But Meta’s biggest concern is the slow build times in Kotlin compared to 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 was two decades ahead of schedule to optimize its compiler. As we have several large applications, the consequences of longer build times could negatively impact the experience of our developers.”
On this last point, Strulovich indicates that it is necessary to compile the times after the HTTP client project, OkHttp, moved from Java to Kotlin in 2019. OkHttp is small, with only 24,000 rows of Kotlin. Its compile time in Java was 2.4 seconds compared to 10.2 seconds in Kotlin.
The meta engineers decided to convert almost all their code to 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 open source android project are introduce Rust into their codebases).
Potential good news for Kotlin adoption elsewhere is that Meta has released several internal Kotlin conversion tools on GitHub that others may be able to use directly, or at least inspire others to find ways to convert. automate the conversion.
As Facebook engineers detailed Last year, while Kotlin adoption can be fluid, it’s been difficult at scale. Facebook uses its own Buck build system compared to IntelliJ/Android Studio IDE, which uses Gradle.
Strulovich describes several open-source Kotlin tools that Meta has contributed to, such as the “Pygments” library that he uses to “bring the experience to the same level as Java.” Meta has also built Ktfmt, a plugin for IntelliJ and Android Studio that provides a “deterministic Kotlin trainer based on the google-java format code and philosophy”.
Strulovich says that, so far, Meta has on average seen an 11% reduction in the size of its code base. The Google Home team reported a 33% reduction in the size of their app’s code base when migrating new feature development to Kotlin.
“We’ve seen much higher numbers cited online, but we suspect those numbers are derived from specific examples,” Strulovich said. “We’re still happy with this number, because 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 in part with Kotlin, including Maps, Home, Play, Drive, and Messages. According to Google, compared to Java, Kotlin is more efficient, produces safer code and a smaller codebase, and is 100% interoperable with the Java language, making it easier to transition from Java codebases by allowing them to coexist.