Creating Multiple APK Files of Android App

New Android devices are continuously being introduced with different CPU architectures and different screen densities. This results in the increase in apk size, as it contains resources for every device. Why users need to download apk containing resources of other devices that are useless for them and making their phone storage full. So, developers need to create multiple or splitting the apk file which is smaller in size containing only the essential resources, fitting all segment devices available. Developers can generate the multiple numbers of apks for a single application, by splitting a single apk into multiple apks, where each apk is specific for specific devices. Multiple apks generated contain resources for different screen densities device specifically and different CPU architecture. The main goal of splitting apk is to reduce apk size that can’t be achieved through reduction of app resources of single apk file.

It is true that these days’ devices are coming with larger spaces but at the same time there are too many devices available are having very low memory. So, the targeted devices are very large. Hence, developers can’t lose some group of devices or reduce resources size used in the application. So, developers need to split their apk file into multiple files.

For the generation of screen density specific apk files, developers need to generate files in all the screen densities available. Generally, there is six set of densities-ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi.

Generating one APK File for Multiple Screen Densities

Paste the following block of code in your Gradle file and click build apk.

android {
…….
…….
splits {
density {
enable true
exclude “mdpi”, “xxxhdpi”
compatibleScreens ‘small’, ‘normal’, ‘large’, ‘xlarge’
}
}
}

This code will generate apk for all set of densities excluding mdpi and xxxhdpi with universal app. Universal apk is compatible with every available devices and upcoming devices. Let’s take a look at different keywords of this code:
1. Exclude: By this keyword, developers add a list of screen densities for which they do not want their gradle to make apk.
2. Include: With this, developers add a list of densities that they want their gradle to make apk for. To use this keyword, reset must be added to the code then use include.
3. Reset: It is used to clear all the default list of screen densities, use only if include is used.
4. Density: This is used to build multiple apk based on screen density
5. CompatibleScreen: This is to specify a list of compatible screen sizes, if developers are building apk for all devices, they use compatibleScreens ‘small’, ‘normal’, ‘large’, ‘xlarge’ or all at once.

Generating Multiple apks for Specific Types of abi

We can generate a specific apk file for a set of Abi armeabi, armeabi-v7a, arm64-v8a, x86, x86_64, mips, mips64.
Write the following block of code in gradle file and click on build apk.

android {

splits {
abi {
enable true
reset()
include “x86”, “mips”
universalApk false

}
}
}

This code will generate apk for x86 and MIPS. Universal Apk is set false by default for abi so developers need to set it true.

Generating Combined apk for Screen Density and CPU Architecture

Copy the following code in the app level gradle file and click on build apk.
android {
…..

splits {
density {
enable true
reset()
include “mdpi”, “xhdpi”
}
abi {
enable true
reset()
include “mips64”, “mips”
}
}
}
This will generate four combined apk for mdpi-mips64, mdpi-mips, xhdpi-mips64 and xhdpi-mips.

Rules for Uploading Multiple apk Files on Google Play Store:

1. Should contain a distinct version code for each apk.
2. Should contain identical package name for each apk.

How to create apk with Distinct Version Code?

Write the following code in the app level gradle file and click on build apk to generate apk with different version code.
ext.densityCodes = [‘mdpi’: 1, ‘hdpi’: 2, ‘xhdpi’: 3]

import com.android.build.OutputFile
android {
…..

android.applicationVariants.all { variant ->

variant.outputs.each { output ->

def baseDensityVersionCode =

project.ext.densityCodes.get(output.getFilter(OutputFile.DENSITY))

if (baseDensityVersionCode != null) {
output.versionCodeOverride =
baseDensityVersionCode * 1000 + variant.versionCode
}
}}}

You are now ready to upload multiple apks of your application. Hopefully after reading this article you’ll be able to reduce your apk size by splitting into multiple files.

We will be happy to hear your thoughts

Leave a reply

AndroidXDA