WHAT YOU WILL LEARN IN THIS CHAPTER:
Adobe's portfolio includes numerous software products that enable you to author Flash-based mobile applications. These include Flash Builder, an Integrated Development Environment (IDE) that fully supports the build of Flex Framework and AS3-based mobile projects, Flash Professional for Flash-authored projects, Device Central for emulating content, and the AIR SDK for targeting multiple platforms.
In this chapter you take a close look at Flash Builder, learning how to create a Flex mobile project and how to run that project on the desktop, and also take a look at how to configure the project to run on Apple iOS, BlackBerry Tablet OS, and Google Android.
Flash Builder is the ideal tool of choice for creating mobile applications using the Flex framework. Built on top of Eclipse, an IDE widely used by many Java developers, Flash Builder is a robust development environment.
This book mainly focuses on building applications with the latest version of Flash Builder — at the time of writing, version 4.5.1.
NOTE If you are using Flash Builder 4.5.0, you will need to install the Flash Builder 4.5.1 updater, which you can find on the Adobe website (www.adobe.com/support/flex/downloads_updaters.html).
Toward the end of this chapter, you are guided through setting up a Flex mobile project using the IDE.
Flash Builder has numerous features that simplify the task of building mobile applications, making it easier and fun.
Cool features in the latest version include wizards to target new devices that support Adobe AIR, such as the BlackBerry Playbook and Google Nexus One. You can run content on supported devices connected to your development environment, so you can install, test, and run an application instantly.
Within the IDE are tabs to switch between a mobile application's source code and the Design view. Within the Design view you can drag and drop components from panels within the IDE. The Source code view supports color schemes that can be applied to AS3 syntax and MXML script, making it easier to code documents. There is also an integrated debugger facilitating testing and advanced code hinting, and autocomplete commands that insert full class paths. Flash Builder is nothing short of a developer's dream.
The official requirements for Flash Builder are listed at the Adobe website (www.adobe.com/products/flash-builder/tech-specs.html).
The following sections take a look at the following Flash Builder features:
Flash Builder enables you to create different workspaces. A workspace contains each of the projects that you create in Flash Builder, and, by default, the workspace is the Flash Builder installation path, but this can be changed. You can also add multiple workspaces to keep collections of projects separate. I recommend for the examples used in this book that you create a workspace and keep the projects separate.
In this section you take a look within the workspace and a closer look at the tools within the IDE used to build Flex and ActionScript-based mobile applications.
Flash Builder includes a number of view panels that provide various features and configurable options for developing and testing applications.
Perspectives are a specific arrangement of view panels displayed together, aimed at providing suitable tools for a particular task.
By default, Flash Builder has two perspectives: the Flash perspective, which contains an arrangement of view panels for authoring your applications, and the Flash Debug perspective, which contains an arrangement of view panels for debugging applications.
Flash Builder will ask you to switch to a different perspective if you are debugging an application.
The following lists some of the view panels available in the Flash Perspective by default:
Figure 2-1 shows the Flash Perspective with the view panels displayed at the bottom of the IDE and the button highlighted on the top right.
The Flash Debug perspective contains view panels that enable you to rigorously test your application using a variety of features in a debugging session, including the ability to step through your code while the application is running.
The aim of the perspective is to help you to examine the source code and values assigned to variables, ultimately helping you to find problems in your application. The Flash Debug perspective launches when you select to run a debug session.
When you first run Flash Builder the option to show the Flash Debug perspective is unavailable. You need to have opened it at least once before within the workspace. Navigate to Window Perspective Flash Debug. You can also open the perspective by clicking the Open Perspective button and then selecting Flash Debug.
The following lists some of the view panels that are displayed when the Flash Debug perspective is open:
Each of these panels can be seen in Figure 2-2.
The Source view is simply the code editor where for the majority of this book you will be spending time following many of the example listings covered (Figure 2-3). Next take a look at the Design view contained within the Editor Area view panel (Figure 2-4). The Design view gives you a preview of your application.
In the Design view are several panels you can use to help facilitate the design of an application:
By default, the Components panel appears in the same pane as the Outline view panel on the left of the screen, and the States and Properties view panels appear on the right. The Design view is fully interactive, so you can drag and drop components directly onto the Stage area of the design.
Flash Builder also has controls to select the orientation of the mobile device, so you can choose to preview content in landscape or portrait mode.
NOTE You can only switch to the Design mode when the source is MXML and error free. You cannot switch to Design mode when editing an .as file.
The Properties view located to the bottom right of the Flash Builder has multiple sections for editing:
When you make updates to the style properties the changes are reflected in the Design view, and depending on which component you select in the Design view, the sublist of properties that appear in the Properties view panel may change.
A number of different features were covered in the previous section on Flash Builder. In this section, you take a look at using the IDE for yourself.
Over the course of this chapter you'll follow the creation of the Hello World App project. This mobile application simply enables you to enter your name onscreen and present it back with the text “Hello World, my name is ...” — a simple example, but enough to get you started using Flash Builder.
The first few steps take you through defining the Flex Mobile Project settings, which are usually the first things you encounter when you start a new project.
Within the Flex Mobile Project panel, you also target your development for the mobile platforms supported by Flash Builder.
After defining the Project Settings, the next few steps take you through targeting the three mobile platforms: Apple iOS, Google Android, and BlackBerry Tablet OS.
NOTE At this point you should familiarize yourself with the steps under “Defining the Flex Mobile Project Settings” and “Targeting Mobile Devices on Different Platforms” because these will be mentioned only briefly when you start other example projects in later chapters.
In the Flash Builder Package Explorer panel, you should see that the Hello World project has now been created, and several files have been automatically generated for the project.
In the src directory, you will see the default package folder, with the main application file HelloWorldApp.mxml.
In the views package folder, you will see a file called HelloWorldAppHelloWorldView.mxml. The name of the file is built from a combination of the project name and the initial view title defined. The file represents the first view class that the application will see when launched.
Rename the class via Flash Builder to HelloWorldAppHome.mxml. Do this by highlighting the file and selecting File Rename from the Flash Builder menu. In the Rename Class panel that opens, ensure the Update references box is checked, enter HelloWorldAppHome as the name for the file, and then click OK to confirm.
The third file generated is HelloWorldApp-app.xml; this is the AIR application descriptor file template. AIR application descriptor files are explored in more detail in Chapter 3.
Last, the fourth file generated is the blackberry-tablet.xml file, which is generated when you target your projects to run on the BlackBerry Tablet OS. BlackBerry Tablet OS files are explored in more detail in Chapter 3.
In Listing 2-1 you see the main application file HelloWorldApp.mxml, with the fx and s namespaces defined. Notice here that the firstView property on the application has been set to views.HelloWorldAppHome to reflect the updated filename.
LISTING 2.1: The HelloWorldApp.mxml application file for the Hello World project
<?xml version="1.0" encoding="utf-8"?>
<s:ViewNavigatorApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
firstView=“views.HelloWorldAppHome”>
</s:ViewNavigatorApplication>
In Listing 2-2 you see the <s:View> container defined in the HelloWorldAppHome.mxml file. This has been modified slightly from the generated file, with the title property set to display Hello World.
LISTING 2.2: The HelloWorldAppHome.mxml view for the Hello World project
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
title=“Hello World”>
</s:View>
Ensure these two files have been modified as shown in the first two listings, and then follow these steps to build on the example:
LISTING 2.3: Adding two <s:Label> components to the view in HelloWorldAppHome.mxml
<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="Hello World"> <s:Label x=“63” y=“56” width=“289” fontSize=“26” text=“My name is:”/>
LISTING 2.4: Adding two <s:TextInput> components to the view in HelloWorldAppHome.mxml
<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="Hello World"> <s:Label x="63" y="56" width="289" fontSize="26" text="My name is:"/> <s:Label x="63" y="182" width="289" fontSize="26" text="I live in:"/> <s:TextInput id=“nameTxt” x=“63” y=“98” width=“350” prompt=“Enter a name here...”/> <s:TextInput id=“locationTxt” x=“63” y=“230” width=“350” prompt=“Enter location here...”/> </s:View>
LISTING 2.5: Adding a <s:Button> component to the view in HelloWorldAppHome.mxml
<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="Hello World"> <s:Label x="63" y="56" width="289" fontSize="26" text="My name is:"/> <s:Label x="63" y="182" width="289" fontSize="26" text="I live in:"/> <s:TextInput id="nameTxt" x="63" y="98" width="350" prompt="Enter a name here..."/> <s:TextInput id="locationTxt" x="63" y="230" width="350" prompt="Enter location here..."/> <s:Button x=“63” y=“402” label=“Submit”/> </s:View>
After clicking OK the file will be generated in the Project Explorer.
LISTING 2.6: Adding a <s:Label> to the view in HelloWorldAppMessageView.mxml
<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title=“Your message...”> <s:Label id=“messageTxt” color=“#868686” paddingLeft=“20” paddingRight=“20” paddingTop=“20” width=“100%” height=“100%” fontSize=“32”/> </s:View>
LISTING 2.7: Adding a new function called onSubmit() to the <fx:Script> declaration in HelloWorldAppHome.mxml
<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="Hello World"> <fx:Script> <![CDATA[ protected function onSubmit():void {} ]]> </fx:Script> <s:Label x="63" y="56"
width="289" fontSize="26" text="My name is:"/> <s:Label x="63" y="182" width="289" fontSize="26" text="I live in:"/> <s:TextInput id="" x="63" y="98" width="350" prompt="Enter a name here..."/> <s:TextInput id="" x="63" y="230" width="350" prompt="Enter location here..."/> <s:Button x="63" y="402" label="Submit"/> </s:View>
LISTING 2.10: Assigning the onSubmit() method to the click property on the <s:Button> component in HelloWorldAppHome.mxml
<s:Button x="63"
y="402"
label="Submit"
click=“onSubmit()”/>
In onSubmit(), you've created a function that will use the data set via the text input fields, pushing the data object dObj and the name and location properties set on that object through to the HelloWorldAppMessageView. To utilize the data object, you will need to update the HelloWorldAppMessageView.
You can use one of the following event properties to handle what happens when the <s:View> component renders to the screen:
Similar to the click event property used for the <s:Button> component, you assign a method to handle the creationComplete and viewActivate properties.
LISTING 2.11: Assigning the onCreationComplete() method to the creationComplete property in HelloWorldAppMessageView.mxml
<?xml version="1.0" encoding="utf-8"?> <s:View xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" title="Your message... " creationComplete=“onCreationComplete()”> <fx:Script> <![CDATA[ protected function onCreationComplete():void {} ]]> </fx:Script> </s:View>
Here you see that the data object for HelloWorldAppMessageView is utilized, and the name and location properties that were set in HelloWorldAppHome are referenced and assigned to the text property on messageTxt. Now, whenever the pushView() method is invoked via the onSubmit() method in HelloWorldAppHome, the onCreationCompete() method will display the text entered by the user.
The Hello World App is now ready for testing. Next you take a look at running your project by setting up run configurations.
Run configurations are a key feature of Flash Builder that enable you to create and manage how you run and debug your mobile projects. You can elect to run your mobile application on the desktop, or directly on a connected device.
When you run your project on the desktop you can select from a number of devices, enabling you to run your applications using different screen sizes and pixel densities.
The next few sections take you through defining run configurations for desktop and devices.
First, to set up a project that runs on the desktop, follow these steps:
At this stage you could also elect to choose BlackBerry Tablet OS or Apple iOS and select a device that runs on those target platforms. For the majority of the book the example projects will be emulated using the Google Android platform and the Google Nexus One device profile.
WARNING Be aware that selections for the Target Platform in the Run Configuration window will appear only if you have enabled your application to be targeted on that platform. So, if you have targeted your application to run only on Google Android Platform, neither Apple iOS nor BlackBerry Tablet OS devices will be selectable here.
Once your project's run configuration has been defined you will be able to launch your mobile application. To launch the application as it is currently follow these steps:
In the next section you take a look at the steps to create run configurations on Apple iOS, BlackBerry Tablet iOS, and Google Android devices.
Once you've set up a run configuration for the Hello World application on the desktop, return to the Run Configurations window to set up run configurations for an actual mobile device on each of the target mobile platforms supported, starting with Google Android.
Follow the next steps to create a run configuration for devices running the Google Android OS platform:
These are the only steps you need to take to create a run configuration for Google Android devices in Flash Builder. You will also need to make sure your device is connected and has USB debugging enabled.
For Google Android devices running Android version 2.3.4, you need to ensure USB debugging is enabled.
If you have a BlackBerry Tablet OS device like the BlackBerry PlayBook, the next section covers the creating run configurations for BlackBerry Tablet OS.
Next take a look at defining a run configuration for devices running the BlackBerry Tablet OS.
Unlike the Google Android platform, you will need to configure a BlackBerry Tablet OS device in order for the run configuration to be completed (Figure 2-22).
The next few steps in Flash Builder require that you use some properties from your BlackBerry Tablet OS device. Here you use the BlackBerry PlayBook, where you will need to obtain an IP address and a password to run you applications on the device.
NOTE In Figure 2-25 you will actually see that the PlayBook is connected via USB and Wi-Fi; thus, there are two IP addresses. The first IP address shown, 169.254.168.221, corresponds to the USB connection, whereas the second IP address, 10.0.1.2, is the Wi-Fi network connection. Either can be used for the Device IP, which is set in step 5. However, in this example, follow the USB route. Also, note that the IP address will change whenever you connect your device.
Before you complete your test device you will need to ensure the device has a debug token installed. This will enable you to run applications on your device that are not digitally signed. For this you will need to register with RIM, who will be able to send you RDK and PBDT versions of a CSJ file, both required to create your debug tokens.
The window in Figure 2-27 shows an example of the completed details you will need to provide when creating a debug token and uploading it straight to the device.
NOTE For more information on debug tokens, I recommend reading the Running unsigned applications using a debug token article on the BlackBerry Developers website (http://docs.blackberry.com/en/developers).
Next take a look at defining a run configuration for devices running on the Apple iOS platform.
Unlike the Google Android and the BlackBerry Tablet OS platforms, for Apple iOS you will have to define the package settings before the run configuration is complete. Note the error message in Figure 2-30.
This requires you to obtain an Apple iOS Developer Certificate and Mobile Provisioning profile, which you will need to install on your device prior to deploying your mobile application. Before you can do this, you need to become a member of the iOS Dev Center.
The window in Figure 2-31 shows an example of the completed Digital Signature details.
NOTE For more information on generating certi. cates and installing mobile provisioning profiles on your iOS device, visit the iOS Provisioning Portal at the Apple iOS Developer website (http://developer.apple.com/devcenter/ios).
At this stage you have learned how to create configurations for running your mobile applications on a connected device. The next chapter covers building and packaging for the three mobile platforms in more detail.
In this chapter you created a Flex-based mobile application using Flash Builder. Along the way you also explored the Flash Builder IDE in depth, gaining an understanding of some of the key concepts.
Over the course of the book, the Flash Builder IDE will become more and more familiar as you build on and create further examples.
You learned how to create run configurations that targeted each of the different mobile platforms supported in Flash Builder, and may have noticed the differences in how running on a Google Android is definitely the easier of the three platforms, closely followed by BlackBerry Tablet OS, and then Apple iOS. The latter two both rely on you registering with the development communities of Apple and BlackBerry, before you can get started.
In the next chapter you take a closer look at the AIR application descriptor file and building for each of the mobile platforms in more detail.
WHAT YOU LEARNED IN THIS CHAPTER
TOPIC | KEY CONCEPT |
Run configurations for Apple iOS devices | Obtain a developer certificate and provisioning profile from Apple.
Define the package settings. |
Run configurations for BlackBerry Tablet OS devices | Register with RIM for debug token details.
Create a test device profile. Enable development mode on the device. Set the IP address for the device. Connect the device via USB. |
Run configurations for Google Android devices | Ensure USB debugging is enabled on the Google Android device.
Connect the device via USB. |
18.191.252.126