Android provides great mobile tooling for Java developers to use along with Eclipse or similar editors to write native applications. We will go over some of the tools and how you may want to use them as an AIR developer. But first, let’s install the SDK.
Installing the Android SDK
Before we begin, make sure you are running in administrator mode so that you have all the needed permissions and access.
The Android SDK requires Java 1.6 or later, which you can obtain at http://www.java .com/en/download or http://www.oracle.com/technetwork/java/javase/downloads/index .html. You can download the SDK for Windows, Mac OS (Intel), or Linux at http://developer.android.com/sdk/index.html. The Android SDK only contains the minimum
tools to start with. Use the SDK Manager to install or update components.
The SDK directory has different names depending on its version and your platform. It may be named something like android-sdk_r08-mac.zip or android-sdk_r08-windows.zip. For the sake of simplicity, rename it to androidSDK. We will use this name to refer to the SDK folder for the rest of the chapter.
Check that you have an Internet connection and an open terminal (or a command prompt on Windows), and enter the following command. If you are not familiar with using the command line, refer to “Using the Command-Line Tool”, which covers this topic:
androidSDK/platform-tools/android update sdk
You need Android SDK Tools, revision 6 or later, which might already be installed by default. Click the Settings button and check the “Force https” box; then select the boxes to download SDK Platform Android 2.2, API 8, revision 2. You will need these files in order to use the emulator.
Unfortunately, installation steps change as the SDK evolves. For instance, the tools directory in revision 6 was renamed platform-tools in revision 8. In addition, version 8 on Windows does not detect the Java Runtime Environment (JRE) and requests that you also install the Java SE JDK (the standard 32-bit version only; the Android SDK does not detect the Java runtime in the 64-bit version). There are other issues with the Windows Android SDK installer on Windows 64-bit machines as well. Please read the documentation to successfully install the SDK for your environment.
On Windows, most Android devices require drivers to communicate with the Android SDK. Instructions for installing these drivers are available on the Android developer website at http://developer.android.com/sdk/win-usb.html. If you have difficulty updating Google’s USB driver package, try to manually edit the android_winusb.inf file in the Android SDK.
Alternatively, with the SDK open, go to Available Packages, select “USB Driver package, revision 3”, and click Install Selected. Note that the location of the packages may change with new upgrades. For instance, in newer versions, the packages can be found under Available Packages→Third party add-ons→Google USB driver package, revision 4.
Note that with AIR 2.6, Adobe made the process much easier by bundling the Android USB drivers for most Android devices on Windows.
We will use what is inside the tools folder.
If you don’t know how to use the command line, read the section “Using the Command-Line Tool”
Installing the Android Debug Bridge
ADB is composed of a client that consists of your application, a daemon running on the device or emulator, and a server which runs in the background on your development machine and manages the communication between the client and the daemon. You can read more about it at http://developer.android.com/guide/developing/tools/adb.html.
To install an Android application using ADB, type the following at the command prompt:
androidSDK/platform-tools/adb install hello.apk
To install ADB in debug mode or for the emulator, provide a destination:
androidSDK/platform-tools/adb -d install hello.apk
androidSDK/platform-tools /adb -e install hello.apk
Let’s look at how to test and debug using ADB.
Detecting Your Device
Connect your device to your computer’s USB port. Turn on USB storage when prompted, or by exposing the option from the top slide-down menu. Set your device for development by turning on USB debugging under Settings→Application→Development.
In the Terminal window on the Mac, or at the command prompt on Windows, type the following to determine whether the SDK sees your device:
androidSDK/platform-tools/adb devices
If the SDK detects your device, it will return the ID for it as follows:
List of devices attached
HT019P805406 device
If it does not detect your device, kill the ADB server and restart it:
androidSDK/platform-tools/adb kill-server
androidSDK/platform-tools/adb start-server
If all else fails, restart your computer. Once your device is detected, you can start using some of the Android tools.
Using the Dalvik Debug Monitor
You can use the Android Dalvik Debug Monitor (DDM) to test the Android application on your desktop. To launch the GUI, navigate to the ddms file under Android→ Tools→ddms (ddms.bat on Windows) and double-click it.
You can take device screenshots from live running applications at your device’s resolution, which is very handy for documentation. On the console, select Device→Screen Capture. Click Refresh every time you want to see an update of your device screen. The images are recorded at your device screen’s resolution.
Selecting Device→File Explorer exposes some of the device’s nonprivate file structure, as well as options to pull, push, or delete files on the device.
DDM handles a range of other tasks as well, such as log dumps, thread views, phone call simulation, and memory and performance profiling. You can read more about it at http://developer.android.com/guide/developing/tools/ddms.html.
Using the logcat Command
The ADB logcat command provides a mechanism for grabbing information from the device and dumping it via USB onto a log screen. Use it to view ActionScript traces and potential errors. More information on this command is available at http://developer.android.com/guide/developing/tools/adb.html.
Reading the logs is a great way to learn about the Android OS, which components are involved, and how various requests are managed.
Type the following at the command line:
androidSDK/platform-tools/adb logcat
If you have the emulator on and the device connected, you can choose the debugging destination:
androidSDK/platform-tools/adb -e logcat
androidSDK/platform-tools/adb -d logcat
You should now see traces along with native messages. click the button to send email, send text, or make a phone call to see the trace messages.
Android provides a message filtering system using priority levels from Verbose to Silent. To print messages for the running AIR application alone at all levels of priority, use the following command (replace applicationID with your application name as defined in the application descriptor):
adb logcat air.applicationID:V *:S
To print messages from the currently running AIR application as well as the Activity Manager, use the following:
adb logcat air.applicationID:V ActivityManager:| *:S
I prefer a simpler approach to only display trace statements and AS error messages. I restrict logcat to only print messages with the string I.air, as in this command:
androidSDK/platform-tools/adb logcat|grep “I.air”
or this command:
androidSDK/platform-tools/adb logcat|grep “hello” application name
During the packaging process, each AIR application has the word air added to its applicationID. Therefore, using the filter I.air guarantees to dump all messages for the currently running AIR application. I stands for the Information priority level. This level does not display native errors but does display AS error messages.
To clear your Terminal window, on the Mac select Terminal→View→Clear Scrollback. On Windows, type C:>cls at the command prompt.
Before starting a new debug process, clear the logcat buffer:
androidSDK/platform-tools/adb logcat -c
Accessing the device’s filesystem
The Android ADB gives access to some of the device’s filesystem. Try this command to display all the applications installed on the device:
androidSDK/platform-tools/adb shell pm list packages
Using the Virtual Device Manager and Emulator
Using the Virtual Device Manager and Emulator
If you do not have an Android device, use the Runtime emulator to simulate one and test your application. Keep in mind that the Runtime emulator is very slow for both AIR and Android applications, so you should not consider its performance to be an accurate benchmark of how applications will perform on actual devices. The Runtime emulator is located at AIRSDK→Runtimes→AIR→Android→Emulator→Runtime.apk.
First, compile your application for the emulator:
androidSDK/bin/adt -package -target apk-debug -storetype pkcs12
-keystore yourCertificate.p12 hello.apk Main-app.xml Main.swf
Install the emulator version of the AIR runtime on the emulator:
androidSDK/platform-tools/adb -e install AIR-sdk-path/RuntimeEmulator.apk
A successful installation displays a message similar to this one:
1713 KB/s (6617672 bytes in 3.771s)
pkg: /data/local/tmp/Runtime_Emulator.apk
Success
Install the Android Package (APK) file using -e, for emulator, as a destination:
androidSDK/platform-tools/adb -e install hello.apk
Launch the AVD Manager application: on the Mac, select Android SDK→Tools→Android; on Windows, select Android SDK→SDK Setup.exe.
Create an Android Virtual Device (AVD), an emulator, using the AVD Manager application. Select the Virtual Devices option and click New. Give the device a name and select the target API: Android 2.2, API level 8. The size of the SD card and Skin is optional. Click Create AVD.
Click Start to launch the emulator. The virtual device has a panel on the left representing the face of your device, a group of buttons for all input on the top right, and a keyboard on the bottom right.
Repackage your application with the emulator as a target. In Flash Professional, select File→AIR Android settings→Deployment→Emulator release. When using Flash Builder, enter the following command:
androidSDK/bin/adt -package -target apk-emulator -storetype pkcs12
-keystore yourCertificate.p12 hello.apk Main-app.xml Main.swf
In Flash Professional, the emulator launches the application automatically. In Flash Builder, install it on the emulator using the Android SDK:
androidSDK/platform-tools/adb install hello.apk
If you have the device attached and the emulator running, use either -d or -e before the install:
androidSDK/platform-tools/adb -e install Main.apk
You can read about the emulator in detail on the Android developer website, at http://developer.android.com/guide/developing/tools/emulator.html or http://developer.android.com/guide/developing/tools/othertools.html#android.