AndroidPub
Published in

AndroidPub

5 Easy Hacks To Reduce The Android APK Size

The Android Marketplace came into the picture in 2012. Since then, the average size of Android APK has increased gradually. Sometimes the reason for this is the increase in memory. However, it can severely affect the user experience. People are utilizing the latest technologies & innovation.

All these beautiful graphics and rich media content can have a negative impact on the UX. As per the research carried out by Google Play, for every 6 MB increase in APK size, there’s a 1% decrease in app installation. So, you can understand the importance of the Android APK size.

Taking this scenario into consideration, we have decided to write this blog. In this blog, we will provide you with some easy hack to reduce APK size. So, why wait for anything special to happen? Let’s get straight into the thick of things and analyze each trick one-by-one.

Easy Hacks To Reduce Android APK Size

1. Using Android App Bundles

As a developer, when you’re developing an app, you want to be compatible with every screen size. That’s where you have to write code for every screen. Now, when a person downloads the app, he/she receive a lot of code that is useless. It’s a catch 22 situation for sure.

Supporting multiple devices without increasing APK size has been a massive challenge. But, the Android developers are looking to change that scenario. For that purpose, they have introduced a new format — Android App Bundle (.aab). It will help you to reduce the APK size.

When you use .aab format for your app, Google Play Store will do the following things:

● An APK comprises code and resources.

● Multiple configuration APIs.

Android App Development Services is transforming app ideas into reality with the invention of .aab format. For building the app as a bundle, select Build > Generate Signed Bundle/APK from Android Studio toolbar. After that, select Android App Bundle checkbox.

Now, click on the Next button, which will redirect you to module dropdown. Here, you should choose the app as the base module. Complete the formalities and generate a .aab file.

Make sure you store the file in AndroidAppBundle/app/release directory. When you’re ready with .aab file, move to Google Play Console, and select Create Application. Here, you need to enter basic info about the app. After that, click on the Create button and then, save the details.

Now, on the left-hand side, select App Releases. Finalize the track of your app and then, choose Manage button. After that, click on Create Release.

Now, move to Google Play for App Signing, which is essential for Android App Bundle. Select Browse Files and then, select the .aab file that you’ve created.

After all this processing, click on the Save button to store your configuration. After uploading the .aab file in console, you will be able to see how much space you have saved in APK size.

2. Deliver On-Demand Features

Any app has some features which are not necessary. So, by separating those features, you will be able to reduce the APK size. The Dynamic Feature APKs comes into the picture. The user can download this APK as per their requirements. It will ease out all your problems.

For creating a dynamic feature module, select File > New > New Module from Android Studio toolbar. Now, select the Dynamic Feature Module and click on the Next button.

Now, you can open the Base application module dropdown. After that, you can select the base module. Name your module and proceed further. Now, select the Enable on-demand option.

Finally, give your module a public name and click on Finish button.

How To Enable The Access To The Dynamic Feature Module?

After creating the Dynamic Feature Module, you need to give it access to the users. For that purpose, open build.gradle file and add the following dependencies.

