Appcelerator Blog

The Leading Resource for All Things Mobile

Configuring Appcelerator Titanium to Use Intel x86 Images

4 Flares 4 Flares ×

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.

IMPORTANT Assumptions:

We assume here that:

  1. 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.)
  2. You have Appcelerator Titanium Studio installed. If not, go to http://www.appcelerator.com and get it — it’s free, and it rocks.
  3. 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.

STEP 1

Find your Android SDK location. If you already know where your Android SDK is installed, you can skip this step.

  1. Open Titanium Studio
  2. Click in the menu at the top: Titanium Studio / Preferences
  3. In the Preferences screen:
  4. Click Titanium Studio / Titanium
  5. Look under Android / Android SDK Home
  6. 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

STEP 2

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

STEP 3

Install the Intel Hardware Accelerated Execution Manager for your platform

Launch the Android SDK Manager from the terminal window by typing:

android

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.

STEP 4

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:

extra_intel_haxm-macosx_r02.zip

then download and install:

release_1.0.4-hotfix

STEP 5

Create a new Android Run Configuration for your Titanium project.

  1. Go into Titanium Studio.
  2. In the Project Explorer window, right-click on your project’s root entry (on the project’s name):
  3. Select Run As / Run Configurations
  4. Click “Titanium Android Emulator” in the tree
  5. Click the “Launch New Configuration” button (at the top left of the tree.)
  6. Under Android API, select an [x86] platform. For our example, we choose: Android 4.0.3 [x86]
  7. For this example, select Screen: HVGA
  8. Under Project, click Browse and select your project.
  9. Name your new configuration at the top. For our example, we name it: MyProject Android 4.0.3 [x86]
  10. Click Apply
  11. Click Run
  12. 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.)
  13. 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
  14. Close the emulator.

STEP 6

Configure your new Android Emulator instance.

Launch the Android Virtual Device Manager. Back in the terminal window, type:

android avd
  1. Click on your emulator instance from Step 5, then click Edit.
  2. Here, we’re going to configure this emulator to look and work like a real Android phone.
  3. Select the “Resolution” radio button and enter a resolution of 320 x 528 (allowing 40 pixels at the bottom for the hardware buttons.)
  4. Under “Hardware” at the bottom:
  5. Click New.., Select GPU emulation, Click OK
  6. Click New.., Select Hardware Back/Home keys, Click OK
  7. Click New.., Select Ideal size of data partition, Click OK
  8. Click New.., Select Keyboard support, Click OK
  9. Click New.., Select SD Card support, Click OK
  10. Now change the following values:
  11. SD Card Support: yes
  12. Hardware Back/Home keys: no (this puts the back/home keys at the bottom of the screen)
  13. Keyboard Support: yes (this allows you to type from your computer keyboard when in the emulator)
  14. Device ram size: 512 (this should be the default, but you can adjust it here)
  15. GPU emulation: no (you can change this to “yes” and see if it works for you later)
  16. Click “Edit AVD”
  17. Close the Android Virtual Device Manager.

STEP 7

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/< >.avd/config.ini

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.

STEP 8

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.)

  1. Click New…
  2. Under AVD name, enter something like “Google_API_15_Template”
  3. 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.
  4. Under Device, choose any device (we’ll use an HVGA device here…)
  5. Click OK, then OK
  6. Select your new device “Google_API_15_Template”, then click Start, then click Launch
  7. Wait for the new emulator instance to completely boot.
  8. 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
    
  9. Exit the emulator. Exit the Android Virtual Device Manager.
  10. Now, we copy the Google APIs to the new emulator instance (from Step 5)
  11. In the terminal window, type:
    $ emulator -partition-size 512 @< > &
    For our example, it would be:
    $ emulator -partition-size 512 @titanium_16_HVGA_x86 &
    
    (Note: the above command will fail if your ANDROID_SDK_ROOT variable is not set correctly from Step 2.)
  12. 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
    
  13. Don’t close the emulator just yet — we’ll use it again in Step 9.

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/< >.avd/

For our example, it would be:

$ cp system.img ~/.android/avd/titanium_16_HVGA_x86.avd/

Now close the emulator – you’re done!

4 Flares Twitter 0 Facebook 0 Google+ 4 LinkedIn 0 Email -- 4 Flares ×

