Sencha Touch applications developed for Android devices should be tested in the Android simulator as part of your development process. This blog post discusses how to install and use the Android simulator, for Sencha Touch applications developed with either the command line tools or Sencha Architect
The number of Android smartphones being sold and used has recently surpassed iOS smartphones. (This Time magazine article from April 2013 breaks down the numbers).
Many Sencha Touch developers are targeting the Android platform. This blog post will show you how to set up an Android development environment from start to finish. The screenshots and directory names are Mac-centric. The process for Linux is almost identical, and the process for Windows is very similar.
1. Install Android SDK Tools
Install the Android SDK Tools: download the proper file for your operating system.
On the download page, click the dropdown for “Download for Other Platforms” and download the “SDK Tools Only” zip file for your operating system. (For Windows, download the zip file, not the installer.exe).
What you’re downloading is not the SDK itself, but a zip file that contains the installer for the rest of the SDK. We will use the SDK tools to download the actual emulator in the next section.
Unzip the Android SDK Tools to the “bin” directory in your home directory, which you may have to create.
For Mac OS, save the file to /Users/username/bin (create the “bin” directory if it doesn’t exist). The full path to your Android SDK directory on Mac is /Users/username/bin/android-sdk-macosx/.
For Linux, save the file to /home/username/bin (create the “bin” directory if it doesn’t exist). The full path to your Android SDK directory on Linux is /home/username/bin/android-sdk-linux/.
If you’re using Windows 7, Windows puts your personal files in the directory C:\Users\username. If you’ve already installed Sencha Cmd (see below), the C:\Users\username\bin directory has already been created for you, otherwise you can create it. Unzip the Android SDK under the bin directory. For Windows, the full path to the Android SDK is C:\Users\username\bin\android-sdk-windows.
2. Install the Android emulator
Using the Android SDK Manager (that you just downloaded), install the Intel-based Android emulator.
In the Android SDK directory, under the “tools” subdirectory, you’ll find an executable file called “android”. Double-click on it to launch the Android SDK Manager.
Uncheck the box next to “Android 4.2.2 (API 17)”. This will uncheck all the boxes in that section. The only box you need to check is “Intel x86 Atom System Image”. You can leave the checkboxes next to “Platform tools” and “Build-tools” checked if you want.
Since we are using Sencha Touch to develop our Android apps, we don’t need to install any of the other products.
Here are the final steps to finish the install:
- Click “Install 4 packages…” to download and install your Android packages.
- Click on the package headers, and click “Accept License”.
- The progress bar at the bottom will show you the progress of the install. Wait until all your packages have been installed.
3. Configure and start up the Android emulator
To test our emulator download, we’ll configure the emulator for its first run, then start it up to see if it’s working properly.
After all your packages have downloaded in the previous step, select the “Tools → Manage AVDs…” menu item in the Android SDK tools.
A dialog will appear showing you all the virtual Android machines that you’ve defined. Since this is the first time that you’ve run this command, there are no machines shown. Click the “New…” button to define a new machine.
Fill in the name, type of device, and the API level (17 in our case). Also, make sure the “Use Host GPU” checkbox is checked. Click OK to accept these settings, and click OK to accept the summary screen.
You’ve configured your first Android emulator, and you should see a green check in front of your virtual Android device. On the right side of the dialog, click “Start…” to start up the emulator. On the following screen, click “Launch”.
If you’ve installed everything correctly, the Android emulator will start up. Depending on the speed of your machine, the startup process may take several minutes. When the emulator starts up, you should see the screen below.
Click on the circle to see the applications that are already installed in this emulator. If you’re able to see this screen, then you have installed and configured your Android emulator correctly.
Additional steps for Linux 64-bit
The Android emulator file is a 32-bit binary. To run 32-bit binaries on your 64-bit Linux system, you will need to install 32-bit libraries for your version of Linux. On many Linux systems, this is not done by default.
If you get an error message similar to “emulator: file not found” even though you can see the emulator file right there in your “tools” directory, then you have to install 32-bit loaders for your 64-bit Linux system.
Ubuntu/Debian: type “sudo apt-get install ia32-libs”
Red Hat/Fedora: read the docs
Other flavors of Linux: Google “<Linux flavor> 32 bit loader for 64 bit system” or something similar
4. Generate an Android certificate
Create a certificate to use when downloading programs to the Android emulator or testing on the physical Android device.
To generate a certificate usable with your Android emulator, you will be using the “keytool” program that comes with Java, so you must have Java SE 1.6 or higher installed on your machine. This is already installed if you’re using a Mac, and is most likely already installed on your Windows machine.
From the Terminal or a command prompt, type “keytool”. If the command is not found, you may need to go to java.com, and download Java SE 1.6 or higher.
For simplicity, we’ll create the keystore in the Android SDK directory, and we’ll set the keystore, alias, and password all to “android1”. (For a real certificate you would not set such obvious values for the keystore and password, but we’re trying to keep things simple for our demo.)
Here’s what the dialog looks like:
$ pwd /User/username/bin/android-sdk-macosx $ keytool -genkey -v -keystore android1.keystore -alias android1 -keyalg RSA -keysize 2048 -validity 10000 Enter keystore password: (I entered "android1") What is your first and last name? [Unknown]: Patrick Chu What is the name of your organizational unit? [Unknown]: Training What is the name of your organization? [Unknown]: Sencha What is the name of your City or Locality? [Unknown]: Redwood City What is the name of your State or Province? [Unknown]: California What is the two-letter country code for this unit? [Unknown]: US Is CN=Patrick Chu, OU=Training, O=Sencha, L=Redwood City, ST=California, C=US correct? [no]: yes Generating 2,048 bit RSA key pair and self-signed certificate (SHA1withRSA) with a validity of 10,000 days for: CN=Patrick Chu, OU=Training, O=Sencha, L=Redwood City, ST=California, C=US Enter key password for <android1> (RETURN if same as keystore password): [Storing android1.keystore] $
At this point, you have a file called “android.keystore” that contains your certificate. You will need this file to package a Sencha Touch app into a “native” Android application.
5. Edit packager.json
Edit “packager.json” to reflect details about the Android emulator environment and the certificate file that you generated above.
In this step, we’re going to generate a demo Sencha Touch app, so we have something to load into the Android simulator. To do this, I’m going to make some assumptions about your Sencha Touch environment:
- The root of your localhost web server is a folder called “htdocs”. This is the default if you installed Apache using XAMPP.
- Your Sencha Touch files are in the directory “htdocs/touch2”
- You’ve already installed Sencha Cmd. To test, type “sencha” from the Terminal or command prompt. If you see a Sencha Cmd help screen, then Sencha Cmd is installed. If the command is not found, install Sencha Cmd
Change directory to “htdocs/touch2”, and enter the following command:
- sencha generate app –name AndroidDemo –path ../demo
- For Windows, the directory is “..\demo”
It’s OK if the htdocs/demo directory doesn’t exist — Sencha Cmd will create it. When the command completes, you will have a simple but complete Sencha Touch application. If your web server is running, you can see it running at http://localhost/demo.
To tell this application about the details of our Android environment, we have to edit the “htdocs/demo/packager.json” file. Here are the properties that we need to edit, common to all platforms:
- “applicationId”: “com.demo.AndroidDemo”, /* (this must contain at least 2 parts separated by periods) */
- “platform”: “AndroidEmulator”,
- “certificateAlias”: “android1”,
- “certificatePassword”: “android1”,
- “androidAPILevel”: “17”,
Properties specific to Mac OS:
- “certificatePath”: “/User/username/bin/android-sdk-macosx/android1.keystore”,
- “sdkPath”: “/User/username/bin/android-sdk-macosx”,
- Substitute “username” with your username
Properties specific to Linux:
- “certificatePath”: “/home/username/bin/android-sdk-linux/android1.keystore”,
- “sdkPath”: “/home/username/bin/android-sdk-linux”,
- Substitute “username” with your username
Properties specific to Windows:
- “certificatePath”: “C:/User/username/bin/android-sdk-windows/android1.keystore”,
- “sdkPath”: “C:/User/username/bin/android-sdk-windows”,
- Substitute “username” with your username
- Yes, you can and should use FORWARD slashes in your Windows path
Save your edits to the file.
You’re almost done! Now all we have to do is run our program in the Android emulator. Here are the steps:
- Start your Android emulator. It has to be running at the time that Sencha Cmd attempts to send it the application’s files
- Change directory to “htdocs/demo” (the root of your application, where your packager.json file lives)
- Type: sencha app package run packager.json
(Note: your Java executable must be somewhere in your path in order for Sencha Cmd to work)
If you’re lucky (and patient), you should see the sample application start in your Android emulator! Congratulations!
Click on the tabs at the bottom to move between screens in the sample app.
6. (optional) Install Intel’s hardware acceleration drivers for the Android emulator
Install hardware acceleration for the Android emulator to improve performance. This step is HIGHLY recommended.
While running your Sencha Touch program inside your Android emulator, you may notice that it runs a bit “sluggish” (the most polite term I could think of). Since the emulator is implemented completely in software, it’s always been slow.
However, if you’re running on an Intel-based Mac or Windows machine, you can use Intel drivers that provide hardware acceleration for the Android emulator, in the same way that VMWare’s or Hyper-V’s virtual machines are also hardware assisted. Your Intel processor has to support Intel’s Virtualization Technology (VT) in order for the drivers to work. Intel’s name for this package is HAX, short for “Hardware AXceleration”.
You can download the software from this page.
The page also contains links to very detailed installation instructions for all supported platforms. After you install the software, you will notice that the Android emulator runs much faster.
NOTE: You’ll notice that, in the Android Package SDK tool, HAXM is listed at the bottom as a possible install. I’ve had problems installing HAX this way. Instead, go to the link above and install it from that page.
NOTE: If you are running Hyper-V in Windows 7 on your PC, you will have to disable it in order for Intel’s hardware acceleration to work. Hyper-V will lock up the virtualization feature for itself, thus preventing other virtualization programs from running.
7. More resources
Below I&rsquo’ve listed two useful Android development features that I don’t have room to cover in this blog post.
- Chrome Mobile Emulation:
Within Chrome, you can simulate touch events, screen resolutions, device user agents, device orientation, and geolocation, all from within Chrome. This allows you to perform the bulk of your development and debugging in the host’s browser, which is much faster, and then run the application in the simulator as a once-a-day test and sanity check.
- Remote Debugging on Android:
Use Chrome on your host to debug web applications in your Android browser.
For Further Information
Do you have a great idea for a Sencha Touch app, but you’re not sure what the best way is to develop it? Sencha Training provides live, comprehensive classes covering both Sencha Touch and Ext JS at locations all over the world, year round. Sencha Certified instructors teach you the shortcuts and best practices to getting your Sencha Touch applications running as quickly as possible, and cover both iOS and Android deployment. For more details, click here.
If you have any questions or feedback, please add a comment.