Engineering Stability in Migrations: Moving to Immutable Collections in Uber’s Android Apps

Warren Smith, Molly Vorwerck
6 min readadvanced
--
View Original

Overview

The article discusses Uber's transition from mutable to immutable collections in their Android apps, highlighting the challenges faced and the solutions implemented to enhance stability and reduce bugs. It details the use of AutoValue for model generation and the introduction of tracked mutable collections to ensure a smooth migration process.

What You'll Learn

1

How to generate models using AutoValue in Android applications

2

Why using immutable collections can prevent bugs in Android apps

3

How to implement tracked mutable collections for debugging

Prerequisites & Requirements

  • Understanding of Android app architecture and model generation
  • Familiarity with AutoValue and Gson libraries(optional)

Key Questions Answered

What challenges did Uber face when using mutable collections in AutoValue models?
Uber encountered bugs due to inadvertent mutability when using collection classes in AutoValue models. These mutable collections led to hard-to-spot bugs, particularly when collections were modified during network operations, resulting in inconsistent data being displayed to users.
How did Uber migrate to immutable collections without crashing production users?
Uber implemented an API-invisible change at the network serialization layer using Gson's TypeAdapters to back model APIs with tracked mutable collections. This allowed them to detect mutations during development while maintaining existing interfaces, facilitating a smooth transition to immutable collections.
What are tracked mutable collections and how are they used?
Tracked mutable collections (TMCs) are special implementations that log mutations during runtime. They allow developers to catch errors in debug builds and log them in release builds, helping to identify and fix issues related to mutable collections in production.

Technologies & Tools

Backend
Autovalue
Used for generating immutable model classes in Android applications.
Backend
Gson
Utilized for JSON serialization and deserialization in network communication.
Backend
Guava
Provides immutable collection classes for use in Android models.

Key Actionable Insights

1
Implement tracked mutable collections in your Android applications to catch mutations during development.
This approach allows developers to identify and log where collections are being modified, reducing the likelihood of bugs related to mutability in production.
2
Transition to immutable collections to enhance the stability of your models.
Immutable collections prevent unintended modifications, ensuring that shared data remains consistent across different parts of the application.
3
Utilize AutoValue for model generation to reduce boilerplate code in your Android apps.
By automating model creation, you can minimize errors and improve maintainability, especially when working with complex data structures.

Common Pitfalls

1
Using mutable collections in models can lead to hard-to-track bugs due to unintended modifications.
This often happens when collections are shared across different components, leading to inconsistent data states. To avoid this, always prefer immutable collections or implement tracking mechanisms.

Related Concepts

Immutable Collections
Model Generation In Android
Network Serialization Techniques