Skip to main content

Xamarin Forms Binary Size




Apps on iOS are nice, Xcode is fine development environment and working in it is fun.
I have and app on iOS which I would like to port also to Android.
I thought about rewriting it in Xamarin.Forms. It turned out Xamarin apps (now as a part of Visual Studio) are also nice to write. I choose Xamarin.Forms to minimize code needed per platform.
App is available on github: https://github.com/jaropawlak/MyExpenses.Xamarin and has almost all functionality my original app had.
There is, however, one issue. BIG issue. It's the binary size.
While my original iOS app takes less than 2MB on device, Xamarin version is around 100MB. That is not acceptable for so simple app. I don't mind apps being big for a reason.
I would also accept that if it wouldn't make the app slow. But it makes it slow.
Have a look:

Cool ha? Native is so fast! So, what are the sizes exactly?



Native app is 1.1 MB







  Xamarin.Forms app is 101.2 MB




The question is: can I shrink the app to some reasonable size? First idea is to build without debugging symbols. Let's give it a try:









No luck here. What about changing linker settings to link only sdk libs? Well it did make app smaller and also made app crash on start. A lot of googling allowed to make it work (quick tip: LinkDescription.xml in iOS project). What’s next? 80megs is still a lot. Let’s go with more extreme settings.

I checked also checkbox to “Use the LLVM optimizing compiler”. This resulted in best size I could make with Xamarin.Forms app:
But… app stopped working again. Whatever linker settings would I change and whatever googled advice would I take, I wasn’t able to make my app small and working.
You might not see it while reading this post but experiments took few days of punishing change-build-deploy-see_it_crashes cycle. It could be either big, or not very small but still not working.
Setting Linker to LinkAll resulted in never ending cycle of updating LinkDescription and watching app fails again.
I tried to help linker by removing autofac and going with my own Poor-Man’s DI solution which didn’t help much either. EntityFrameworkCore also uses reflection much and linker was still overoptimizing. Finally I gave up with LinkAll.
My absolute smallest working file is this:

with those linker settings:


And with LinkDescription.xml file you can find in the repo. Half of original size. App is still slower than fully native option but is better than at the beginning.
Have a look.
If you are struggling to make your app smaller, my takeaways for shrinking app in Xamarin.Forms are:
  • Link Framework and SDK, then if it doesn’t work - all answers are in LinkDescription.Don’t bother with LinkAll.
  • Removing autofac didn’t make all things much better. Keep your code clean.
  • Don’t touch “incremental build” setting it will make app bigger :) 
  • If you write app that has to be fast (or small. Or both): think again if you must use Xamarin for that.
  • Last - shrinking app can be very punishing work. Try to have some fun! 

Bonus: app size with incremental build checked:

Have a nice time shinking your app!

Comments