SWC and SWF files in native extensions

I’d like to highlight the role of your ActionScript library — a SWC file — in native extensions. And while I’m at it, I’d like to discuss the role of  the SWF files that the SWC file contains.

You’ve read it before: A native extension is made of two parts: The ActionScript side and the native code side. Here we are just talking about the ActionScript side.

You build the ActionScript side of your extension into an ActionScript library that is a SWC file. The SWC file is an archive file (think ZIP file) that contains your ActionScript classes and other resources, such as its images and strings.

Suppose your extension targets different platforms — for example, both iOS and Android. If  your extension requires a different ActionScript implementation for each platform, create a different SWC file for each platform. A best practice  is that all the ActionScript implementations have the same public interfaces. However, even if all the ActionScript implementations have the same public interfaces, what happens inside the implementation can vary by platform. If so,  you’ll have to create multiple SWC files.

The SWC file comes into play when you package your extension into an ANE file. To do the packaging, you use the ADT command-line tool. You specify a SWC file in the -swc option of the ADT packaging command. For example:

adt -package <signing options> -target ane MyExtension.ane MyExt.xml
    -swc MyExtension.swc
    -platform Android-ARM -C platform/Android .
    -platform iPhone-ARM -C platform/ios .
    -platform default -C platform/default library.swf

The SWC file contains a file called library.swf.  It is the main SWF file of your ActionScript library. ADT automatically puts the library.swf from the SWC file into the ANE file. When an AIR
application uses a native extension, it includes the extension’s ANE file in its library path so that the application can compile. In fact, the application compiles against the public interfaces in library.swf.

Let’s say, as discussed above, you have different SWC files for each platform, but they all share the same public interfaces. In this case, it doesn’t matter which of the platform-specific SWC files you use in the -swc option of the ADT command. It doesn’t matter because the library.swf file in the SWC file is used only for application compilation.

Application execution is another matter. Take a look again at the above ADT command.  Each platform directory (platform/android, platform/ios, and platform/default in this example) specifies the files to include in the ANE package. The -C option tells ADT to make the specified directory the current directory. ADT puts the files and directories that follow into the ANE package. Each platform directory has a library.swf file in it.  For the iOS and Android platform directories, the ‘.’ indicates that all the files in the current directory are to be packaged into the ANE. The files include the library.swf as well as native libraries.

Therefore, you must put the appropriate library.swf into each platform directory. When you have a different SWC file for each platform, get the library.swf from the appropriate SWC file. You can extract the library.swf from a SWC file with extraction tools such as WinZip.  When the application executes and calls your extension class, this is the library.swf file that runs.

Note the following about the library.swf file:

  • ADT requires that you provide a main SWF file named library.swf for each platform. When you create a SWC file, library.swf is the name of the main SWF file.
  • The library.swf file for each platform is different if your ActionScript side has platform dependencies.
  • The library.swf file for each platform is the same if the ActionScript side has no platform dependencies.
  • The library.swf for each platform can load other SWF files that you include in the platform-specific directory. These other SWF files can have any name.

Detailed documentation about creating extensions is at Developing native extensions for Adobe AIR.

For more information about using a native extension in your AIR application, see Using native extensions in AIR applications.

Jackie Levy, Content and Community Lead

Comments are closed.