16 Comments

  1. Yay! I spent all yesterday trying to get the Android emulator to run faster and scouring stackoverflow for what you’ve just written.

    Really appreciate your work. Am keen to get that emulator running faster by following your instructions.

    Thanks heaps,
    Anthony

  2. Alberto M.

    when executing the command:
    $ adb shell /data/mkfs.yaffs2.x86 /system /data/system.img

    I get the following error:
    /data/mkfs.yaffs2.x86: 1: Syntax error: “(” unexpected

    Am I doing anything wrong?

  3. Alberto M.

    never mind, I didn’t set the emulator as x86.

  4. Mark Ruys

    In step 2, you forget an echo:
    echo ‘export ANDROID_SDK_ROOT=”/path/to/android/sdk”‘ >> ~/.bash_profile
    Because of the quotes, you need not to escape spaces by slashes anymore.

    To make copy & paste easier, I would source .bash_profile:
    . ~/.bash_profile
    and then:
    sudo ln -s $ANDROID_SDK_ROOT/tools/android /usr/bin/android
    sudo ln -s $ANDROID_SDK_ROOT/tools/emulator /usr/bin/emulator
    sudo ln -s $ANDROID_SDK_ROOT/platform-tools/adb /usr/bin/adb
    etc. In my case $ANDROID_SDK_ROOT/tools and $ANDROID_SDK_ROOT/platform-tools/ were already in my $PATH, so no need to create the three symlinks.

    In step 4, you can omit the download and install of extra_intel_haxm-macosx_r02.zip as the hotfix is a complete package superseding r02 plain.

    One other comment fastdev deployment makes my app crash in the emulator. I think fastdev is only stable for older Android SDK’s (e.g. API 8). After I disabled fastdev, I can run apps in the emulator API 17. I don’t think this has anything to do with Intel versus ARM.

    Blogs like this are really useful. But also makes you wonder why developing for Android is sooo much harder then for iOS in Titanium!

  5. tariq

    Windows!!!!!

  6. Jongeun Lee

    If you get an error with emulator-x86, please make symbol link of emulator-x86 :

    $ sudo ln -s /path/to/android/sdk/tools/emulator-x86 /usr/bin/emulator-x86

  7. Josh

    I dont suppose there is an AVD with all of the API keys we could download na use instead, is there?

  8. Conny

    It seems like you can in fact edit the disk.dataPartition.size from within AVD – the trick is you have to enter it like “512MB” in order for it to validate.

    Makes me wonder – does adding “512m” manually to ini file actually work, or will does it get (silently) discarded?

    Thanks a lot for the writeup! The speed difference is just enormous~! (Gfx acceleration and Fastdev seems to be working fine too, using SDK 3.0.0.GA and just (re-)launching from within TiStudio)

  9. fahad

    what about Windows platform?

  10. There are a lot of manual steps in there. Any chance of providing an automated packaged solution ?

  11. Shawn

    Super useful! I got it working and it’s much faster.

    Here are some questions I have:

    I’m running android 4.2.2. I found that every time I went to Settings->Storage, I got error “Settings has stopped.” Does this have something to do with this emulator?

    I wanted to erase the emulator SD card, but now I can’t do it through Settings->Storage. What’s the proper way of doing so?

    And can I do a factory reset on this emulator? When I tried, it rebooted and stuck on the Android launch screen.

  12. Mark Burggraf

    @fahad: This tutorial was written specifically for OSX, but the concepts should be the same for Windows.

  13. Mark Burggraf

    @Shawn: Sorry, I’ve never seen the “Settings has stopped” error, so I don’t think it’s related to this. You can reprovision the SD card by creating a new file in the “android avd” interface. You can also make a copy of the empty sdcard image file and just copy it over the current version when you want a “blank” sdcard.

  14. Lalo landa

    after pushing the files in the emulator, it stops working. When I try to open it shows me a black screen and can not do anything. Any ideas?

  15. Alberto

    All methods I have seen, like this one, rely on creating this system image from within the guest Android system, which is very slow, and hard to automate. My method tricks QEMU to persist the changes to /system into the disk image, instead of throwing them away.

    You can see the full method here: http://confcompass-tech.blogspot.nl/2013/07/google-apis-in-x86-android-emulator.html

Comments are closed.

Sign up for updates!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
4 Flares Twitter 0 Facebook 0 Google+ 4 LinkedIn 0 Email -- 4 Flares ×