Decreasing App size
When an APK is downloaded by a user, unused code and data is also downloaded. This isn’t ideal, but most Android developers are doing it this way since it’s the default. Fortunately, the Google Developer API is robust and allows for multiple APK support if you want to target specific devices with slimmed down APKs.
1
2
import com.android.build.OutputFile
3
4
// For each APK output variant, override versionCode with a combination of
5
// ext.abiCodes * 1000 + variant.versionCode. In this example, variant.versionCode
6
// is equal to defaultConfig.versionCode. If you configure product flavors that
7
// define their own versionCode, variant.versionCode uses that value instead.
8
android.applicationVariants.all { variant ->
9
10
// Assigns a different version code for each output APK
11
// other than the universal APK.
12
variant.outputs.each { output ->
13
14
// Stores the value of ext.abiCodes that is associated with the ABI for this variant.
15
def baseAbiVersionCode =
16
// Determines the ABI for this variant and returns the mapped value.
17
project.ext.abiCodes.get(output.getFilter(OutputFile.ABI))
18
19
// Because abiCodes.get() returns null for ABIs that are not mapped by ext.abiCodes,
20
// the following code does not override the version code for universal APKs.
21
// However, because we want universal APKs to have the lowest version code,
22
// this outcome is desirable.
23
if (baseAbiVersionCode != null) {
24
25
// Assigns the new version code to versionCodeOverride, which changes the version code
26
// for only the output APK, not for the variant itself. Skipping this step simply
27
// causes Gradle to use the value of variant.versionCode for the APK.
28
output.versionCodeOverride =
29
baseAbiVersionCode * 1000 + variant.versionCode
30
}
31
}
32
}
33
Copied!
It is highly recommended to add this code to the end of the build.gradle file of your main project. This code tells to Gradle to generate multiple APKs by ABI type during a release build. And therefore the size of your application will be much smaller.
Last modified 1yr ago
Copy link