Today’s guest post and cool Android emulator optimization tip comes from Titanium developer Mark Burggraf. Thanks for handy tips, Mark!
If you have used Titanium to develop for Android, you know that the Android Emulator can be slow. The reason for this is that the Android Emulator is actually launching a virutal machine for the ARM architecture to run your app, and this is running on top of your computer which uses an x86 architecture. This post guides you through the process of configuring your Android Emulator to run as x86, resulting in a dramatic increase in performance and a much smoother development cycle.
Configuring Appcelerator Titanium to Use Intel x86 Images
(A.K.A. “How to make working with the Android Emulator bearable.”)
If you follow these instructions, you’ll end up with an Android Emulator optimized for the x86 platform that runs as fast as your iOS Simulator (once it finally boots up.) This x86 version of the emulator boots faster, runs faster, and restores your sanity when working with Android. NOTE: many (maybe most?) Titanium custom modules will not work with the x86 emulator. If you rely on a lot of third-party custom modules, you may be out of luck. Contact the module developers and ask them if they’ll support the platform with x86 libraries.
We assume here that:
- You’ve got a true Apple Mac running OSX 10.8.3. (Other versions of OSX may work just fine, but we’ve only tested 10.8.3.)
- You have Appcelerator Titanium Studio installed. If not, go to http://www.appcelerator.com and get it — it’s free, and it rocks.
- You have the Android SDK installed and working with Titanium Studio. If not, follow the guides on the Appcelerator site to get it working.
This tutorial isn’t about getting Titanium working with the Android emulator. We’ll assume you already have that working. This tutorial is about getting Titanium to work BETTER AND FASTER with the Android emulator. Or, rather, just getting the Android emulator to work BETTER AND FASTER.
Find your Android SDK location. If you already know where your Android SDK is installed, you can skip this step.
- Open Titanium Studio
- Click in the menu at the top: Titanium Studio / Preferences
- In the Preferences screen:
- Click Titanium Studio / Titanium
- Look under Android / Android SDK Home
- Make a note of this path — this is your Android SDK Root Path
In Step 2, wherever you see /path/to/android/sdk you’ll want to replace it with your own Android SDK path
Set your ANDROID_SDK_ROOT variable and create symbolic links.
Open a terminal window. Type the following (using the path from Step 1):
export ANDROID_SDK_ROOT=/path/to/android/sdk >> ~/.bash_profile
IMPORTANT NOTICE: If you have spaces in your path, you’ll need to escape each space with a backslash. Let’s say you put your Android SDK in the path “/Applications/Titanium Studio/Android SDK”. You’ll need to do this:
export ANDROID_SDK_ROOT=/Applications/Titanium Studio/Android SDK >> ~/.bash_profile
Close the terminal window and open a new terminal window. Your ANDROID_SDK_ROOT variable should now be set!
To make your life easier, we’ll now set up some symbolic links to the import Android command-line utilities. Once again, if you have spaces in your Android SDK path, you need to put a backslash before each space:
$ sudo ln -s /path/to/android/sdk/tools/android /usr/bin/android $ sudo ln -s /path/to/android/sdk/tools/emulator /usr/bin/emulator $ sudo ln -s /path/to/android/sdk/platform-tools/adb /usr/bin/adb
Install the Intel Hardware Accelerated Execution Manager for your platform
Launch the Android SDK Manager from the terminal window by typing:
Under “Extras”, check the box for “Intel x86 Emulator Accelerator (HAXM)”. Click the “Install package(s)” button and complete the installation. While you’re in the SDK Manager, make sure you have one or more x86 images installed under the Android Platform(s). For example, if you’re using Android 4.0.3 (API 15), look under that folder to make sure “Intel x86 Atom System Image” is installed. You’ll want the x86 image installed for each platform that you want to test.
Install the HAXM hot fix from Intel.
Go to the page: Intel® Hardware Accelerated Execution Manager 1.0.1 (R2)
In the “Mac OS X” section, download and install:
then download and install:
Create a new Android Run Configuration for your Titanium project.
- Go into Titanium Studio.
- In the Project Explorer window, right-click on your project’s root entry (on the project’s name):
- Select Run As / Run Configurations
- Click “Titanium Android Emulator” in the tree
- Click the “Launch New Configuration” button (at the top left of the tree.)
- Under Android API, select an [x86] platform. For our example, we choose: Android 4.0.3 [x86]
- For this example, select Screen: HVGA
- Under Project, click Browse and select your project.
- Name your new configuration at the top. For our example, we name it: MyProject Android 4.0.3 [x86]
- Click Apply
- Click Run
- Wait for the emulator to start up completely and run your project. (Your project may not run if it requires Google API libraries — this is ok — we’ll add them back in later.)
- Make a note of the emulator instance name (shown at the top of the emulator window). It will look like this: titanium_16_HVGA_x86
- Close the emulator.
Configure your new Android Emulator instance.
Launch the Android Virtual Device Manager. Back in the terminal window, type:
- Click on your emulator instance from Step 5, then click Edit.
- Here, we’re going to configure this emulator to look and work like a real Android phone.
- Select the “Resolution” radio button and enter a resolution of 320 x 528 (allowing 40 pixels at the bottom for the hardware buttons.)
- Under “Hardware” at the bottom:
- Click New.., Select GPU emulation, Click OK
- Click New.., Select Hardware Back/Home keys, Click OK
- Click New.., Select Ideal size of data partition, Click OK
- Click New.., Select Keyboard support, Click OK
- Click New.., Select SD Card support, Click OK
- Now change the following values:
- SD Card Support: yes
- Hardware Back/Home keys: no (this puts the back/home keys at the bottom of the screen)
- Keyboard Support: yes (this allows you to type from your computer keyboard when in the emulator)
- Device ram size: 512 (this should be the default, but you can adjust it here)
- GPU emulation: no (you can change this to “yes” and see if it works for you later)
- Click “Edit AVD”
- Close the Android Virtual Device Manager.
Manually edit the emulator configuration file.
This step is necessary due to a bug in the Android Virtual Device Manager app. We want to adjust the “Ideal size of data partition” but we can’t do that in the Android Virtual Device Manager for some reason, so it must be done manually. Why adjust the size of the data partition? Because, in the development cycle, every time you run a new version of your application, the data partition begins to fill up. After a few runs of your app (sometimes after just 2 runs) you get an “out of space” error when you try to run the app. It’s easy to fix — you just go into the emulator, go into settings, go to Apps, find your app, then delete your app, then try running your app again from Titanium Studio. This is time consuming, though, and you lose any data you’ve entered in your app. Increasing the size of the data partition allows you to run your app many, many more times before you have to delete it and start again.
Make a note of your emulator instance name from Step 5, such as “titanium_16_HVGA_x86″. We’re going to edit a text file now — if you have an editor configured, or you prefer nano or vi or something else, go ahead and use that. We’ll assume here you’re clueless, but that you know how to use Textedit.
In the terminal window, type:
$ open -a TextEdit ~/.android/avd/<
For our example, it would be:
$ open -a TextEdit ~/.android/avd/titanium_16_HVGA_x86.avd/config.ini
Change the line:
disk.dataPartition.size=0 to disk.dataPartition.size=512m
This gives a 512mb data partition. Save the file and exit TextEdit.
Copy the Google APIs to your emulator instance.
In the terminal window, start the Android Virtual Device Manager:
android avd &
(Why is there an & at the end of the line? So that this command runs in the background and we get a terminal prompt back.)
- Click New…
- Under AVD name, enter something like “Google_API_15_Template”
- Under Target, select Google API’s (Google Inc.) — API Level 15 (the API level must match the API level you selected in Step 5. For Android 4.0.3, the API level is 15.
- Under Device, choose any device (we’ll use an HVGA device here…)
- Click OK, then OK
- Select your new device “Google_API_15_Template”, then click Start, then click Launch
- Wait for the new emulator instance to completely boot.
- Once the new emulator instance is running, go back in the terminal window and type:
$ cd ~ $ adb pull /system/etc/permissions/com.google.android.maps.xml $ adb pull /system/framework/com.google.android.maps.jar
- Exit the emulator. Exit the Android Virtual Device Manager.
- Now, we copy the Google APIs to the new emulator instance (from Step 5)
- In the terminal window, type:
$ emulator -partition-size 512 @<
$ emulator -partition-size 512 @titanium_16_HVGA_x86 &
- Once the emulator boots (and it should boot much faster than the Google API Template instance), type this in the terminal window:
$ adb remount $ adb push com.google.android.maps.xml /system/etc/permissions $ adb push com.google.android.maps.jar /system/framework
- Don’t close the emulator just yet — we’ll use it again in Step 9.
Build a system image
Download mkfs.yaffs2.x86 from here: https://code.google.com/p/android-group-korea/downloads/detail?name=mkfs.yaffs2.x86
Copy the mkfs.yaffs2.x86 file to the emulator. In the terminal window, type:
$ adb push [[path]]/mkfs.yaffs2.x86 /data
You probably downloaded mkfs.yaffs2.x86 to your ~/Downloads folder, so it would look like this:
$ adb push ~/Downloads/mkfs.yaffs2.x86 /data
Now create the new system image by typing:
$ adb shell chmod 755 /data/mkfs.yaffs2.x86 $ adb shell /data/mkfs.yaffs2.x86 /system /data/system.img
Pull the newly built image to the current folder (This step takes a VERY LONG TIME):
$ cd ~ $ adb pull /data/system.img
Now copy the new image to your avd folder:
$ cp system.img ~/.android/avd/<
For our example, it would be:
$ cp system.img ~/.android/avd/titanium_16_HVGA_x86.avd/
Now close the emulator – you’re done!