James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Live, Love, Bike, and Code

Share


Tags


How To Keep Your Android App Size Down

On my recent visit out to Tonto National Forest to visit one of my best friends, Luke Karrys, I started to realize the importance of trying to keep your app size down when shipping Android apps to Google Play. Working from the middle of the forest is relaxing, productive, and just plain fun, but there is one huge issue, internet and cell reception barely exists in the the middle of no where. 

When I woke up one morning and saw that I had 10 app updates ready to be installed from Google Play I was pretty excited to see what was new and fixed. However, dropping in an out of 3G service combined with an internet connection that will become crippled for everyone else if you download any files resulted in less than stellar results, and had me completely cancel all of my app updates. It had me thinking, why does the Facebook app really need to be 30MB and are all of my apps oversized as well?

There are a few ways you can take to make your app smaller when developing with Xamarin. Additionally, any Android developer can do one simple thing when deploying to Google Play to reduce their app size.

Linking Your Libraries

Xamarin applications use a “linker” in order to reduce your app size. You can browse through the documentation and find out how this works, but to simplify things, it uses static analysis to your app to remove assemblies and types that are not used in your app to bring down your app size. This is for any Xamarin app, so you should also try this out in your iOS app because it can reduce your app size in a default “Hello, World” application from 16MB down to 2.9MB! There are three settings that you can supply from the projects settings:

Don’t Link will do just that, it won’t link anything and you will be left with All of Mono, mscorlib, Xamarin.Android, and a bunch of other stuff:

Link SDK assemblies only is your safest bet and should be your default as it will only attempt to strip things out of Xamarin.Android and any of your third party libraries will not be touched. However, to really bring down your app size you should try out Link All Assemblies, as it will investigate everything and bring down your app size. Be sure to FULLY test your app as it is possible that the linker may be agressive and strip out something you need, and if that is the case you can actually use a [Android.Runtime.Preserve] flag or set a linkskip in your MSBuild to ensure that not all of your libraries get linked. 

So employing this practice with my Bike Now app, which uses Json.NET, Android Support v4, v7, Google Play Services, and Xamarin.Insights, we can compare and contrast the app size when we build our app to support all three ABIs (we will talk about this next!).

As you can see linking correctly can make a huge impact, but we can do even better! 

Splitting your APKs

On Android, there are ABIs (Application Binary Interfaces) that you can support when you ship your application. The most used will be the armeabi-v7a, however there are still tons of devices that support and run the old armeabi ABI and even x86 devices as well. So to ensure your app is reaching the most users you most likely have come into the project settings and selected every single ABI (I know I do!).

However, for every ABI that you select you are actually bundling a separate libmonodroid and sgen with your app. Don’t believe me then rename your .apk to .zip and take a look in the lib folder:

This of course makes sense as you would need a different version of monodroid and sgen that supports that ABI. The issue is that you now have all of these libraries bundle into a single APK and your users will be downloading all of them! The solution for any Android developer (even Java devs) is to simply split up your APKs and upload all of them to Google Play! This way you have a smaller app size across all three APKs. You can do this now with a simple check in your project options:

Now, instead of just a single APK to upload I have three with different and smaller sizes (note it will take longer to create your packages):

Notes: 

You may need to close XS after selecting check box and ensure this flag is set in your csproj:

<AndroidCreatePackagePerAbi>true</AndroidCreatePackagePerAbi> 

Additionally, your new APKs will be in your /bin/Release folder and will be marked with Signed in their file name.

Keep your users happy and keep down that app size with these quick tips. 

Copyright © James Montemagno 2015 All rights reserved. Privacy Policy

View Comments