How to create multiple apk files for android application

how-to-create-multiple-apk-files-for-android-application

Too many android devices and much more to come and among them, various different CPU architectures and different screen densities devices are there. Making single apk with all device compatible, increases your apk size, as it has resources for every device. Why to make our users, to download apk containing device specific useless resources who are having lower memory devices. Do we really need single larger apk with device specific useless resources or we need apk with smaller size only containing the essential resources, fitting all segment devices available. If you are looking for some sort of solution to reduce apk size by not loosing any useful resources it is time to implement apk split for building multiple apk.

Time to say hello to multiple apk.

What is multiple apks?

Generating the multiple number of apks for a single application, by splitting a single apk into multiple apks, where each apk is specific for some sort of devices. Multiple apk is mainly used to generate specific apks for different screen densities and different CPU architecture.

What is Apk Split?

Simply, Apk split is achieving lesser size apks, by splitting a single apk into multiple apks, that can be screen density device specific, abi device specific or combined screen density-abi device specific.

The significant goal of apk split is to reduce the size of apk.

What actually goes behind the scene? How the apk size reduces?

It does not compress your app size by compressing your resources used within the app. It actually makes new apks for the specified type of devices by including only the device compatible resources, and by excluding every other resources that is not compatible or unused for specific device type. Like if you are creating two apk one for hdpi and another for xdpi, then in the first apk, there will be no xdpi resources and similarly in the second apk there will be no hdpi resources.

Why Apk split?

Nowadays devices are coming with larger space then why i need to worry about apk size? It is true that devices nowadays are coming with larger spaces but at the same time there are too many devices available are having very low memory, and you are targeting that group of devices too, to access your app.Here, you can do one thing is reduce your resources size to get fit into every device or leave the apk as it is and lose some group of devices, but you actually do not want this.

You can not make everyone happy at the same time, or you can?

If at the same time you do not want to lose any group of users, and do not even want to compromise with the qualities of the resources used in the app, it is time, you need to introduce your app with apk split.

To generate multiple apks for specific types of screen density devices

In general there is six set of densities-ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi for which you can generate screen density specific apk.

Write these block of code in your app level gradle file and click build apk.

android {
.......
.......
    splits {
        density {
            enable true
            exclude "mdpi", "xxxhdpi"
            compatibleScreens 'small', 'normal', 'large', 'xlarge'
        }
    }
}

This block of code will generate apk for all set of densities excluding mdpi and xxxhdpi with a universal apk.

Lets understand each one of them.

density :- To build multiple apk based on screen density

exclude :- In this, add list of screen densities for which you do not want your gradle to make apk.

reset :- To clear all the default list of screen densities, use only if you are using include.

include :- To use this must add reset to your code then use include, in this, add list of densities that you want your gradle to make apk for.

compatibleScreen :- It is to specify list of compatible screen sizes, if you are building apk for all devices use :-

compatibleScreens ‘small’, ‘normal’, ‘large’, ‘xlarge’

And, if you want to restrict any of screen size among them, you can eliminate from the comma seperated list.

In this, gradle will also generate a universal apk.

Universal apk :- This apk is compatible with every available devices and upcoming devices.

To generate multiple apks for specific types of abi

Set of abi armeabi, armeabi-v7a, arm64-v8a, x86, x86_64, mips, mips64 for which we can generate the abi specific apk.

Write these block of code in your app level gradle file and click build apk.

android {
  ...
  splits {
    abi {
      enable true
      reset()
      include "x86", "mips"
      universalApk false
      
    }
  }
}

This block of code will generate apk for x86 and mips.

For abi, universalApk is false by default, you need to set it to true to generate Universal Apk.

To generate combined apk for screen density and cpu architecture

Write these block of code in your app level gradle file and click build apk.

android {
.....

    splits {
        density {
            enable true
            reset()
            include "mdpi", "xhdpi"
        }
        abi {
            enable true
            reset()
            include "mips64", "mips"
        }
    }
}

This block of code will generate four combined apk for mdpi-mips64, mdpi-mips, xhdpi-mips64 and xhdpi-mips.

Rules to upload multiple apk on google play

Must have distinct version code for each apks.

Must have identical package name for each apks.

How to create apk with distinct version code?

Write these block of code in your app level gradle file and click 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
    }
  }
}
}
Now you are ready to publish your apk.

That's it for now.

Happy Learning :)