In 2012, we made the transition from WebView to native in the Facebook app for Android. In the past year, both the number of engineers at Facebook writing Java code for Android and the number of lines of Java code in our Android repository exploded. We quickly discovered that the standard set of Android build tools was not keeping up with our growth.
In particular, we noticed that building with Ant was slow and using Android library projects to modularize our code was a heavyweight operation. We quickly found ourselves drowning in a sea of duplicate AndroidManifest.xml and build.xml files, coupled with unnecessary res/directories. There was so much boilerplate when introducing a new module that engineers preferred to do the wrong thing, bloating existing modules with new code rather than attempting to navigate the complexity of creating a new module.
This had to stop.
During my first Facebook hackathon in July 2012, I decided to create a new build system for Android that would address our needs. My primary objective was to create a build tool that favored the creation of many small modules rather than a handful of large modules. I hypothesized that once we had clear, fine-grained dependencies, we would be able to dig into the issue of slow build times.
At the end of Camp Hack-a-Thona (the name of our annual three day hackathon in the summer), I had a working prototype for Buck, a new Android build system. By the end of August, I introduced Buck as the build tool for our Android apps, and we deleted all of the build.xml files in our repository a couple of weeks later. It took less time to download Buck’s source code, build it from scratch, and then build the Android app with Buck than it took to build the Android app with Ant. From Day 1, Buck was twice as fast as Ant, cutting Facebook for Android app build times down from 3m40s to 1m30s.
Buck has enabled us to scale our repository, as modules are defined by simple, declarative build rules, so the overhead in creating one is negligible. Today we have the equivalent of over 400 Android library projects in our repository (but only 51 AndroidManifest.xml files and 141 res/ directories), which would be unthinkable using Ant. Because all of our Android apps (Facebook for Android, Messenger, and Pages Manager) are built from one codebase with a single, unified directory of Java source code, code reuse is straightforward. This fine-grained design also makes it easier to create small, sample applications for testing individual features of an app. These sample apps can be built much more quickly than the full-blown Facebook for Android app, which helps our developers iterate faster.
The crowd at Mobile DevCon NYC convinced me that we should not keep this tool to ourselves and that it was time to open-source Buck. Watch the video here.
We are extremely excited to share Buck with the Android development community. You can check out the code from GitHub as well as explore the documentation. We hope that you find Buck as helpful in scaling and speeding up your Android development as we have.