MENLO PARK, CA—When Facebook's mobile app began misbehaving on an older version of Android in late 2012, Facebook engineers had to dive deep into Android's code to figure out what was causing the mishap. In a whiteboard session today at Facebook headquarters, mobile engineering director Mike Shaver described how Facebook identified a problem in Android itself and created a workaround for its own app so users wouldn't have to suffer.

At the beginning of the session, Shaver explained that he was particularly fond of the Android platform because it is both opportunistic and open. "The whole point of it is that there's no central power that drives it," Shaver said. "That's one of the things that makes Android so exciting to develop for." Facebook continues to see Android as a viable platform not only because of the amount of users Android has around the world, but because it allows Facebook to develop mobile applications that interact directly with the operating system. This isn't always easy, however.

In an official blog post, Facebook notes that there are many challenges to overcome when developing for Android, especially on older versions of the platform. But the company isn't referring to fragmentation issues. Android’s runtime engine, the Dalvik Virtual Machine, kept breaking the Facebook application during the app’s revamp this past December. “In order for the program to do anything, it has to work with Dalvik,” explained Shaver at the closed session. To ensure that an application runs smoothly, it needs to be able to use a large portion of small “methods”—elements defining behavior within the application. But when emulated in older iterations of Android like Gingerbread, Facebook's app would crash.

Gingerbread was released and written “long before the arc of Android’s success,” as Shaver put it. So it had some limitations baked into Dalvik and couldn't handle some of the capabilities and features that were essential to the Facebook application. Facebook’s official blog post refers to a specific bug the company had filed that detailed failed app installations. But rather than nix the whole Gingerbread user base altogether, Shaver said the team set out to “look under the hood.”

Here's how Facebook explains it in-depth:

During standard installation, a program called "dexopt" runs to prepare your app for the specific phone it's being installed on. Dexopt uses a fixed-size buffer (called the "LinearAlloc" buffer) to store information about all of the methods in your app. Recent versions of Android use an 8 or 16 MB buffer, but Froyo and Gingerbread (versions 2.2 and 2.3) only have 5 MB. Because older versions of Android have a relatively small buffer, our large number of methods was exceeding the buffer size and causing dexopt to crash. After a bit of panic, we realized that we could work around this problem by breaking our app into multiple dex files, using the technique described here, which focuses on using secondary dex files for extension modules, not core parts of the app. However, there was no way we could break our app up this way—too many of our classes are accessed directly by the Android framework. Instead, we needed to inject our secondary dex files directly into the system class loader. This isn't normally possible, but we examined the Android source code and used Java reflection to directly modify some of its internal structures. We were certainly glad and grateful that Android is open source—otherwise, this change wouldn't have been possible.

Android’s open nature allowed the team to delve deep into Dalvik and figure out where it had set up a buffer. Capped at 5MB, Facebook developers moved a pointer around the numbers to increase that threshold to 8MB. After sending the patch over to Google for their opinion, the new version of Facebook's app was approved in a few days and it became usable again for Gingerbread users. The problem did not affect newer versions of Android.

None of this would have been possible if it weren't for Facebook pushing its developers to have “mobile empathy”—the company’s plan to get its employees to put mobile first. At the beginning of 2012, Facebook employed only a small group of developers to work on its core applications, which at the time were really just wrappers of the site’s mobile Web experience. “It wasn't good enough,” stressed Shaver. Facebook then started an intensive training program in July 2012 in an effort to bring its developers up to speed on coding natively on both Android and iOS. The five-day intensive session features eight hours per day of training, taught by Big Nerd Ranch, and those who can last the week can start writing code the following Monday. As an aside, any Facebook employee can take the course. So far, 450 people from various backgrounds have completed the training.

Listing image by Kevin Krejci