8 Ways To Lessen Your Android App Apk Length

Since the Android Marketplace launched in March 2012, the dimensions of the average Android app has quintupled.

Some of this boom makes experience: the reminiscence available to the typical Android tool has increased, so why now not use this greater reminiscence to deliver a higher person enjoy?

However, there is a factor wherein all of these excessive-definition pics, revolutionary functions and rich, multimedia content have a terrible impact at the consumer enjoy—no-one desires an app that takes all the time to down load and then takes a bit out in their device’s memory! 

There’s even proof to signify that there’s an inverse correlation between the dimensions of your APK and the wide variety of those who download your app. According to a recent have a look at, for each 6MB boom in APK size, you can anticipate a 1% drop within the wide variety of those who set up your app after visiting its Google Play web page. 

Everything you could do to lessen the scale of your APK will inspire greater users to download your app, so in this newsletter I’ll show you a way to create a trimmer, slimmer APK and ensure your app finishes up on as many gadgets as viable. 

Android App Bundles: Support More Devices, While Reducing APK Size

Whenever you increase an Android app, you’ll want to ensure that app is compatible with as many special devices as viable, through providing code and assets which are optimized for different screen densities, CPU architectures, languages, and lots of different elements. Unfortunately, this also means that each unmarried man or woman who downloads your app will get hold of a ton of code and assets that they’re never going to apply.

Supporting as many devices as viable with out dramatically growing the scale of your APK has lengthy been a trouble for Android builders. However, the Android crew are aiming to exchange all this, with a brand new publishing layout that’s designed that will help you assist the full range of Android devices, at the same time as lowering the size of your APK. According to the reliable Android Developers weblog, with the aid of publishing your app on this new Android App Bundle (.aab) format, you need to be capable of lessen the size of your APK by means of a median of 35%. 

Once you’ve uploaded your app as an .aab document, Google Play will use this unmarried report to generate:

A base APK containing all of the code and sources required to supply your app’s base functionality. Everyone who downloads your app will get hold of the identical base APK. 

Multiple configuration APKs, that are tailored to distinct device configurations. Every time someone downloads your app, they’ll get hold of a configuration APK that’s tailor-made to their specific tool and carries only the code and resources they really want. 

To build your app as a signed Android App Bundle, first pick Build > Generate Signed Bundle/APK from the Android Studio toolbar. Then pick the Android App Bundle checkbox, and then click on Next. Now, open the module dropdown, and select app as your base module.

Follow the onscreen commands to complete the build method. Android Studio will now generate an .aab record and store it in your laptop’s AndroidAppBundle/app/launch directory.

When you’re ready to upload your .aab file to Google Play, just head over to the Google Play Console and choose Create Application.

You’ll be asked to go into some fundamental data about your app after which click on Create, followed through Save. In the Console’s left-hand menu, select App releases. Decide which music you want to upload this app to, and then pick its accompanying Manage button and click Create launch.

When brought on, join in App Signing by using Google Play, that’s mandatory for the use of the App Bundle layout. Select Browse files after which choose the .aab record which you need to add.

Finally, click on Save to shop your configuration.

Once you’ve uploaded your app to the Console, you could see how much space you’ve saved by means of the use of the App Bundle layout. To accomplish that, pick out App releases from the left-hand menu. Find the tune wherein you uploaded your Bundle, after which pick Edit launch then click to extend the Android App Bundle segment.

Select Explore App Bundle, and you’ll see a difficult estimate of the space you’ve stored, expressed as a percent. 

Deliver Features on Demand, With Dynamic Feature Modules 

It’s commonplace for a undertaking to have one or extra capabilities that aren’t vital to supply that application’s center capability. You can frequently lessen the size of your APK by using isolating these capabilities into elective dynamic feature APKs, which the person can then down load on call for, as and when required.

To create a dynamic feature module, choose File > New > New Module… from the Android Studio toolbar. Select Dynamic Feature Module, observed by way of Next. 

