Appendix E. The Android Emulator: Speeding Things Up

Ever felt like you were spending all your time waiting for the emulator? There’s no doubt that using the Android emulator is useful. It allows you to see how your app will run on devices other than the physical ones you have access to. But at times it can feel a little...sluggish. In this appendix, we’ll explain why the emulator can seem slow. Even better, we’ll give you a few tips we’ve learned for speeding it up.

Why the emulator is so slow

When you’re writing Android apps, you’ll spend a lot of time waiting for the Android emulator to start up or deploy your code. Why is that? Why is the Android emulator so sloooooow? If you’ve ever written iPhone code, you know how fast the iPhone simulator is. If it’s possible for the iPhone, then why not for Android?

There’s a clue in the names: the iPhone simulator and the Android emulator.

The iPhone simulator simulates a device running the iOS operating system. All of the code for iOS is compiled to run natively on the Mac and the iPhone simulator runs at Mac-native speed. That means it can simulate an iPhone boot-up in just a few seconds.

The Android emulator works in a completely different way. It uses an open source application called QEMU (or Quick Emulator) to emulate the entire Android hardware device. It runs code that interprets machine code that’s intended to be run by the device’s processor. It has code that emulates the storage system, the screen, and pretty much every other piece of physical equipment on an Android device.

An emulator like QEMU creates a much more realistic representation of a virtual device than something like the iPhone simulator does, but the downside is that it has to do far more work for even simple operations like reading from disk or displaying something on a screen. That’s why the emulator takes so long to boot up a device. It has to pretend to be every little hardware component inside the device, and it has to interpret every single instruction.

How to speed up your Android development

1. Use a real device

The simplest way to speed up your development process is by using a real device. A real device will boot up much faster than an emulated one, and it will probably deploy and run apps a lot more quickly. If you want to develop on a real device, you may want to go into “Developer options” and check the Stay Awake option. This will prevent your device from locking the screen, which is useful if you are repeatedly deploying to it.

2. Use an emulator snapshot

Booting up is one of the slowest things the emulator does. If you save a snapshot of the device while it’s running, the emulator will be able to reset itself to this state without having to go through the boot-up process. To use a snapshot with your device, open the AVD manager from the Android Studio menu by selecting Tools→Android→AVD Manager, edit the AVD by clicking on the Edit symbol, and then check the “Store a snapshot for faster startup” option.

This will save a snapshot of what the memory looks like when the device is running. The emulator will be able to restore the memory in this state without booting the device.

3. Use hardware acceleration

By default, the QEMU emulator will have to interpret each machine code instruction on the virtual device. That means it’s very flexible because it can pretend to be lots of different CPUs, but it’s one of the main reasons why the emulator is slow. Fortunately, there’s a way to get your development machine to run the machine code instructions directly. There are two main types of Android Virtual Device: ARM machines and x86 machines. If you create an x86 Android device and your development machine is using a particular type of Intel x86 CPU, then you can configure your emulator to run the Android machine code instructions directly on your Intel CPU.

You will need to install Intel’s Hardware Accelerated Execution Manager (HAXM). At the time of writing, you can find HAXM here:

HAXM is a hypervisor. That means it can switch your CPU into a special mode to run virtual machine instructions directly. However, HAXM will only run on Intel processors that support Intel Virtualization Technology. But if your development machine is compatible, then HAXM will make your AVD run much faster.

4. Use Instant Run

Since Android Studio 2.3, it’s been possible to redeploy apps much more quickly using the Instant Run utility. This utility allows Android Studio to recompile just the files that have changed, and then patch the application currently running on the device. This means that instead of waiting for a minute or so while the application is recompiled and deployed, you can see your changes running in just a few seconds.

To use Instant Run, click on the Apply Changes option in the Run menu, or click on the lightning bolt icon in the toolbar:

There are a couple of conditions on using Instant Run. First, your app’s target needs to be at least API 15. Second, you need to deploy to a device that runs API 21 or above. So long as you satisfy both these conditions, you’ll find that Instant Run is by far the fastest way of getting your code up and running.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.248.24