Flex SDK 3 bug with SWC and GenericTag

| 3 min read

I recently used ANT and compc to compile a SWC. I had to include another SWC with classes and jpg and some other classes as I needed to force them at compile-time to make the getDefinitionByName class working.

If you don't know getDefinitionByName (and registerClassAlias and getClassByAlias), it make us able to create instances of classes with a string (from a XML for example). The problem is the compiler doesn't include them at compile-time if they are not used.

You will find tons of people talking about that if you google it.

One trick is add a public var myClass:MyClass = null; in the code or use a array. Not really ideal as you have to do that for every class. Example:

public var myClassPage:MyClassPage = null;
var PageClass:Class = getDefinitionByName("com.site.page.MyClassPage") as Class;
var page:SuperClassPage = new PageClass();

Here is a class that will help you manage that if you need:

package com.soundstep.managers {

import com.site.menu.basic.*;
import com.site.page.*;
import flash.net.*;

/**
* <b>Author:</b> Romuald Quantin - <a href="http://www.soundstep.com/" target="_blank">www.soundstep.com</a><br />
* <b>Class version:</b> BETA 1.0<br />
* <b>Actionscript version:</b> 3.0<br />
* <b>Copyright:</b>
* <br />
* <b>Date:</b> 05-2008<br />
* <b>Usage:</b>
* @example
* <listing version="3.0"></listing>
*/


public class ImportManager {

//------------------------------------
// private, protected properties
//------------------------------------

private static var INITIALIZED:Boolean = false;

private static var importManager:ImportManager = new ImportManager();
private static var importer:Array = [
BasicMenu,
Home,
Page1,
Page2,
Page3,
Page4,
Page5,
PageSub21,
PageSub211,
PageSub22,
PageSub221,
PageSub222,
PageSub223,
PageSub23,
PageSub231,
PageSub232,
PageSub233,
PageSub31,
PageSub311,
PageSub32,
PageSub321,
PageSub33,
Page4,
PageSub41,
PageSub411
];

//------------------------------------
// public properties
//------------------------------------

//------------------------------------
// constructor
//------------------------------------

public function ImportManager() {
if (importManager) throw new Error("ImportManager is Singleton.");
}

//
// PRIVATE, PROTECTED
//________________________________________________________________________________________________

private static function init():void {
for each (var i:Class in importer) {
registerClassAlias(String(i).substring(7, String(i).length-1), i);
}
INITIALIZED = true;
}

// PUBLIC
//________________________________________________________________________________________________

public static function getClass(className:String):Class {
if (!INITIALIZED) init();
for each (var i:Class in importer) {
if (String(i).indexOf(className)) {
return getClassByAlias(className);
break;
}
}
return null;
}

}
}

You can use it like that:

var PageClass:Class = ImportManager.getClass("MyClassPage");
var page:SuperClassPage = new PageClass();

Anyway, this is not the subject of the post. "Revenons a nos moutons", as we say in France.

I didn't want to force the import of the classes at compile-time in this way. I had a SWC compiled with flash with some fonts, movies and jpg. I was using this SWC to compile a SWF with mxmlc. I set an Ant task with compc to compile this SWC + the classes I needed with the getDefinitionByName into another SWC. And then, compile my SWF with this unique SWC.

It should have worked very well if compc was compiling my SWC. I had error about flash.swf.tags.GenericTag... nice...

After a bit of research, I saw that Adobe knows about it and are working on it. The problem is the jpg that are included in the first SWC. Even if you try to compile a SWF with mxmc including a SWC with jpg inside, you will get this error. Here are some links where they talk about:

http://bugs.adobe.com/jira/browse/SDK-14888
http://bugs.adobe.com/jira/browse/SDK-14667

You can see that there is a patch there: http://bugs.adobe.com/jira/secure/attachment/17851/GenericTag.patch

I don't know more about this bug and I'm not really a java man... anyway.

I had to find this GenericTag class that is extending flash.swf.Tag and make it extend flash.swf.tags.DefineTag (full path, not like it is written in the patch).

The class is located in a jar file, which you can open with winrar or similar: \flex\_sdk\_3.0.0.477\lib\swfkit.jar

and in the jar, the class is there: \flash\swf\tags\GenericTag.class

A "class" file is java class that is compiled. I found this nice tool to decompile it: http://classeditor.sourceforge.net/

I change: public class GenericTag extends flash.swf.Tag by: public class GenericTag extends flash.swf.tags.DefineTag

After that you just have to save the file and put it back in the jar. compc or mxmlc is now compiling without error.

I'll spare you this time, you can download the jar updated. This fix has been done for Flex SDK 3.0.0.477.