Lottie is a library for Android, iOS, Web, and Windows that parses Adobe After Effects animations exported as JSON with Bodymovin and renders them natively on mobile and on the web. If you haven’t already read Brandon’s post about the Swift rewrite of Lottie on iOS, check it out! We’d like to provide a little more background of why we decided to invest in this rewrite.
Lottie was written over two years ago in Objective-C at a time when Airbnb was not fully migrated to Swift, and most companies had yet to adopt Swift. Fast forward to 2019 — over 50% of all top non-game apps are using Swift. The Airbnb app is more than 90% Swift code, and we haven’t written Objective-C in years. It was time to bring Lottie, one of our most popular open source repos, into the future by rewriting it in Swift.
Why a rewrite?
A lot of developers would take pause if you told them you were planning on rewriting a large open-source library from scratch. Why not just build on top of it? The Objective-C version was working well; why not maintain the status quo? We realized that when considering the differences between Objective-C and Swift, a rewrite was appropriate for a number of reasons:
- Objective-C to Swift interoperability still isn’t great. It works, but that’s about as much as you can say about it. If we only wrote some layers of Lottie in Swift, we’d end up with a hybrid codebase which forces contributors to deal with this challenge for longer and doesn’t move the library forward.
- Swift has many wonderful language improvements such as structs, pattern matching, better immutability, stricter access controls, powerful enum types, functional programming paradigms, and more. None of these features are available in Objective-C which means the Lottie codebase would also miss out on all of these features and any new ones that get introduced in the future.
- Context switching from Objective-C to Swift takes time, and makes working in Lottie and a modern codebase side-by-side more difficult and frustrating.
- More and more engineers are learning Swift first without ever touching Objective-C. As the number of engineers with this background increases, projects written in Objective-C become outdated and inaccessible to these newer iOS engineers.
My Own Swift Story
When I first started at Airbnb, I didn’t really know Swift at all. I was really comfortable and happy writing Objective-C; why change that? In 2017, Swift was going through a lot of changes and the interoperability with Objective-C was a major challenge to deal with. The error messages were often difficult to follow, and the compiler didn’t provide much help. Objective-C was a much more mature language and simply worked better. For these reasons I actively disliked Swift and avoided using it whenever I could.
Oddly enough, one of the main reasons I joined Airbnb was because I knew I needed to learn Swift to advance my career as an iOS engineer. Airbnb was a leader in the transition to Swift having rewritten most of their app into Swift 2 at the end of 2016. When I joined, the app had just been updated to support Swift 3, and I was cautiously optimistic about forcing myself to use the language I avoided for years. Just like with any language (programming or not), you can learn it extremely quickly by fully immersing yourself in it. I was introduced to structs, powerful functional programming paradigms, and writing protocol-oriented code. I came to work excited to learn more about this language, and everyday it got a little easier. Yes, there were snags along the way (the major one being slow compile times), but after a few months I really grew to love the language and all of its intricacies.
It’s been two years since I’ve written Objective-C, and I’m now on the team that owns and maintains the Lottie library. As part of that, I’ve had to look through the Lottie source code at times to address bugs or questions asked by other engineers. Doing so has become a big context switch back into Objective-C that takes me out of my usual flow and is growing increasingly more difficult.
While I was working on implementing a new animated button using a Lottie animation, I ran into a bug where certain layers of the animation weren’t being properly hidden based on the button state. As I dug into the rendering source code to find the culprit, I quickly found myself lost in Objective-C code that was layered like an onion. Lottie has classes for different types of layers, masks, shapes, gradients, strokes, and transforms. It has been contributed to by many other engineers who write code with different styles. There are methods that have been added which all do the same thing in slightly different ways. I grew frustrated trying to decipher what was important and what was just noise that needed to be filtered out.
It became obvious that trying to debug this issue without help was nearly impossible. In order to allow more people to contribute directly to the library to solve these kinds of problems, we realized our best option was to move Lottie into the future and rewrite it in Swift.
During the rewrite I was able to work with Brandon directly on the new Lottie version, and give input into how it could be improved. When we integrated the new version into the Airbnb app, I was able to find new bugs and report them or contribute back and fix them myself. I added support for Objective-C compatibility and fixed some issues with the new AnimatedSwitch class. Being able to work on this library has been empowering, and I finally feel like I can not only debug new issues, but also give back to the community by improving it for everyone.
Airbnb’s commitment to open source
Airbnb loves working with the open source community. We wouldn’t be able to build our apps and services as quickly and easily without a plethora of open source software, and we want to do what we can to give back. This was our main motivation to open source Lottie over two years ago. When we partnered with Brandon Withrow to rewrite Lottie in Swift, we knew that the only option would be to do so in the open and to give access to the community as early as possible. Since updating the open source version of Lottie to version 3.0 we’ve gotten plenty of comments, issues, and pull requests from the community which has greatly improved this new version.
We hope you and your teams are enjoying this update, and that others in the community will continue to partner with us on this new modernized platform in making it better. Thank you so much for all of your support of Lottie and Airbnb open source projects!
If you’re passionate about Swift and iOS development, we encourage you to apply for any of the different roles we have open! We’d love to have you on the team!
Huge thanks to Maja Wichrowska and Stephanie Teng for writing and editing support.