How To Optimize and Reduce Android Apps Size in Xamarin
Read this article to know 10 things that you need to know while developing an android app using Xamarin to optimize process and reduce app size.
Join the DZone community and get the full member experience.
Join For FreeIn such times of aggressive competition, no business has left any stone unturned; therefore, to keep up with it, every feasible tactic has to be exercised in the market, and mobile applications have emerged victorious.
How to Enhance App Development Process for Android With Xaramin?
Fast Deployment
Fast Deployment comes under the debug mode in Xamarin; it lessens the deployment time as it positions the DLLs (assemblies) in the app directory. This further paces the process as no time is lost in generating the APK's or reinstalling but gets synchronized in the device.
Here’s how you can enable it using Visual Studio.
Go to the desired project.
Click on properties.
Choose Android Options.
Tick the Use Shared Runtime option under Packaging properties.
Use Fast Deployment (debug mode only) will be available, tick that.
Multi — Dex
Multi-Dex too comes under the debug mode in Xamarin. It is the process of splitting the application code to multiple-dex files with not more than 65KB of bytecode in each. The Startup time can be increased by 15% due to secondary dex file loading; therefore, Xamarin Linker can be used beforehand to reduce the application size.
Here’s how you can disable it using Visual Studio
- Go to the desired project.
- Click on properties.
- Choose Android Options.
De-check Enable Multi-Dex.
In case an error pops up, check back the option.
Release Builds
It is a configuration at the releasing stage of the application that aids in a smaller APK size and a swift Start Uptime.
Go to the desired project.
Click on properties.
Choose Android options.
Under configuration select Active (Release).
Select Xamarin.Android or ProGuard under Code Shrinker.
Simple Generational Garbage Collector
Simple Generational Garbage Collector is used by Xamarin.Android. It comprises of 2 generations and wide object space, along with two types of collections:
1. Minor Collections
Minor Collections are cheap and recurring; these are utilized to collect the freshly allocated objects along with the dead ones. Once some MB is full of allocated objects, Minor Collections are executed. These collect the Gen0 heap.
2. Major Collections
Major Collections are costly and rare; these are utilized to recover the dead objects. Once the memory has exhausted for the latest heap size (before resizing), these are executed by calling GC. Collect () or Collect (int) along with the argument GC.MaxGeneration.
To release the memory, the concurrent garbage collector must be enabled for Garbage Collection. This benefits in eliminating the huge breaks when the application has high memory piles to be collected.
Go to the desired project.
Click on properties.
Choose Android Options.
Under configuration select Active (Release).
Tick the Use the concurrent garbage collector under the Code Generation and Runtime option.
How to Reduce App Size Using Xamarin
Android Application Bundles
Android Application Bundles are not directly deployed to the device; instead, the resources and codes are compiled to be then uploaded. Next, Google Play provides it to the users employing Dynamic Delivery.
Here is how to enable AAB.
Go to the desired project.
Click on properties.
Choose Android options.
Under configuration select Active (Release).
Under Android Package Format select bundle.
Android Size Analyzer
Android Size Analyzer is an effortless technique for recognizing and executing numerous strategies to reduce the size of the application. It is accessible to Standalone JAR as well as the Android Studio Plugin
Specific Densities
Android is at the disposal of a diverse group of devices comprising different densities concerning the screen. In Android 4.4 (API level 19) and higher, all (mdpi, hdpi, ldpi, xhdpi, xxhdpi, xxxhdpi, and tvdpi) densities are supported hence there is no need to export the rasterized assets to every density.
Minimal Resources
In the process of the development of the application, to enhance user-friendliness and adaptability, resources are picked up from the external libraries. One must incorporate only those resources that the application surely needs. Mobile-friendly libraries are also a satisfactory alternative that can meet the strict performance needed for the application.
Android App Launch Checklist
After developing your Android app, Make sure to do these things before launching your app officially on any platform.
1. Specify Icon:
All Xamarin.Android must have the icon of the application-defined.
2. Shrinking:
To reduce the size of the final APK, one can use Xamarin. Android linker on managed code and ProGuard for Java bytecode, they determine and eliminate unused fields, classes, methods, and attributes.
3. Protection:
Protecting the application users by anti-debugging, anti-tampering, or using native compilation.
4. Versions:
To update the version information to keep the users informed of the versions they install.
5. Compilation:
It is needed to compile and process the code for verification.
6. Set Packaging:
It handles the formation of APKs, enhances the assets, and modularizes the package.
Opinions expressed by DZone contributors are their own.
Comments