From here, you can open the Base application module dropdown and pick your base module. Give your module a call, and then click Next. Select the Enable on-call for checkbox. If your undertaking supports Android 4.four or in advance, then you definitely’ll additionally want to permit Fusing, as this will make your dynamic function module to be had to those gadgets as a multi-APK. 

Finally, provide your module a public name, a good way to be seen to the end user. Click Finish. 

Enabling Access to the Dynamic Feature Module

Once you’ve created a dynamic characteristic module, you’ll want to give customers a way to request it at runtime. Open your module-level build.gradle file, and upload the Google Play Core library as a project dependency: 

dependencies

//Add the following//

implementation ‘com.google.android.play:middle:1.three.6’

Open the Activity or Fragment in which you want to load your dynamic feature module, and then upload the following: 

personal void loadDyanmicFeatureOne()

//Create an instance of SplitInstallManager//

SplitInstallManager splitInstallManager =

SplitInstallManagerFactory.create(this);

//Create the request//

SplitInstallRequest request =

SplitInstallRequest

.newBuilder()

//Specify the module which you want to down load//

.addModule(“dynamic-module”)

.construct();

SplitInstallStateUpdatedListener listener = new SplitInstallStateUpdatedListener()

@Override

public void onStateUpdate(SplitInstallSessionState splitInstallSessionState)

if(splitInstallSessionState.sessionId() == mySessionId)

switch (splitInstallSessionState.popularity())

case SplitInstallSessionStatus.INSTALLED:

Toast.makeText(MainActivity.this, “The dynamic module changed into downloaded”, Toast.LENGTH_SHORT).display();

spoil;

;

splitInstallManager.registerListener(listener);

//Submit the request thru the asynchronous startInstall//

splitInstallManager.startInstall(request)

.addOnFailureListener(new OnFailureListener()

@Override

//If the download fails with an exception….//

public void onFailure(Exception e)

//…then do some thing//

)

.addOnSuccessListener(new OnSuccessListener()

@Override

//If the module turned into downloaded correctly…//

public void onSuccess(Integer sessionId)

//…then do some thing//

mySessionId = sessionId;

);

You can now add code and resources in your dynamic feature module, and users might be able to request the module at runtime.

Try Before You Buy: Offer an Instant Apps APK 

It might not be an APK inside the conventional, installable sense, but you can offer part of your software as a light-weight, standalone Instant Apps APK that customers can then load on call for. For everybody who’s suffering with extraordinarily restrained garage or a restrictive information plan, Instant Apps may be the only possible way for them to experience your application. 

In our three-part Instant Apps series, we created a multi-characteristic Instant Apps APK that turned into mapped to a unique URL. URL mapping makes your Instant App on hand from any region on the web that helps hyperlinks, consisting of boards, tweets, Facebook posts, and blogs. However, URL mapping is also a multi-step manner that calls for you to very own a web area wherein you could host your undertaking’s assetlinks.json document. 

In the cutting-edge versions of Android Studio, developing an Instant Apps APK simply were given less complicated, because it’s now possible to create an Instant App that isn’t mapped to any URL, removing all of the complexity associated with URL mapping.

Once you’ve posted your URL-less Instant App, users can be capable of get admission to it through a Try Now button in your app’s Google Play list. 

To create those kind of “URL-much less” Instant Apps, you’ll want:

Android Studio 3.2 or better.

Instant Apps Development SDK 1.three.zero or better.

The modern versions of the Android SDK Build and Platform equipment. 

Projects that guide Instant Apps have a completely unique shape. If you practice modularization as part of your general app development system, then you can most effective need to make a few tweaks to your venture, but in case your task isn’t modularized, then be organized to spend a while restructuring it. 

To guide Instant Apps, you’ll want to create the following modules:

App module. This is the “preferred” installable app module.

Base module. This includes the code and sources that’ll be used throughout all your mission’s modules, along with your app’s launcher icons. 

Feature module. This contains the code and assets which can be required to supply a stand-on my own function.

