Scaling Dagger: DI in a modular world
A few days ago I had the pleasure of giving a talk alongside César Puerta about some DI related stuff we have been working at Twitter in the last couple of years. Managing your DI setup while your codebase scales and the modules keep growing is quite the challenge, and we thought that what we learnt could be of some use to other developing struggling with these problems, as they are useful regardless of your app having 5 gradle modules or 50 or 500.
It took a couple of stressful weeks to put together the content, but in the end I think we ended up with a good deck! I really enjoy using hand drawn diagrams to convey information, plus using animation to better help with the understandability of content transitions. So I doubled down on these!
The gist of the talk was as described by its excerpt.
As your Android app grows in size, using a DI framework like Dagger becomes a necessity. But as your app keeps growing and aging, does Dagger scale well with it? Does it play well with modularized apps? How do you visualize and navigate the graph as complexity grows?
In this session we will describe the mechanisms that we use at Twitter to facilitate dependency injection, review the pain points we encountered with Dagger, and share some practical advice on how to address them. Finally, we’ll offer a sneak peek of the tools that built on Dagger to facilitate DI that we will open source in 2020.
NOTE: There have been some recordings from the organization, I will provide the whole talk when those are ready.
I think I was more mentally prepared for this one and didn't have much stage fright, except for maybe the 20 mins leading to the talk where I was setting the equipment up and some things were not working well. But in the end we got everything sorted out.
I kicked off the introductions, then César delivered the modularity introduction: how the Twitter for Android codebase module structure came to life and how it looks like today.
After that, I proceeded to showcase our Dagger DI setup, what's good about DI -Dagger in particular- and some challenges we face when using Dagger in a modular codebase: the graph architecture we use, location of the graph definitions, how to deal with big component/subcomponent monolithic definitions, how to fix common pain points around graph instantiation and how to structure your codebase so the Dagger wiring scales for a large number of developers. After showing how to use the concept of Subgraphs, how to effectively leverage multibindings to favor more module encapsulation, and other tricks, I passed the torch to César again.
César would introduce what we have been working for almost two years now: a framework that provides ways to achieve total encapsulation of DI code into Gradle modules, plus some other goodies like adoption of OOP principles for graph setups. We have been calling this framework Scythe, and we will be opensourcing this at Twitter in 2020.
In the end, I think we told the story we wanted to tell, and the feedback we got from people that knew their Dagger was very positive. It was quite a challenge to pack everything we needed to include in just 45 minutes, we have so much we want to talk about this theme!
There was an odd incident in the middle of my big chunk of the talk though. Some suited guy from the venue came by and walked up to the stage, apparently to change the batteries of the mic I was using. He was signaling César to get close to him and they started to whisper, all really close to me. I had no idea of what was going on.
To be honest, I thought the guy was going to kick us out, and nothing made sense! I was trying to walk through the audience on probably the most key aspect of the talk, while all of that was happening on the background. Can't wait to get my hands on the recording, because it was quite funny – but actually terrifying when you think about public speaking! Anyway, it was a good exercise on mental fortitude to not be thrown off by all that. I was really in the zone!
Anyway, it's been a while since I gave a public talk. There have been plenty internal talks I've been giving as of late (one about modularity, one about Scythe migrations and one about UI Architecture), which I enjoy, but giving them publicly to the Android community is great! I hope I will have more chances to give this talk again somewhere in the not so far future, I think the content is pretty useful – apart from the world premiere of Scythe!
It was a great experience and I feel I learned some stuff from it! As long as you worked on your content, and you rehearse, everything will be naturally fine. Even if you get some crazy shit going on like the Mic guy incident, it doesn't really matter!
It is always a pleasure to mingle with the Android community, which is mostly comprised of great folks. Got to know some really cool people and talk with them about all sorts of things we were all working on. I will probably make one new year resolution for giving more talks publicly!