Author Archive: sbhaskar

Creating Flex SDK from AIR SDK

It’s easy to make a Flex SDK from an AIR SDK for which you don’t have a corresponding Flex SDK. This is what you do:

1. Download AIR SDK.
2. Locate the existing Flex SDK.
For a default installation, it’s available in the C:\Program Files\Adobe\Adobe Flash Builder Version\sdks folder.
3. In this location, extract AIR SDK to a new folder .
4. Copy the contents of your existing Flex SDK and paste it to the folder where you have AIR SDK, WITHOUT over writing any of the files.
Flex SDK is ready. When you create a new project, use this as the SDK.

Accessing the StageWebView object

Recently, for one of my articles on StageWebView, a reader has commented that he gets an error while calling a stage object through a preperationComplete event. I must say that while preparing the article, I never thought about that kind of a scenario. This blog entry is to explain how to sort this issue out.
This was the example that I provided:

==
<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"  
        xmlns:s="library://ns.adobe.com/flex/spark"  
        xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:local="*" creationComplete="init()"> 
 <fx:Declarations> 
  <!-- Place non-visual elements (e.g., services, value objects) here --> 
 </fx:Declarations> 
<fx:Script>
 <![CDATA[
  import flash.geom.Rectangle; 
  import flash.media.StageWebView; 
  import flash.net.*; 
  import flash.net.URLRequest; 
  public var webView:StageWebView = new StageWebView(); 
  public function init():void 
  { 
   webView.stage = this.stage;
   webView.viewPort = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight );
   webView.loadURL("http://www.bbc.com");
  }  
 ]]>
</fx:Script>  

</s:WindowedApplication>

===
Compiling the code generates the following error: “Cannot access a property or method of a null object reference.” When a creationComplete event fires, the stage is still null. It is not initialized yet. The stage property acquires a value only after the display object has been added to the stage’s display list. Here, we don’t have any display object added to the stage. If you are accessing the function through another component, say a button, you don’t find this issue.

One solution is to wait for the addedToStage event. This event gets fired whenever the stage is initialized and obtains a value. The issue I explained above has nothing to do with the StageWebView api. See the code below. Using the creationComplete event, I called in the init() function which listens for the addedToStage event. After it’s fired, the target function accesses the stage object.

The following code works without any issues:

==
<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"  
        xmlns:s="library://ns.adobe.com/flex/spark"  
        xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:local="*" creationComplete="init()"> 
 <fx:Declarations> 
  <!-- Place non-visual elements (e.g., services, value objects) here --> 
 </fx:Declarations> 
<fx:Script>
 <![CDATA[
  import flash.geom.Rectangle; 
  import flash.media.StageWebView; 
  import flash.net.*; 
  import flash.net.URLRequest; 
  public var webView:StageWebView = new StageWebView(); 
  public function init():void 
  { 
   this.addEventListener(Event.ADDED_TO_STAGE , handleLoad );
  } 
  public function handleLoad(e:Event):void
  {
   webView.stage = this.stage;
   webView.viewPort = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight );
   webView.loadURL("http://www.bbc.com");        
  }
 ]]>
</fx:Script>  

</s:WindowedApplication>
==

Creating an apk File Using FlashBuilder 4.0

Using FlashBuilder 4.0, you cannot create an apk file through the GUI elements. You need to use AIR SDK commands for accomplishing this. The steps for creating an apk file is provided below:

  (The bin directory refers to the bin directory in your latest AIR SDK. Do the following after you create an SWF file.)

1. In the application descriptor file, edit the supported profiles to <supportedProfiles>mobileDevice</supportedProfiles>.

2. Create a certificate file (.pfx file).

bin\adt -certificate -cn <any-name> 1024-RSA <certificate-file-name>.pfx <password>

3. Assign the certificate to the swf file.

bin\adt -package -storetype pkcs12 -keystore <certificate-file-name>.pfx -storepass <password> <air-application-name>.air <application-descriptor>.xml <swf-file-name>.swf

This command creates a digitally signed air application.

4. Create an apk file.

bin\adt -package -target apk-emulator -storetype pkcs12 –keystore <test_cert.pfx> <name-apk>.apk <application-descriptor>.xml <swf-file-name>.swf

This command creates a valid apk file that you can install in an emulator. You need to provide the password you set in step 3.

5. Install the apk file in the emulator.

<emulator-install-dir>\tools\adb install <apk-name>