```

dependencies {

//Add the following//

implementation ‘com.google.android.play:core:1.3.6’

Now, open the Activity or Fragment where you want to incorporate this feature. After reaching that part, add the following piece of code.

private void loadDyanmicFeatureOne() {

//Create an instance of SplitInstallManager//

SplitInstallManager splitInstallManager =

SplitInstallManagerFactory.create(this);

//Create the request//

SplitInstallRequest request =

SplitInstallRequest

.newBuilder()

//Specify the module that you want to download//

.addModule(“dynamic-module”)

.build();

SplitInstallStateUpdatedListener listener = new SplitInstallStateUpdatedListener() {

@Override

public void onStateUpdate(SplitInstallSessionState splitInstallSessionState) {

if(splitInstallSessionState.sessionId() == mySessionId) {

switch (splitInstallSessionState.status()) {

case SplitInstallSessionStatus.INSTALLED:

Toast.makeText(MainActivity.this, “The dynamic module was downloaded”, Toast.LENGTH_SHORT).show();

break;

}

}

}

};

splitInstallManager.registerListener(listener);

//Submit the request via the asynchronous startInstall//

splitInstallManager.startInstall(request)

.addOnFailureListener(new OnFailureListener() {

@Override

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

public void onFailure(Exception e) {

//…then do something//

}

})

.addOnSuccessListener(new OnSuccessListener<Integer>() {

@Override

//If the module was downloaded successfully…//

public void onSuccess(Integer sessionId) {

//…then do something//

mySessionId = sessionId;

}

});

}

}

Now, the users will be able to access this functionality.

3. Offer An Instant Apps APK

The instant app APK is one of the best ways to reduce the APK size. It may not be the traditional APK that you get with every app. But, you can make a part of your app lightweight & instant. Users can load that part on-demand. For anyone who has limited data storage, it could work well.

In the latest version of Android Studio, creating an instant app APK has become easier. Now, one can create an instant app without URL mapping. It will reduce complexity. After you publish the URL less instant app, users can access it via the Try Now button.

For creating these type of apps, you will require:

● Android Studio 3.2 or higher

● Instant Apps Development SDK 1.3.0 or higher

● Latest Version Of Android SDK

Projects that make use of instant apps have a unique structure.

For supporting instant apps, you need to create the following modules.

● App Module: The standard installable app module.

● Base Module: It comprises code & resources of all project module.

● Feature Module: It contains code & resources needed for a standalone app.

● Instantapp Module: A container that takes module & converts them in instant app APK.

4. Publish Instant Apps-Enabled App Bundle

In Android Studio 3.3 and higher, there is a unique facility. Now, you can combine the instant app with the app bundle. It will create the .aab file that comprises an instant and installable app.

For this purpose, open the Instantapp module. Now, in this module, find out AndroidManifest.xml file and add the following code.

<manifest

xmlns:android=”http://schemas.android.com/apk/res/android"

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

//Set the targetSandboxVersion to 2//

android:targetSandboxVersion=”2">

//Add the following//

<dist:module dist:instant=”true” />

</manifest>

Open the build.gradle file and make sure that your instant variant has a smaller versioncode than the installable variant.

android {

flavorDimensions “flavorDimension”

productFlavors {

instant {

versionCode 1

}

installed {

versionCode 2

}

}

}

Now, Select Build > Generate Signed Bundle / APK and build your app bundle.

5. Remove All The Unused Resources

Removing unused or unnecessary resources such as methods, classes, fields & attributes will help you to reduce the APK size. Various tools that can help you in this regard.

Try R8 Code Shrinker

Code Shrinking is one of the most innovative ways to reduce the APK size. Android Studio 3.3 is planning to bring this tool, which will be helpful to all the mobile app developers out there.

However, for using that tool, you need to know the basics of mobile app development. That’s where the role of Complete Guide Of Mobile App Development comes into the picture.

For trying R8, you should use Android Studio 3.3 or higher.

After that, add the following code to the build.gradle file.

```

android.enableR8=true

Code Shrinking With ProGuard

Before R8 burst onto the scene, ProGuard was the prime choice for code shrinking. For enabling ProGuard, add the following code to the build.gradle file.

buildTypes {

release {

minifyEnabled true

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

}

}

}

Enable Resource Shrinking

After enabling code shrinking, you can make use of resource shrinking. It will discover and eliminate any redundant resources.

buildTypes {

release {

//Add the following//

shrinkResources true

minifyEnabled true

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

}

}

}

Improve The Code With Android Lint

Lint is a scanning tool. It helps you to discover resources which have not been referred anywhere. However, Lint won’t remove that resource. It will only give you the report.

For using Lint, select Analyze > Inspect Code from Android Studio toolbar. If Lint detects any unreferenced resources, it will display them in Inspection Window.

Conclusion:

Nowadays, there is a lot of demand for Android Apps in the market. However, the biggest challenge for the developers is to reduce the APK size without affecting the app performance.

Taking this scenario into consideration, here we have provided you with five easy hacks which will help you to reduce the Android APK size with the utmost ease.

If you’ve any questions or suggestions related to this blog, then feel free to ask them in our comment section. We’ll try to respond to each of your queries. Thank You.!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store