Tuesday, April 26, 2011

Minify the size of your XAP file with some help from Telerik

As every good Windows Phone 7 developer knows. It's generally a very good idea to make the size of your XAP file as small as possible. Among other things this can help towards faster app loading times.

For an app I've been working on recently (more off than on) I wanted to get the start up time as short as possible. One of the ways I've been doing this has been to remove unnecessary external libraries and to optimise my use of the ones that I do need.
Now, while I'm a big fan of NuGet sometimes I don't want everything in a library. In this instance I created a version of the library I was using specifically restricted to the functionality that the app needs. I did this by getting the source of the library I was using (well actually libraries as there were 2), added the source project to my solution, excluded everything I didn't need from the project and then linked my app to that project rather than the downloaded copy of the compiled library.

Before I started this process my app was 599Kb. After the above process I got it down to 298Kb but there were still a couple of large assemblies in my XAP file that were bugging me.
These were both from Telerik (part of their WP7 control suite) but as I was just using a small part of the functionality available in those libraries this bugged me. As source wasn't available, I couldn't use the above technique. So I tweeted about it.

A few hours later I had a very interesting response.

It turns out Telerik already have a solution to this problem. It's called the Telerik Minifier and it provides 2 option:
1. You tell it which objects you specifically want and it gives you a set of libraries which contain just those. or
2. You point it at your XAP file and it removes everything it can to make it smaller. (I'm guessing it does the process in option 1 but just manages the updating of the XAP file for you.)

I opted for option 2 and then a minute or so later my XAP file was down to 209Kb in size.

And yes, my app now opens super fast. So fast in fact that I'm not including a Splash Screen!

Update
After some questions about what's actually happening, have a look at the image below.


On the left is the contents of the XAP before minification. On the right is the contents of the XAP after minification. The Telerik controls have been replaced with new versions which only contain the necessary code for running the app.

3 comments:

  1. Do you know of any similar (more generic) solutions that aren't tied to Telerik controls?

    ReplyDelete
  2. @Derek I've not tried it myself but you could have a look at the Mono.Cecil Linker (http://www.mono-project.com/Linker) which appears to create stripped down versions of assemblies based on code that is actually used.

    ReplyDelete
  3. Is this actual re-compiling the assemblies on the fly or is it just removing references from the project file that aren't used?

    If it's the second you can do this your self manually...

    ReplyDelete

I get a lot of comment spam :( - moderation may take a while.