Creating your first AIR application for Android with the Flex SDKTo begin, you must have installed and set up the AIR and Flex SDKs. This tutorial uses the AMXMLC compiler from the Flex SDK and the AIR Debug Launcher (ADL), and the AIR Developer Tool (ADT) from the AIR SDK. See Setting up the Flex SDK. You must also download and install the Android SDK from the Android website, as described in: Android Developers: Installing the SDK. Note: For information on iPhone development, see Creating a Hello World iPhone application
with Flash Professional CS5.
Create the AIR application descriptor fileThis section describes how to create the application descriptor, which is an XML file with the following structure: <application xmlns="...">
<id>...</id>
<versionNumber>...</versionNumber>
<filename>…</filename>
<initialWindow>
<content>…</content>
</initialWindow>
<supportedProfiles>...</supportedProfiles>
</application>
This example only sets a few of the possible application properties. There are other settings that you can use in the application descriptor file. For example, you can add <fullScreen>true</fullScreen> to the initialWindow element to build a full-screen application. To enable remote debugging and access-controlled features on Android, you also will have to add Android permissions to the application descriptor. Permissions are not needed for this simple application, so you do not need to add them now. For more information, see Setting mobile application properties. Write the application codeCreate a file named HelloWorld.as and add the following code using a text editor: package
{
import flash.display.Sprite;
import flash.text.TextField;
public class HelloWorld extends Sprite
{
public function HelloWorld()
{
var textField:TextField = new TextField();
textField.text = "Hello, World!";
stage.addChild( textField );
}
}
}
Compile the applicationBefore you can run and debug the application, compile the MXML code into a SWF file using the amxmlc compiler. The amxmlc compiler can be found in the bin directory of the Flex SDK. If desired, you can set the path environment of your computer to include the Flex SDK bin directory. Setting the path makes it easier to run the utilities on the command line.
Running amxmlc produces HelloWorld.swf, which contains the compiled code of the application. Note: If the application does not compile, fix syntax
or spelling errors. Errors and warnings are displayed in the console
window used to run the amxmlc compiler.
For more information, see Compiling MXML and ActionScript source files for AIR. Test the applicationTo run and test the application from the command line, use the AIR Debug Launcher (ADL) to launch the application using its application descriptor file. (ADL can be found in the bin directory of the AIR and Flex SDKs.) From the command prompt, enter the following command: adl HelloWorld-app.xml For more information, see Device simulation using ADL. Create the APK package fileWhen your application runs successfully, you can use the ADT utility to package the application into an APK package file. An APK package file is the native Android application file format, which you can distribute to your users. All Android applications must be signed. Unlike AIR files, it customary to sign Android apps with a self-signed certificate. The Android operating system does not attempt to establish the identity of the application developer. You can use a certificate generated by ADT to sign Android packages. Certificates used for apps submitted to the Android market must have a validity period of at least 25 years. Generate a self-signed certificate and key pair From the command prompt, enter the following command (the
ADT executable can be found in the bin directory
of the Flex SDK): adt –certificate -validityPeriod 25 -cn SelfSigned 1024-RSA sampleCert.pfxsamplePassword This example uses the minimum number of attributes that can be set for a certificate. You can use any values for the parameters in italics. The key type must be either 1024-RSA or 2048-RSA (see the ADT certificate command). Create the AIR package From the
command prompt, enter the following command (on a single line): adt -package -target apk -storetype pkcs12 -keystore ../sampleCert.p12 HelloWorld.apk HelloWorld-app.xml HelloWorld.swf You will be prompted for the keystore file password. Type the password and press Enter. For more information, see Packaging a mobile AIR application for Android. Install the AIR runtimeYou can install the latest version of the AIR runtime on your device from the Android Market. You can also install the runtime included in your SDK on either a device or an Android emulator. From the command
prompt, enter the following command (on a single line): adt -installRuntime -platform android -platformsdk Set the -platformsdk flag to your Android SDK directory (specify the parent of the tools folder). ADT installs the Runtime.apk included in the SDK. For more information, see Install the AIR runtime and applications for development. Install the AIR app From the command
prompt, enter the following command (on a single line): adt -installApp -platform android -platformsdk path-to-android-sdk -package path-to-app Set the -platformsdk flag to your Android SDK directory (specify the parent of the tools folder). For more information, see Install the AIR runtime and applications for development. You can launch your app by tapping the application icon on the screen of the device or emulator. |
|