Rajat Shelke
Rajat Shelke

Reputation: 39

Reduce apk size in xamarin.forms

I am consuming Web Api services in my app. Though it is app with small functionality such as registration and login then also the size of the release was firstly 55 MB then i searched a bit then i got it down to 22 MB. My settings are Linking- SDk assemblies only. i have tried every other thing such as Deselect Shared Runtime. Select Pro guard, Generate one package, all type of linking but did not get desired result. Am i missing something or the app size will be 22 MB. please suggest a solution

Upvotes: 1

Views: 7340

Answers (2)

Arvind Chourasiya
Arvind Chourasiya

Reputation: 17422

To reduce APK file size you need to use ProGuard with visual studio. It will remove all unused code, resources and shrink files, classes, variables names to very short name.

How to use ProGuard you see my another stackverflow question How to use ProgGuard with Xamarin forms.

Another option use linking Android project's properties, use Sdk and User assembly option. It some time remove code which we are actuary using so need to be careful.

Upvotes: 0

logictechy
logictechy

Reputation: 131

(Copied from : https://montemagno.com/how-to-keep-your-android-app-size-down)

  1. 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:

enter image description here

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:

enter image description here

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!).

  • Don’t Link: 40.7MB
  • Link SDK Assemblies Only: 18.7MB
  • Link All Assemblies: 13MB

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

2. 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!).

enter image description here

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:

enter image description here

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:

enter image description here

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):

  • armeabi-v7a: 10.2MB
  • armeabi: 10.3MB
  • x86: 10.4MB

Notes:

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

true

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.

Refer to these links:

  1. https://learn.microsoft.com/en-us/xamarin/android/deploy-test/building-apps/build-process
  2. https://montemagno.com/how-to-keep-your-android-app-size-down/
  3. https://forums.xamarin.com/discussion/113229/what-is-the-ideal-apk-size-from-xamarin-forms-and-how-to-reduce-it-without-force-close

Upvotes: 8

Related Questions