Category Archives: Hints & Tips

General hints and tips. Usually related to Flash, Flex or AIR in some way, but not always.

How to package Adobe AIR apps for Android that use the shared runtime

Since the release of the Adobe AIR 3.8 SDK, all Android apps are packaged as captive runtime, regardless of the packaging options you select in IDEs like Flash Builder.

To override this behaviour, simply add the following environment variable to your system settings:

AIR_ANDROID_SHARED_RUNTIME=true

Restart your IDE and the next time you package your Adobe AIR app for Android, you’ll be able to use the shared runtime as before!*

* Mixed results reported by users of Flash CC, works every time with pretty much everything else

How to opt-out of Adobe AIR app analytics on Android

If you ever publish an Android app using Adobe AIR, you’ll notice that the application ID is automatically prefixed “air.”, for example air.com.company.appname. This is to enable Adobe to track apps created using Adobe AIR in app stores like Google Play and Amazon Appstore.

If you want to opt-out of Adobe’s analytics, or just don’t want an “air.” prefix on your app ID, simply add the following environment variable to your system settings or build script:

AIR_NOANDROIDFLAIR=true

Restart your IDE and that’s it!

Unfortunately, opting-out of Adobe AIR app analytics on iOS is rather more complicated.

How to target legacy versions of Adobe AIR runtime on Android using Flash Builder 4.7 (without using captive runtime)

The problem

Despite Adobe Flash falling out of favour for many web applications, Adobe AIR remains a great option for cross-platform mobile app development and rapid prototyping, and with the advent of captive runtime for Android it’s now possible to distribute apps without having to ask users to download and install the Adobe AIR runtime separately.

However, if you’re looking to distribute a simple experiment or throwaway game that’s just a few 10s of 100s of kilobytes on its own, wrapping it up with a near 10MB runtime isn’t quite as desirable.

So, you can just select “Export application that uses a shared AIR runtime”, right? Sadly not. If you’re targeting the original Kindle Fire (AIR 2.7), any of the Kindle Fire HD range (AIR 3.1), or the Nook HD or HD+ (AIR 3.3) you’ll find that they all come with embedded, non-updateable versions of the Adobe AIR runtime which are at least a few versions older than the AIR SDK used by Flash Builder 4.7 for ActionScript projects (this is how to update it, in case you wondered).

Assuming your app doesn’t use any of the features added in more recent versions of AIR, this is how you can target legacy runtimes using Flash Builder 4.7:

The solution

1. Edit the .actionScriptProperties file

While Flash Builder doesn’t have an option to change the compiler used for ActionScript or ActionScript Mobile Projects, you can switch your project from the new ASC2 compiler to the standard ActionScript Compiler (ASC) by editing your project’s .actionScriptProperties file:

  1. Use Explorer/Finder to navigate to your project folder and locate .actionScriptProperties
  2. Open the file in a text editor, and find/replace useFlashSDK=”true” with useFlashSDK=”false”
  3. Save .actionScriptProperties
  4. Close then open your project using the right-click menu options (or restart Flash Builder)

Now, if you open your project properties in Flash Builder (right click > Properties) and select ActionScript Compiler, you’ll see that you can select which version of the Flex SDK (which includes ASC), and therefore AIR SDK, you’d like to use to compile your project.

2. Add the required SDK(s)

If the SDK you need isn’t listed, Adobe has instructions for adding SDKs to Flash Builder here, and archived versions of both the Flex and AIR SDKs can be downloaded from:

3. Add -swf-version

Once you’ve edited .actionScriptProperties and installed your SDKs, the final step is to ensure that the your project uses the correct SWF version for the target AIR runtime.

To do this, open the ActionScript Compiler properties for your project and add -swf-version=XX on a new line in the “Additional compiler arguments:” text box, where XX is the appropriate number from this table.

4. That’s it!

Just debug and compile your project as before, and you’ll have a nice, tiny APK that works on even the most awkward of devices!

Using shared runtime with AIR SDK 3.7+

If you want to re-enable the ability to package Android apps without the captive runtime using AIR SDK 3.7+, set the AIR_ANDROID_SHARED_RUNTIME environment variable to true.

Device appears to be offline?

If you receive the an error saying “Device appears to be offline. Restarting the device may fix the problem.” while testing or debugging your app in Flash Builder, simply overwrite [legacy_sdk]\lib\android\bin with the same folder from the latest Adobe AIR SDK, or copy aapt.exe, adb.exe, AdbWinApi.dll, AdbWinUsbApi.dll and dx.jar (under lib rep) from [latest_android_sdk]\android-sdk\platform-tools into the folder, overwriting the files that are already in there.

 

Generating a Code Signing Request using OpenSSL for Windows

If you’re developing apps for iOS (iPhone, iPod touch or iPad) you’ll need to generate a 2048-bit Code Signing Request (CSR) before you can create a Development Certificate and start testing your apps.

