Xaps Minifier. Visual Studio 2010 Add-on for Reducing the Size of Silverlight Applications

    I constantly work with Silverlight applications and upload releases regularly. Typically, I use the MVVM pattern and its implementation of Prism . As a result, several XAP files are created containing application assemblies and a manifest.

    Everyone who works in accordance with this approach notices that most XAP files contain duplicate assemblies. For example, when using the Prism library , almost every XAP file will contain all the assemblies from this library. Prism adds about 300 Kb to each XAP file, which can increase the application size by more than 1 Mb (if there are 4-5 XAP files). In addition, additional libraries (primarily UI elements) can further increase the size of the application.

    All these facts made me start looking for ways to reduce the size of XAP files.

    Idea


    I was working on one problem when I came across a blog post on Jeff Prosise . He mentioned that the assembly can be added to the application, but not to the XAP file. To do this, just set the parameter

    CopyLocal=false

    for each required assembly, which is in the References list . In this case, the project will refer to the desired assembly, but will not add it to the bin folder during compilation.

    This gave me the idea that all duplicate assemblies in the application can be affixed

    CopyLocal=false

    without changing only the assembly parameters of the main XAP file. It is the main XAP file that should contain all duplicate assemblies that will be automatically loaded into the Application Domain, and will be available for all assemblies from the remaining XAP files.

    This will look like a solution after minimization

    The picture above demonstrates how the solution will be updated if it is changed in accordance with the rules described above. Imagine that each project is a separate XAP file. In this case, Project 1 and Project 2 contain the same pair of assemblies ( Assembly 1 and Assembly 2 ), and I set them CopyLocal=falseto exclude them from Project1.xap and Project2.xap.

    Build 1 ( Assembly 1 ) is already in the main project ( the Main by Project ), but because it added to the main project is not required. In contrast, Assembly 2 ( Assembly 2 ) should be added to the main project to ensure the operability of the first and second project.

    Project 2 ( Project 2 ) contains assembly 4 ( Assembly 4 ), which is already present in the main project - we exclude this assembly from project 2.

    Project 3 ( Project 3 ) is not changeable, because It contains only unique assemblies.

    Let's summarize what has been optimized:
    ProjectThe number of assemblies in XAP before optimizationThe number of assemblies in XAP after optimization
    Project 1four2
    Project 2fourone
    Project 333
    Main project3four
    Total14ten

    As you can see, the total number of files in the application has decreased, even despite the increase in the main project by one assembly.

    Naturally, making these changes manually will be a slow process and may lead to errors. Therefore, I decided that I should write an add-on for Visual Studio that would automate these operations.

    Add-on implementation


    I will describe only the general algorithm according to which the optimization of XAP files occurs.
    Duplicate assembly exclusion algorithm

    1. Getting all Silverlight projects . The search is based on the fact that the project file contains the parameter.

      ProjectTypeGuids

      It contains a GUID that indicates the type of project (console application, asp.net application, etc.)
    2. Search for the main project . Starting from this project, the application starts and it is in it that duplicate assemblies from other projects should be added. The search is based on the value of the project parameter.

      SilverlightAppEntry

      If this parameter is initialized with the name of a valid (present in the project) class, then such a project is the main one in the application.
    3. Getting a list of duplicate assemblies. All Silverlight application projects are bypassed and all assemblies that occur more than two times are remembered
    4. Adding a duplicate assembly to the main project , if it is not there.
    5. We set the parameter for the assembly

      CopyLocal=False

      in all projects where it occurs, except for the main project.

    As a result of this algorithm, the current application in Visual Studio will be updated. You can immediately compile it and see the difference in the size of the XAP files.

    Work visualization


    Visual Studio allows you to use various built-in mechanisms to display the current status of the running add-on.

    I chose four options:
    1. Animation . An animated icon is displayed in the status bar throughout the entire operation of the add-on
    2. Indicator of progress . The status bar shows the percentage of optimization relative to each duplicate assembly
    3. Text message . The status bar displays a text message about the current completion step
    4. Logging actions in OutputWindow . A separate window displays all messages during the add-on.

    Publish Add-ons


    In order to publish the add-on on Visual Studio Gallery , it is enough to have a Live account, go to the gallery website, click Upload and follow the add-on publication wizard.

    Add-on Installation


    The fastest and most convenient option for installing add-ons is to use Visual Studio Gallery . To do this, in Visual Studio 2010, just open the Visual Studio Extension Manager (main menu-Tools-Extension Manager), select Online Gallery, and enter “xap” in the Search Online Gallery field. After that, you can select Xaps Minifier and click Download - downloading and installing add-ons will begin.

    Install add-ons through Visual Studio Extension Manager

    Using add-ons


    After installing and restarting Visual Studio, you should load the Silverlight project, call the context menu for the solution and select the Minify Xaps item.

    Launch add-ons

    The process of analyzing and optimizing the application will start, and the progress will be displayed in the status bar of Visual Studio. After the optimization process is completed, a dialog box appears with information on how many assemblies it was possible to exclude from the application, how many assemblies the main project increased and how many projects were changed.

    Optimization of real projects


    The first test program was my demo application, which I presented at the Remix10 conference. This application contained 5 Silverlight projects and 4 XAP files. The size of the non-optimized application was 1.2 Mb, and after optimization it decreased to 500 Kb. At the same time, the size of the main project increased from 120 Kb to 400 Kb.

    The second application was the Prism Demo application presented by John Papa at PDC09 . This application contained 7 Silverlight projects and 4 XAP files, and the total size of XAP files was 5.7 MB. After optimization, this application decreased to 1.6 Mb (!!!).

    Known analogues


    I am not aware of the direct analogues of my implementation of the Visual Studio add-on. But the topic of optimizing the size of Silverlight applications is relevant, and therefore companies such as Component One and Telerik offer their tools. Unlike my idea, they use a completely different approach. Their tools analyze the finished XAP file, find unused code (classes, XAML code) and remove it from assemblies.

    In my opinion, this is somewhat controversial decision. Here are my arguments:
    1. I can load classes dynamically, but this cannot be tracked. In this case, I should remember all classes / controls that should not be removed. This can lead to a sharp decrease in the stability of the application, increase the load on the testing department, etc. My add-on does not change any assemblies, and therefore is much safer.
    2. I do not think that third-party providers of paid libraries will include an agreement on the modification of their libraries in licensing agreements. This is exactly what is happening with Component One . It seems to me that this is simply illegal.
    3. My colleague Alexey and I spent about one man-week on this project. The companies mentioned have spent much more time and effort.
    4. My add-on does not depend on the libraries that are used in the application. At the same time, Telerik Assembly Minifier can only optimize Telerik libraries.
    5. When using my add-on, developers do not need to re-optimize the application when new versions of libraries appear

    In any case, my add-on can be used in conjunction with the Component One XapOptimizer and Telerik Assembly Minifier tools .

    Current restrictions


    The add-on does not currently work with:
    • Silverlight for Windows Phone 7;
    • XNA for Windows Phone 7;
    • Silverlight for Symbian;

    There is no technical difficulty to add support for these types of projects that differ only in GUIDs. In the near future it will be implemented.

    In addition, the add-on does not allow you to get rid of assemblies that are added to the XAP file dynamically. This happens when the added assembly depends on other assemblies, and the latter are not explicitly added to the project. In this case, it is recommended to add these assemblies manually to all projects where it falls into the XAP file and re-optimize the application.

    The last limitation that follows from the addition algorithm is that applications consisting of one XAP file cannot be optimized.

    Acknowledgments


    I would like to express my gratitude to my friend and colleague Alexei Gladkikh for the implementation of the prototype algorithm for optimizing XAP files.

    Source code


    The latest sources can be downloaded here . The add-on installation can be downloaded from the official add- on page on the Visual Studio Gallery website or installed via the Visual Studio Extension Manager (see above).

    Share your experience


    It is very important for me to get the response from real users of the add-on. The wishes that I have already received have made XAPs Minifier more convenient and faster. Your comments can take it to a new level.

    I hope that this tool will be a must have for every Silverlight developer.

    Also popular now: