From zero to 10 million lines of Kotlin

We’re sharing lessons learned from shifting our Android development from Java to Kotlin. Kotlin is a popular language for Android development and offers some key advantages over Java.  As of today,…

Omer Strulovich
15 min readadvanced
--
View Original

Overview

The article discusses Meta's transition from Java to Kotlin for Android development, highlighting the advantages of Kotlin such as nullability handling, functional programming support, and reduced code length. It also addresses the challenges faced during the migration process, including build times and interoperability issues.

What You'll Learn

1

How to effectively migrate an Android codebase from Java to Kotlin

2

Why Kotlin's nullability features improve code safety and reduce runtime errors

3

When to use Kotlin's functional programming features to enhance code readability

Prerequisites & Requirements

  • Familiarity with Java and Android development concepts
  • Experience with IntelliJ or Android Studio(optional)

Key Questions Answered

What are the main advantages of using Kotlin over Java for Android development?
Kotlin offers several advantages over Java, including built-in nullability handling that reduces null pointer exceptions, support for functional programming with inline functions and lambdas, and a more concise syntax that leads to shorter and clearer code. These features contribute to improved developer productivity and code maintainability.
How did Meta approach the migration from Java to Kotlin?
Meta approached the migration by deciding to convert almost all their in-house code to Kotlin after addressing initial blockers. They utilized the J2K converter for Java to Kotlin conversion, supplemented by automated tools to handle common issues and streamline the process, ultimately achieving over 10 million lines of Kotlin code in their Android apps.
What challenges did Meta face during the Kotlin migration?
Meta faced several challenges during the Kotlin migration, including longer build times compared to Java, the need to manage a mixed codebase of Java and Kotlin, and interoperability issues that could lead to runtime errors. They also had to address gaps in tooling and adapt their internal libraries to support Kotlin.
How does Kotlin's performance compare to Java after migration?
Kotlin matches Java's performance as it compiles to the same JVM bytecode. Meta conducted A/B tests comparing Java and Kotlin implementations and found no significant performance regressions, confirming that Kotlin's features, such as lambdas and nullability, do not negatively impact execution speed.

Key Statistics & Figures

Lines of Kotlin code in Meta's Android codebase
over 10 million
This figure represents the scale of the migration effort and the extent to which Kotlin has been integrated into their applications.
Average reduction in lines of code
11 percent
This statistic reflects the expected outcome of migrating to Kotlin, where boilerplate code is significantly reduced, leading to cleaner and more efficient code.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Programming Language
Kotlin
Used for Android development to replace Java in Meta's codebase.
Programming Language
Java
Previously used for Android development before the migration to Kotlin.
Tool
J2k
Java to Kotlin converter used to assist in the migration process.
Tool
Redex
Used to optimize bytecode during the migration.
Tool
Ksp
New annotation processing tool being adopted to improve build speeds.

Key Actionable Insights

1
Adopt Kotlin's nullability features to enhance code safety and reduce runtime errors.
By leveraging Kotlin's built-in null safety, developers can catch potential null pointer exceptions at compile time, leading to more robust applications and a smoother user experience.
2
Utilize functional programming features in Kotlin to simplify complex code.
Kotlin's support for inline functions and lambda expressions allows developers to write cleaner and more expressive code, which can improve maintainability and readability.
3
Implement automated tools to facilitate the migration process from Java to Kotlin.
Meta's use of J2K and custom scripts helped streamline their migration, reducing manual effort and minimizing errors, which can be a significant advantage for teams undergoing similar transitions.

Common Pitfalls

1
Failing to properly manage interoperability between Java and Kotlin can lead to runtime errors.
When mixing Java and Kotlin code, developers may encounter issues with platform types that can introduce null pointer exceptions. It's crucial to understand how Kotlin handles nullability and to ensure that Java code adheres to similar safety practices.
2
Underestimating the impact of longer build times with Kotlin.
Kotlin's build times are generally longer than Java's due to its complexity. Teams should prepare for this by optimizing their build processes and considering tools like KSP to mitigate delays.

Related Concepts

Kotlin Migration Strategies
Java Vs Kotlin Performance
Functional Programming In Kotlin