Instantapp module. This is a container that’ll sooner or later take every of your mission’s characteristic modules, and remodel them into Instant App APKs. 

For a more specific study this specific challenge shape, check out: What Are Android Instant Apps?

If you’re beginning a brand new assignment, then you’re in good fortune: you could create a blank task that already has the Instant Apps structure. Simply create a new assignment as everyday, however while triggered, select This mission will help immediate apps.

You can then upload code and sources to every module, as regular. 

To test your URL-less Instant App, you’ll want a well suited device, which in step with the legit Android doctors is “maximum” bodily gadgets strolling Android five.1 or better. If you’re the use of the emulator, then you definately’ll need an AVD (Android Virtual Device) that’s using Android 8.1 or later, x86 structure, and includes the Google APIs. You’ll additionally need to be logged in to a legitimate Google account for your take a look at tool or AVD.

To positioned your Instant App to the check, pick out Run > Run… from the Android Studio toolbar and then pick out Edit Configurations…

In the left-hand menu, pick out instantapp. Since we’re trying out a URL-much less Instant App, locate the URL field and delete all the text internal it. Now pick out Run. If the tool activates you to opt into the Instant App program, then faucet Yes, I’m in. 

Your Instant App ought to now appear onscreen, without a set up and no URL required!

When it’s time to release your app, you can both whole the signing method two times which will create separate installable and Instant App APKs or you could build your task as an App Bundle. 

Publish an Instant Apps-Enabled App Bundle 

In the Android Studio 3.3 beta 1 and higher, you may combine Instant Apps with App Bundles, to create a unmarried .aab file that features each your installable and Instant App. 

To set your app as much as publish an .aab report with an Instant App included, open your venture’s instantapp module. 

In this module’s AndroidManifest.xml file, add the subsequent:

xmlns:android=”https://schemas.android.com/apk/res/android”

xmlns:dist=”http://schemas.android.com/apk/distribution”

//Set the targetSandboxVersion to two//

android:targetSandboxVersion=”2″>

//Add the subsequent//

Open your app module’s construct.gradle record, and ensure your on the spot variation has a smaller versionCode than its installable counterpart:

android

flavorDimensions “flavorDimension”

productFlavors

instantaneous

versionCode 1

installed

versionCode 2

Select Build > Generate Signed Bundle / APK from the Android Studio menu bar, and then build your App Bundle as ordinary, ensuring to sign it with a release key. 

Remove All Unused Methods, Classes, Fields, Attributes, and Resources

It’s clean for unused code and resources to discover their way into your project, especially whilst you’re the use of libraries. Since they upload zero fee for your app, you need to constantly song down and cast off needless assets, and there are numerous equipment that allow you to:

1. Feeling Adventurous? Try the New R8 Code Shrinker

Code shrinking allow you to lessen the scale of your APK by using deleting unused code and assets. 

Android Studio 3.3 beta 1 capabilities a preview of the approaching R8 code shrinking device, which also performs desugaring and dexing and, according to Google, shrinks code faster than ProGuard.

To attempt R8 for your self, make certain you’re walking Android Studio 3.3 beta 1 or higher, and then add the following in your gradle.homes file: 

android.enableR8=true

2. Shrink Your Code With ProGuard 

At the time of writing, R8 become still in preview, so until it’s strong you may choose to use ProGuard to dispose of unused lessons, strategies, fields and attributes from your code.

To allow ProGuard, add the following to your module-degree build.gradle document:

buildTypes

launch

minifyEnabled actual

proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’

Just be aware that ProGuard may additionally dispose of code that your software without a doubt desires, so that you must usually take a look at your project with ProGuard enabled, earlier than publishing it. 

three. Enable Resource Shrinking 

Once you’ve enabled code shrinking, you can use useful resource shrinking to identity and take away pointless sources: 

buildTypes

launch

//Add the following//

shrinkResources actual

minifyEnabled real

proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-regulations.pro’

4. Improve Your Code With Android Lint 

