Deploy and work with Flex swc library

When we need to use a series of Actionscript classes in several projects and centralize them into a single place, the best option available is to deploy our classes as an SWC library and then add it to the “library path” under Flex Builder’s “build path” panel.

Deploy an swc file

To obtain an SWC file, we have first to create a library project by selecting “New > Flex Library Project” from Flex builder’s menu.
The file will be automatically generated under the bin folder as soon a new class is created and/or modified (under src folder) and it will have the same name of the project. Is also possible to copy .as files from a folder and paste them into src folder, but in this case the SWC file won’t be (re)deployed until we select the new added classes under “Flex Library Build Path“.
There are two other cases for which Flex Builder won’t (re)deploy the SWC file and is important to keep these in mind, because no errors nor notification messages will be showed by the ide and this could lead to a waste of time trying to figure out what is going on.
The two big things to remember are:

1. Be sure to check “Include Adobe AIR libraries” under “Flex library compiler” settings (this can be defined during library creation step or after) when the library make use of AIR api
2. Be sure that package path, declared into imported classes, match the structure of src folder (if we import a class “MyClass” with a package declaration like “com.mysite.ui” then src folder must contain “com/mysite/ui” and “MyClass” must be placed there, if we put it under the src root it won’t be compiled into SWC)

Import the swc file into a project

Once we get our library project and our compiled swc, we can import it into others Flex projects by going to “Flex build path > Library path” and selecting “Add project“. There are two ways of using the imported library and to determine the type of linkage to it: a static link (by choosing the default option “Merge into code“) or dynamic link (by choosing the default option “Runtime shared library (RSL)“). The difference between the two types of linkage are that, in the first all library content are compiled into the final project’s swf file, so once you import and merge the library, its swc file is not necessary anymore and you can distribute your application without the external library (it’s already embedded into the app), by using an RSL instead the library is loaded at runtime and the application swf doesn’t keep it’s data, but only a reference to its location, so the resulting file is smaller than the previous one, but you must be sure to release the Flex application in conjunction with the external library in order to works, otherwise Flex Builder will show an error saying “unable to open path/yourlib.swc” (where “path” is the path to the library you previously imported).