//
you're reading...

Programming

Faster Android Development on the Mac

One of my big gripes in developing for Android is the emulator’s long startup time. Coming from an iOS background it’s a huge setback since booting up the iOS simulator is just as fast as starting almost any other application – things work within three seconds on my two year old MacBook Air. Whereas it typically takes a few minutes for the Android emulator to do its thing and finally make my app available for debugging. Yes that being an emulator, it’s essentially a full-blown operating system on top of a hardware emulator. In contrast the iOS simulator is more like a special app launcher and runs simulated iOS apps as if they were native Mac apps.

Being a full-fledged virtualized hardware/software stack may be the primary reason for the Android emulator’s slowness. First it needs to virtualize an ARM processor and many related peripherals that a smartphone / PDA needs. On top of that it runs the Linux kernel. Then after that comes the Dalvik VM which is yet another hardware virtualizer (albeit there is no corresponding real-world microprocessor that executes the Dalvik instruction set). Thus there are two layers of chip-language translation involved: from x86 to ARM then to Dalvik.

If the above paragraph makes your head spin, consider this analogy. Suppose you only speak Mandarin and need to converse with someone who only speaks Spanish. You couldn’t find a Mandarin-to-Spanish translator and neither could the other person. Therefore you hired a Mandarin-to-English translator and similarly the other guy hired an English-to-Spanish translator. You talk to your translator using Mandarin whereas they talk to themselves using English and finally the other guy’s translator conveys the message in Spanish. As you can imagine, communication speeds goes slower than UN conferences. Your laptop’s processor is able to read the x86 instruction set (“Spanish”) whereas your Android app is written by the compiler in the Dalvik instruction set (“Mandarin”) and the two communicates via an intermediate ARM instruction set (“English”).

Coming from an iOS background, this double indirection of instruction sets and the slowness it entails is dismaying. Sure having a slower emulation speed that more matches the speed of the actual device may be useful at times if you need to fine tune your app’s performance. However for day-to-day debugging, faster turnaround times is always welcome & the emulator’s slowness isn’t much help in that respect.

Fortunately Intel have supplied a component that allows you to run Android code on the Mac in near-native speed. It’s a kernel extension that works in conjunction with the Atom/x86 variant of the Android system image & emulator. In essence you configure the Android Emulator to use a version of the Dalvik VM compiled for x86 (along with the rest of the Android OS stack) and then uses the kernel extension to execute many of the Intel-based Android OS instructions directly. The result is a lot faster emulator boot time (under a minute in my machine) and generally faster execution time. Extending the analogy, this is the Mandarin to Spanish translator that you’ve been looking for.

Setting it up

Open the Android SDK Manager and install these:

  • Android SDK version 2.3.3 (API Level 10) or higher.
  • Android Atom/x86 System Image.
  • Intel Hardware Accelerated Execution Manager – this is located near the bottom of the list under the Extras heading.
Android SDK Manager  System Image
Android SDK Manager  Intel HAXM
After all of those are installed, quit the Android SDK Manager (and Eclipse if you’re using it as your Android IDE) and then open Terminal. Go to folder  <sdk>/extras/intel/Hardware_Accelerated_Execution_Manager/ where <sdk> is where you place the Android SDK files.  I use homebrew to install Android SDK and my SDK location is in /usr/local/android-sdk/.  There should be a file named IntelHAXM.dmg there. To start installation simply type
  $ open IntelHAXM.dmg
Then you should see the disk image mounted in Finder. There will be an installer file there, double click it and follow the on-screen instructions. Afterwards open the Android AVD Manager and configure an Android Virtual Device with the following options:
  • ABI: Atom/x86
  • System Image: Android Atom/x86 System Image.
  • Uncheck the “Use GPU Acceleration” checkbox.
Android edit avd
Then start the emulator as per normal. You should see “HAX is running” message in Eclipse’s LogCat view as part of the emulator’s startup.
Android Emulator  Startup

If you get “HAX is not working and emulator runs in emulation mode” message while starting the emulator, chances are the kernel extension is not installed or not running correctly. Intel HAXM requires VT support in your computer’s processors, but most recent Macs capable of running Mountain Lion should already have it. Try looking in the Mac’s System Information and verify that your processor is supported. Otherwise you can review the Android emulator guide for a more complete setup instruction and troubleshooting guide.

That’s all for now. Until next time!

 



Do you enjoy this post? Enter your e-mail address below to receive articles like this one in your mailbox.

Free Updates!

Learn how to grow your indie business while keeping your day job.

Categories

Archives

Keep updated!

Don't miss out on new articles!