Xamarin Size - apk

So, after I started developing the application using Xamarin.Forms, when I tried to build the release build today, I was shocked to see that the application size is ~ 25 MB - ~ 31 MB (after the SDK Only Linking, ProGuard, Creation apk for each abi). My resource folder is in KB only. Apk was actually only 5 MB in debug mode and was so happy to see it. Later, I realized that this was due to the “Use Collaboration” option, which we should not use in Release mode.

Then I tried to create an empty Xamarin.Android application, but building releases using the Linking SDK and User Assemblies, ProGuard, APK for each abi is still ~ 8MB to ~ 13MB.

In the link below, the minimum size is 2.9 MB using the Hello World application, however I cannot create that size. https://developer.xamarin.com/guides/android/advanced_topics/application_package_sizes/

For my application and the empty application that I created, the necessary dlls seem high (for example, mscorlib.dll is 2.2 MB in size, etc., where, as the link says, after the link, it will be 1 MB) Here's what I see as mine build after extract apk enter image description here

In a recent Microsoft technology conference, I learned that the "9 News" application (link below) was built using xamarin, and the creators were present on the scene. However, I was surprised at this app size. It is only 5.85 MB. I'm not sure how this is achieved?

Can anyone help me with this?

https://play.google.com/store/apps/details?id=nineNewsAlerts.nine.com

I am also interested to know that Microsoft is doing something to improve application package sizes? Or will it be allowed if they bring the .NET core to xamarin?

+11
xamarin xamarin.android xamarin.forms
source share
3 answers

The APK files for Xamarin Android are larger than usual because they include Xamarin libraries that translate C # code to Java code to run on Android OS.

Using the following options, you can reduce the size of the APK:

  • Configuration : Active (Release) .
  • Platform : Active (any processor) . This is necessary to create an APK for the Google Play store.
  • Use collaborative execution : false . If you set to true, the APK will use Mono Runtime to execute. Mono Runtime is automatically installed when debugging via USB, but not in the Release APK. If Mono Runtime is not installed on the device, and this parameter is set to true in the Release APK, the application will crash.
  • Generate one package (.apk) for the selected ABI : false . Build an APK for as many platforms as possible for compatibility reasons.
  • Enable Multi-Dex : true , but you can set it to false if your application is not very complex (that is, it has less than 65536 variables or methods, see here ).
  • Enable developer tools (debugging and profiling) : false to release the APK.
  • Binding : SDK and custom assemblies . This will force the Xamarin compiler to remove all unused classes from the SDK and your code, reducing the size of the APK.
  • Supported architectures : Choose all for compatibility reasons.

Here is an example of a print screen:

enter image description here

+11
source share

Here are the statistics for my Xamarin.Forms app size from the Google Play console. You can see how much it declined as soon as I took the topic seriously and moved away from the default Xamarin settings:

enter image description here

In addition to the measures that are usually found when searching for a topic (linking, tracking your own resources, APK for ABI, etc.), here are a few of my own conclusions and comments:

  • Enabling TLS managed subsystems of ~ 1 MB in size - by default, Native TLS 1.2 is selected under Project Settings → Android Settings → Advanced → SSL / TLS Implementation. Managed TLS - only version 1.1, maybe this is someone's problem
  • Upgrading to Xamarin.Forms 4.1 gives a reduction of ~ 1 MB (compared to 3.x and 4.0) - they removed some unused but still mentioned Android compatibility libraries
  • Enabling the aapt2 wrapper provides a reduction of ~ 1 MB only for App Compat resources (those referenced and included by Xamarin.Forms). Could give more if you have a lot of your own resources. With aapt2, you can further reduce resources by using the following additional arguments in the Android.csproj file:

    <PropertyGroup> <AndroidUseAapt2>true</AndroidUseAapt2> <AndroidAapt2LinkExtraArgs>--no-version-vectors -c en-rUS</AndroidAapt2LinkExtraArgs> </PropertyGroup> 
  • Enabling D8 and R8 gives another ~ 1 MB for a small Xamarin.Forms application (Project Settings → Android Settings or .csproj)

     <PropertyGroup> <AndroidDexTool>d8</AndroidDexTool> <AndroidLinkTool>r8</AndroidLinkTool> </PropertyGroup> 
  • SkiaSharp 1.60.3 saves ~ 2 MB per architecture compared to later versions (e.g. 1.68.0). It’s worth looking at the dependencies and their versions that inflate your APK.

  • Disabling AoT (if it was enabled earlier by default) can easily reduce the size of your application by half. Turning on ahead of the clock complication could be a deliberate step to improve performance when launching your application. There is an obvious compromise: either you use AoT, and you have a huge application size, or you don’t have it, and you keep the minimum size when the application starts slowly. One of the things to consider when choosing a compromise ... Most ASO articles (as well as the Google Play console) mention how important it is to have a small application size to increase conversion rates. Obviously, when viewed on Google Play, the download size of the application is displayed, not the time the application started.

  • Configure AoT to minimize its footprint, add the following information to .csproj to configure the release:

     <PropertyGroup> <AotAssemblies>true</AotAssemblies> <AndroidAotAdditionalArguments>no-write-symbols,nodebug</AndroidAotAdditionalArguments> </PropertyGroup> 
  • Performing partial AoT . By default, AoT scans all builds allowed by the project and adds 30 + .so files to the / lib directory of your APK. There are many compatible applications, BCL, and system libraries. You may want to have control over which assemblies to pass through AoT and which to ignore in order to find a good place between performance and application size. Well, there is no such thing as a standard function (at least I have not found). You can play around with the Xamarin build process by making changes to Xamarin.Android.Common.targets - you can find some details about this process here . I found my golden ratio using AoT with just three libraries (1 my own XF user interface library and 2 XF libraries):

    <_resolveduserassemblies2 include = "Tmp / android / assets / Xamarin.Forms.Core.dll; Tmp / android / assets / Saplin.CPDT.UICore.dll; Tmp / android / assets / Xamarin.Forms.Platform.Android.dll;" />

PS: Enabling bindings (SDKs and custom assemblies) is a 90% chance that you will find that your application is broken if you use XAML and bindings extensively. Add your own libraries to the linker exceptions — the XF class library that houses your user interface, any class libraries that you created that might use reflection.

PPS: thanks to my efforts to minimize, partial AoT and pay using the asynchronous / deferred loading of the Xamarin.Forms user interface ( here and here ) I achieved a significant reduction in the size of the application (see the figure above) with significant improvements in the application launch time (according to Nokia N1 via ADB):

  • No AOT, synchronization interface (ms): 3613, 3586, 3576
  • Partial AOT, asynchronous interface (ms): 2202, 2255, 2258
0
source

I understand your problem when creating a mobile application, you always wanted to create a small apk file that can be easily downloaded and used by users. To do this, try to reduce code duplication in your project and reduce the entire image size. Use the following steps, this will help you reduce apk size. Right-click on the properties of the droid. Open Android Settings → Click on the package, then check the creation of one package (.apk) for the selected ABI. Check out the Multi-Dex and proguard options. Now go to the Linker option, select the SDK assemblies in the Binding drop-down list only. Now go to the Advance tab. Check all the options in the Supported Architecture section. Save all the settings and save the project in release mode and create your own project, after which you can create a reduced apk file size. Use the following link for more information.

http://motzcod.es/post/112072508362/how-to-keep-your-android-app-size-down

-one
source

All Articles