On OS X, you can use Keychain Access, but on non-server editions of Windows it’s a little less straight forward. Here’s how:

  1. Download and install the latest version of OpenSSL
  2. Once installed, you will need to add the OpenSSL bin folder to your PATH environment variable, e.g. c:\openssl\bin; if you’re not sure how, Google has the answer
  3. Open a command line window by selecting Start > Run… and entering “cmd”, then pressing OK
  4. To make sure you can find your CSR once it’s been created, type the following and press return:
    cd desktop
  5. To create your CSR, type the following an press return:
    openssl req -nodes -newkey rsa:2048 -keyout request.key -out request.csr
  6. Answer each of the questions
  7. That’s it: your CSR should be waiting for you on your Desktop, ready for upload to the iOS Provisioning Portal

Once your CSR has been approved and you have your Developer Certificate, you’ll need to convert it to a P12 file: instructions are here

Static initializers in AS3

Recently, several people have asked whether it’s possible to create some kind of constructor for static classes in ActionScript 3, and the answer is: yes!

The solution is  a static initializer. This is a block of code, surrounded by curly brackets but no function or var declaration, that runs the first time a property or method of a static class is called. Static initializers are called before any property value is returned or function executed, in the same way a constructor always runs before you can access the properties or methods or a regular class.

package
{
    public class MyStaticClass
    {
        // This is the static initializer
        {
            trace("Running static initilizer!");
            staticInitializer();
        }
        
        // We're calling a secondary method so we can use local variables
        static private function staticInitializer():void
        {
            var now:Date = new Date;
            baseColor = now.hours > 16 ? 0x000066 : 0x0000FF;
        }

        static public var baseColor:uint;
    }
}

Although it’s not strictly necessary to call a secondary method, Flash Player will throw an error if you declare local variables within the static initializer itself.

To prove that the static initializer is running first, simple trace out the baseColor property:

trace(MyStaticClass.baseColor);

If it’s after 4pm, this will result in the following output in the console window:

Running the static initializer!
102

Essential Eclipse plug-ins for Flash Builder 4 and Burrito

With the recent relase of Adobe Flash Builder 4, we thought we’d flag up the update URLs for a few of our favourite Eclipse plug-ins to save you having to hunt around for them.

To install plug-ins:

  1. Copy the appropriate URL to your clipboard
  2. In Flash Builder, select Install New Software… from the Help menu
  3. Click Add, paste URL in the Location field and click OK
  4. Select the plug-ins to install and click Finish

If you’ve spotted anything we’ve missed, add a comment or  tweet it to @mesmotronic.

Eclipse.org

This URL contains links to most of the plug-ins hosted on Eclipse.org, allowing you to add functionality like ANT, JDT (Java Development Tools) and PDT (PHP Development Tools). You’ll also need to add this URL to Flash Builder to enable you to install other plug-ins, like soapUI:

http://download.eclipse.org/releases/galileo/

Subclipse (SVN)

If you’re a regular user of version control, and you should be, the subclipse plug-in is essential:

http://subclipse.tigris.org/update_1.6.x

soapUI

For anyone who uses Web Services, soapUI offers a great way to view and test your services:

http://www.soapui.org/eclipse/update/site.xml

Android SDK

Now that you can develop application using AIR for Android, it could be argued that ADT (Android Developer Tools) isn’t necessarily essential, but if you’re looking to develop mobile apps for anything other than the latest handsets it’s definitely worth a look:

https://dl-ssl.google.com/android/eclipse/

TODO/FIXME

For anyone who makes regular use of the Tasks panel in FlashDevelop or Eclipse’s Java profile, this is a must.

You can download the plug-in here (requires manual installation).


	

Creating a simple HTTP proxy using PHP

Cross domain policy files, or rather a lack of them, are the bane of Flash and Flex developers’ lives. Whether it’s the Twitter API, data from Yahoo! Finance or one of any number of other data sources, the moment your SWF makes it to the web you’re faced with the same problem: Security Error!

The solution is to create a simple proxy on your server that can load the data for you and pass it to your application. If you’re using PHP, then this is about as simple as it gets:

// Too simple! (see comment below)
<?=file_get_contents($_REQUEST['url'])?>
<?php
$url = $_REQUEST['url'];
if (preg_match('/\b(https?|ftp):\/\/*/', $url) !== 1) die;
echo (file_get_contents($url));
?>

The preg_match ensures that the requested URL is valid and offers basic protection against anyone trying to maliciously access files on your server.

That’s it. Just paste that into a text file, save it as proxy.php and upload it to your server. You can then access any data you like simply by passing it a URL. Best of all, it works with both GET and POST, for example:

http://www.mydomain.com/proxy.php?url=http://blog.mesmotronic.com/index.php/feed

Happy proxy-ing!