Flash Development for Android Cookbook
上QQ阅读APP看书,第一时间看更新

Using Flash Builder 4 and below to develop Android applications

To develop mobile Android application in Flash Builder 4, we will need to configure Flash Builder to enable access to a mobile Flex SDK. See the previous recipe if you have not yet configured Flash Builder or Flex Builder in this manner.

How to do it…

There is no specific mobile workflow or tooling built into versions of Flash Builder prior to Flash Builder 4.5. By taking the following steps, we can ensure that our project will be mobile-compatible:

  1. In Flash Builder, right-click in the Package Explorer panel and choose New | Flex Project. Alternatively, we can choose ActionScript Project, but this will not include any mobile benefits, as the actual Flex SDK components will not be employed. However, it is useful to note that ActionScript projects will generally perform better than their Flex counterparts simply due to the fact that they do not rely on such a heavy framework.
  2. The New Flex Project dialog will appear in which you must provide a Project name, and select whether to create the project targeting Web or Desktop. If this project will be compiled for AIR for Android, we will want to make sure to choose Desktop, as this application type will target the Adobe AIR runtime. If creating a project targeting Flash Player in the browser, we will choose Web.
  3. When choosing Desktop, we will also want to be sure to choose a mobile-enhanced version of the Flex SDK for our Android project. Flex 4.5 and above include everything we need to begin developing robust Android applications.
  4. The last thing we must do is make sure that the mobile-enabled Flex SWCs are going to be used in our project. In order to declare <s:ViewNavigatorApplication> or <s:TabbedViewNavigatorApplication> for the main container of our project, these specific SWCs must be accessible, else Flash Builder will report errors.
  5. The final section of the New Flex Project dialog allows us to be sure the mobile SWCs are included. You will notice that mobilecomponents.swc is not included in our project. Select the tab labeled Library path and click on the button labeled Add SWC…:
  6. When the Add SWC dialog appears, browse to the location of the selected Flex SDK. Assuming we unpackaged the SDK to C:\SDKs\Flex4 we will now browse to C:\SDKs\Flex\frameworks\libs\mobile select the mobilecomponents.swc file, and click on Open. This will add support for the mobile components to our application.
  7. Complete the project setup. We are now able to use mobile specific containers and controls without receiving errors from Flash Builder, but we must make a few more adjustments in order to correctly compile our application.
  8. Locate the AIR descriptor file in your project. It is normally named something like {MyProject}-app.xml and resides at the project root. Open this file and change the <visible> attribute to true. It may be necessary to uncomment this node, if it has been commented out.
  9. Right-click on your project in the Package Explorer and select Properties.
  10. Select the Flex Compiler menu option and add the following to the Additional compiler arguments: theme=C:\{SDK Location}\frameworks\themes\Mobile\mobile.swc
  11. Finally, switch the root node of your main application file from <s:Application> to <s:ViewNavigatorApplication>. We can now author and compile applications using the mobile flex framework components.

How it works…

When specifying which type of project we want to create in Flash Builder, the IDE automatically makes available certain portions of the Flex framework so that we can work with all the components necessary for our project. Flash Builder 4 and earlier do not ship with any mobile-enabled Flex SDK and do not provide a workflow for Android projects. Because of this, we must explicitly tell Flash Builder to make use of these extra framework components.

The application descriptor file mentioned in the steps in the preceding section is used to configure an AIR application in various ways: setting the initial window properties, chrome attributes, and even system icons.

See also…

It is important to note that versions of Flash Builder prior to Flash Builder 4.5 will not include the ability to compile projects to .APK (the Android application file extension) and you will need to compile your project using the freely available AIR SDK. See Chapter 11 for information on how to do this.

It is also worth a mention that while you can develop your applications for Android using older versions of Flash Builder, you will not receive many of the benefits provided by a newer release, such as code completion.