Meta has encountered a programming language he likes better than Java • The Register
Facebook parent Meta is migrating its Android app’s 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 version 1.0. The following year, it was adopted by Google as a premier (supported) language for Android development and placed under the direction of its own foundation, funded by JetBrains and Google.
Until then, most Android apps were written in Java. In May 2019, at its developer conference, Google started encouraging Android developers to favor Kotlin over Java. At the end of the year, the search giant said that 60% of the top 1,000 Android apps contained Kotlin code.
Google’s rationale for using Kotlin is that it’s more concise, more secure, supports structured concurrency for easier asynchronous code, and is interoperable with Java. It may also have something to do with Java steward Oracle, who spent more than a decade pursuing (and ultimately losing) an infringement suit against Google over the use of Java APIs in Android.
Meta seems to be intrigued by Kotlin as well, and as Facebook software engineer Omer Strulovich explains, the social advertising industry turned VR high roller has replaced Java code in many of its popular Android apps – Facebook, Instagram, Messenger, Portal and the Quest – with Kotlin.
Rightly so for a company founded as a social network, Meta wants to stay with the popular crowd.
“Kotlin is generally considered a better language than Java, with higher preference ratings than Java in the annual Stack Overflow developer survey,” Strulovich said, noting that Kotlin has become a popular language for Android development.”[s]o it makes sense that we are moving our Android development at Meta to Kotlin…”
But beyond its popularity in the Android ecosystem — which Strulovich concedes in the broader market is inferior to Java somewhere in the top three programming languages depending on who you ask — Kotlin has practical advantages.
These include: nullability, to help avoid null reference errors by supporting types that cannot be nulled; support for functional programming via inline functions and lambda expressions; more concise code; and support for Domain Specific Languages (DSL), programming languages with limited scope for particular tasks.
The transition also had its downsides. Mixed codebases can be difficult to maintain, and Kotlin, which has been around for less time than Java, has a less mature toolset. Redex, Facebook’s Android bytecode optimizer, needed an update. Pygments too, a syntax highlighter. Also, some internal compile-time bytecode optimization libraries didn’t work with Kotlin and needed to be updated. And new tools like Ktfmt, a deterministic Kotlin trainer, had to be built.
Our Android apps for Facebook, Messenger, and Instagram each have over a million lines of Kotlin code, and the conversion rate is growing
Meta could have decided to write only new code in Kotlin, but instead opted to convert all of its Android apps.
“Today, our Android apps for Facebook, Messenger, and Instagram each have over a million lines of Kotlin code, and the conversion rate is growing,” Strulovich said. “In total, our Android codebase contains over 10 million lines of Kotlin code.”
Strulovich didn’t say when Meta began this transition — the company’s developers discussed widespread use of Kotlin at a conference last year — or what percentage of its Android app code has been converted. The register asked Meta for additional details.
Ten million rows of Kotlin in this transition, Meta has realized some advantages. For example, although the Kotlin code is shorter, it may not be as much as hoped.
“On average, we saw an 11% reduction in the number of lines of code from this migration,” Strulovich said. “We’ve seen much higher numbers quoted online, but we suspect these numbers are derived from specific examples.”
Part of the disappointing code reduction is that much of the application code being translated consists of UI definitions, which remain the same whether in Java or Kotlin.
Strulovich also said there was no speed regression, so at least the converted apps maintained their performance. Build size of Kotlin-infused apps also didn’t prove to be an issue, although build times suffered – this was expected since Kotlin compilation hasn’t received as much attention as compiling Java.
Meta migration is underway and accelerating, Strulovich said. Meanwhile, Java’s popularity, by at least one measure, has declined by 4.7% over the past five years.
“Kotlin still lacks some of the tools and optimizations we’ve grown accustomed to working with Java,” Strulovich said. “But we are working to fill those gaps. As we move forward and these tools and libraries mature, we will also work to bring them back to the community.” ®