Lint is a static scanning tool which can pick out sources that aren’t referenced anywhere on your code. However, Lint doesn’t truly do away with these resources, so you’ll need to test its report and get rid of the recognized resources manually.

To run Lint, pick out Analyze > Inspect Code… from the Android Studio toolbar. If Lint detects any unreferenced resources, then it’ll list them in its Inspection Results window.

Convert Your PNGs, JPEGs and BMPs Into WebP

If your challenge includes a number of drawables, then compressing them can drastically reduce the size of your APK.

If your mission’s minSdkVersion is 18 or better, then you could compress your PNGs, JPEGs and BMPs through converting them to WebP format.

First, manage-click the drawable that you need to convert and choose Convert to WebP… Choose among lossy, or lossless encoding. Select Skip photos while the encoded result is larger than the original and click on OK to convert the photograph.

Examine Your APK Using Android Studio’s APK Analyzer

Sometimes, you could not be totally sure what’s brought about your APK to grow out of manage.

You can discover the largest contributors closer to the dimensions of your APK by using Android Studio’s new APK Analyzer. This device lets you discover absolutely the and relative size of various categories of documents, or even the size of individual documents, so that you’ll recognize precisely where you ought to be focusing your efforts.

To analyze an APK, simply choose Build > Analyze APK from Android Studio’s menu bar.

In the subsequent window, choose the APK in query. The APK Analyzer will open in a new panel. 

For every file and folder, the APK Analyzer shows:

Raw file size. This is the unzipped length of this object at the disk.

Download length. This is the anticipated compressed length of the item, as it would be brought with the aid of Google Play.

% of total down load length. The percent of the APK’s overall download size that this object represents. 

This isn’t the best function the APK Analyzer has to provide.

Imagine you’ve efficiently slashed the scale of your APK and released a light-weight application that amasses sparkling five-big name opinions on Google Play—that doesn’t imply you could prevent traumatic about APK length! At some point, you’ll want to launch an replace, because of this APK length will over again become a main situation. 

While it’s always an awesome concept to reveal how your APK evolves from one release to the following, if there’s ever a first-rate boom in APK length then you’ll absolutely want to take a closer have a look at what’s occurring. 

You can use the APK Analyzer to highlight all the variations between your contemporary APK and your previous APK, which can help you 0 in on regions and belongings that you can optimize or even do away with completely.

To evaluate APKs, first choose Build > Analyze APK from Android Studio’s menu bar.

Then pick the most current model of your APK. In the top-proper corner of the APK Analyzer, choose Compare with preceding APK… Next, choose the preceding version of your APK. A popup should seem, highlighting all the variations between your modern-day APK and your previous APK.

Don’t Forget Android Go!

Emerging markets constitute a number of the fastest growing smartphone audiences within the global, and the rise of brands like Vivo and Oppo has proven that budget devices have the capacity to dominate this unexpectedly-increasing marketplace. 

To help Android appeal to this completely new and developing audience, Google have released Android Go, a model of Android with performance and storage upgrades designed to provide a better revel in on price range-friendly gadgets, inclusive of gadgets that have less than 1GB of RAM. 

Android Go customers have get admission to to the complete Google Play store, but Google highlights apps that are optimized for Android Go, including programs that take up much less than 40MB at the person’s tool.

By following the recommendation in this newsletter, you ought to have the ability to significantly reduce the size of your APK for each person who uses it. However, in case you need to make a few additional reductions in particular for Android Go users, then you may use Android’s multi-APK characteristic to publish an APK that’s optimized for the Android Go platform. 

To create a devoted Android Go app, you’ll need to create an APK that has the same package name and signing certificate as your “everyday” app, however has a completely unique version code. 

When creating your Android Go APK, you’ll additionally want to add the following on your Manifest: 

Conclusion 

In this article, I protected all of the main approaches that you can reduce the size of your APK to create a sleeker, greater slimline utility to be able to download faster and take up much less area on the person’s device. 

Leave a Reply

Your email address will not be published. Required fields are marked *