This article is more than 1 year old
Meta met a programming language it likes better than Java
Goggles giant on how it gave its Android apps a Kotlin makeover
Meta, parent of Facebook, is in the midst of migrating its Android app Java code to Kotlin, a younger programming language that also relies on the Java Virtual Machine, or JVM.
Created by software tool maker JetBrains, Kotlin debuted in 2011 and by 2016 had reached its 1.0 release. The following year, it was adopted by Google as a first-class (supported) language for Android development and was put under the management of its own foundation, funded by JetBrains and Google.
Until that point, most Android apps were written in Java. In May 2019, at its developer conference, Google began to encourage Android developers to favor Kotlin over Java. By the end of the year, the search giant said 60 percent of the top 1,000 Android apps contained Kotlin code.
Google's rationale for using Kotlin is that it's more concise, safer, supports structured concurrency for easier asynchronous code, and is interoperable with Java. It may also have had something to do with Java steward Oracle, which spent more than a decade pursuing (and ultimately losing) an infringement claim against Google over the use of Java APIs in Android.
Meta appears to be similarly intrigued by Kotlin and, as Facebook software engineer Omer Strulovich explains, the social ad biz turned VR high roller has been replacing the Java code in several of its popular Android apps – Facebook, Instagram, Messenger, Portal, and the Quest – with Kotlin.
- Oracle sets its own JDK free, sort of, for a while
- Java 17 arrives with long-term support: What's new, and is it falling behind Kotlin?
- Well, this makes scents: Kotlin code quality smells better than Java
Fittingly for a company founded as a social network, Meta wants to hang with the popular crowd.
"Kotlin is generally regarded as a better language than Java, with higher favorability ratings than Java in the yearly Stack Overflow developer survey," said Strulovich, noting that Kotlin has become a popular language for Android development "[s]o it only makes sense that we would shift our Android development at Meta to Kotlin…"
But beyond its popularity in the Android ecosystem – which Strulovich concedes in the broader market falls short of Java, somewhere in the top three programming languages depending on who you ask – Kotlin has some practical advantages.
These include: nullability, to help avoid null reference errors by supporting types that can't have a null value; support for functional programming via inline functions and lambda expressions; more concise code; and support for Domain Specific Languages (DSLs), limited scope programming languages for particular tasks.
The transition has had drawbacks, too. Mixed codebases can be difficult to maintain and Kotlin, having been around for less time than Java, has a less mature set of tools. Redex, Facebook's Android bytecode optimizer, needed to be updated. So too did Pygments, a syntax highlighter. Also, some internal libraries for optimizing bytecode during compilation didn't work with Kotlin and had to be updated. And new tools like Ktfmt, a deterministic Kotlin formatter, had to be built.
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
Meta could have decided to write only new code in Kotlin but instead it opted to convert all its Android apps.
"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," said Strulovich. "In total, our Android codebase has more than 10 million lines of Kotlin code."
Strulovich did not say when Meta began this transition – company developers discussed large scale Kotlin usage at a conference last year – or what percentage of its Android app code has been converted. The Register asked Meta for further details.
Ten million lines of Kotlin into this transition, Meta has realized some benefits. For example, though Kotlin code is shorter, it's perhaps not as much as hoped.
"On average, we’ve seen a reduction of 11 percent in the number of lines of code from this migration," said Strulovich. "We have seen much higher numbers quoted online, but we suspect these numbers are derived from specific examples."
Part of the underwhelming code reduction is that a lot of the app code being translated consists of user interface definitions, which remain the same whether they're in Java or Kotlin.
Strulovich also said that there were no speed regressions, so at least the converted apps maintained their performance. The build size of the Kotlin-infused apps also didn't prove to be a problem, though build times did suffer – that was expected since Kotlin compilation hasn't received as much attention as Java compilation.
Meta's migration is ongoing and accelerating, said Strulovich. Meanwhile, Java's popularity, by at least one measure, has declined 4.7 percent in the last five years.
"Kotlin still lacks some of the tools and optimizations that we have grown used to from working with Java," said Strulovich. "But we’re working to close those gaps. As we make progress and these tools and libraries mature, we will also work to release them back to the community." ®