Build optimized RSLs with ANT

Runtime Shared Libraries (RSLs) are libraries of components that are shared by applications in the same domain.
You can shrink the size of your application’s resulting SWF file by externalizing shared assets into standalone files that you can separately download and cache on the client. Multiple Flex applications can load these shared assets at runtime, but each client need only to download them once. These shared files are called Runtime Shared Libraries.

Here is my files:
src/local.build.properties
flex.sdks.dir=D:/Development/Flex/sdks
testAppPath=C:/Program Files/Mozilla Firefox/firefox.exe
testAppFileName=application.swf
deploymentPath=D:/Development/Projects/FirstFlexProject/bin

src/config.xml
<project name=”Config” basedir=”.”>
<property file=”local.build.properties”/>
<property name=”flex.sdks” value=”${flex.sdks.dir}”/>
<property name=”flex.sdk.version” value=”2.0.1″/>
<property name=”flex.home” value=”${flex.sdks}/${flex.sdk.version}”/>
<property name=”flex.lib” value=”${flex.home}/frameworks”/>
<property name=”flex.compc.jar” value=”${flex.home}/lib/compc.jar”/>
<property name=”flex3.sdk.version” value=”3.0.0″/>
<property name=”flex3.home” value=”${flex.sdks}/${flex3.sdk.version}”/>
<property name=”flex3.mxmlc.jar” value=”${flex3.home}/lib/mxmlc.jar”/>
<property name=”flex3.compc.jar” value=”${flex3.home}/lib/compc.jar”/>
<property name=”flex3.asdoc.jar” value=”${flex3.home}/lib/asdoc.jar”/>
<property name=”flex3.lib” value=”${flex3.home}/frameworks”/>
<property name=”manifest.uri” value=”http://www.test.com/2006/urlkit”/>

<property name=”testApplication” value=”${testAppPath}”/>
<property name=”app.src.dir” value=”.” />
<property name=”app.deploy.dir” value=”${deploymentPath}” />
<property name=”app.library.dir” value=”${app.deploy.dir}/libs” />

<property name=”lib.src” value=”../libsrc” />
<property name=”lib.deploy” value=”../lib” />
</project>

src/build_rsls.xml
<project name=”Build RSLs” default=”run” basedir=”.”>

<import file=”config.xml”/>

<!– macro to easily create an rsl –>
<macrodef name=”create-rsl”>
<attribute name=”rsl-dir”/>
<attribute name=”swc-dir”/>
<attribute name=”swc-src”/>
<attribute name=”swc-name”/>
<sequential>
<!– compile the shared library –>
<java jar=”${flex3.compc.jar}” fork=”true” failonerror=”true”>
<arg line=”+flexlib=${flex3.lib}” />
<arg line=”-source-path+=@{swc-src}” />
<arg line=”-o=@{swc-dir}/@{swc-name}.swc” />
<arg line=”@{swc-name}” />
</java>
<!–Extract compressed shared library–>
<unzip src=”@{swc-dir}/@{swc-name}.swc” mce_src=”@{swc-dir}/@{swc-name}.swc” dest=”@{rsl-dir}” >
<patternset>
<include name=”library.swf” />
</patternset>
</unzip>
<!–Optimize shared library –>
<java jar=”${flex3.home}/lib/optimizer.jar” fork=”true” failonerror=”true”>
<jvmarg line=”-ea -DAS3 -DAVMPLUS -Dflexlib=${flex3.lib} -Xms32m -Xmx384m -Dsun.io.useCanonCaches=false”/>
<arg line=”‘@{rsl-dir}/library.swf’”/>
<arg line=”–output ‘@{rsl-dir}/@{swc-name}.swf’”/>
<arg line=”–keep-as3-metadata=’Bindable,Managed,ChangeEvent,NonCommittingChangeEvent,Transient’ “/>
</java>
<delete file=”@{rsl-dir}/library.swf”/>
<!–Digest RSL–>
<java jar=”${flex3.home}/lib/digest.jar” fork=”true” failonerror=”true”>
<jvmarg line=”-ea -DAS3 -DAVMPLUS -Xms32m -Xmx384m -Dsun.io.useCanonCaches=false”/>
<arg line=”–digest.rsl-file @{rsl-dir}/@{swc-name}.swf”/>
<arg line=”–digest.swc-path @{swc-dir}/@{swc-name}.swc”/>
</java>

</sequential>
</macrodef>

<target name=”compile rls”>
<create-rsl rsl-dir=”${app.deploy.dir}” swc-dir=”${lib.deploy}” swc-src=”${lib.src}” mce_src=”${lib.src}” swc-name=”smallLinker” />
</target>

<target name=”compile flex sources” depends=”compile rls”>
<java jar=”${flex3.mxmlc.jar}”
fork=”true”
maxmemory=”512m”
failonerror=”true”>
<arg value=”+flexlib=${flex3.home}/frameworks”/>
<arg line=”-load-config+=flex-config.xml” />
<arg line=”-source-path .”/>
<arg line=”-output=’${app.deploy.dir}/${testAppFileName}’”/>
<arg line=”-runtime-shared-library-path=../lib/smallLinker.swc,smallLinker.swf”/>
<arg line=”-use-network=false”/>
</java>
</target>

<target name=”run” depends=”compile flex sources”>
<exec executable=”${testApplication}” spawn=”yes” dir=”../bin”>
<arg line=”‘${testAppFileName}’”/>
</exec>
</target>
</project>

libsrc/smallLinker.as
package
{
import flash.display.Sprite;
import mx.core.Application;
import mx.controls.DataGrid;

public class smallLinker extends Sprite{
private var application:Application;
private var dataGrid:DataGrid;
}
}

src/Application.mxml
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”>
<mx:Panel title=”IntelliJ IDEA Sample Flex Application” height=”20%” width=”30%” paddingTop=”10″ paddingLeft=”10″ paddingRight=”10″ paddingBottom=”10″ id=”mainPanel”>
<mx:DataGrid height=”300″ width=”200″ id=”MyGrid” x=”0″ y=”25″ selectedIndex=”0″>
<mx:columns>
<mx:DataGridColumn headerText=”Name” width=”75″>
</mx:DataGridColumn>
<mx:DataGridColumn headerText=”Info1″ width=”80″/>
<mx:DataGridColumn headerText=”Info2″/>
</mx:columns>
</mx:DataGrid>
<mx:Button label=”Click Me” click=”mx.controls.Alert.show(’Hello, World!’)”/>
</mx:Panel>
</mx:Application>

In this example smallLinker RSL includes Application and DataGrid classes

After deploying sources I got Application.swf (118 Kb) and smallLinker.swf (218 Kb)
Application.swf using smallLinker.swf as shared library on demand, i.e. using Application and DataGrid classes

click here for more info about Using Runtime Shared Libraries

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.