Measure the App Size Impact of Android Libraries

August 10, 2020
Written by

apkscale Header

Users – especially those with pay-per-byte mobile plans – will avoid downloading Android applications if they are too large. As a result, managing your Android application size helps ensure that users are not deterred from trying out your application. Google provides great strategies for reducing your application size, but many applications depend on third-party libraries which may not consider their app size impact.

Here at Twilio we understand this challenge, which is why we publish the app size impact of our Voice Android SDKs with every release. Our CI pipeline contains a size measurement step, so we ensure that we do not introduce any unexpected size regressions during the development process.

Today, we are happy to announce that we have open sourced the utility we use to measure the size of our Android SDKs: apkscale.

Measure up

Apkscale is a Gradle plugin that helps measure the app size impact of Android libraries. The plugin is designed for Android library developers who would like to measure the size of their libraries directly from their projects. The snippets below provide the steps to include apkscale in your project.

Add this to buildscript section:

buildscript {
    repositories {
        jcenter()
        maven { url 'https://repo.gradle.org/gradle/libs-releases' }
    }
    classpath "com.twilio:apkscale:$apkscaleVersion"
}

Apply the plugin in your Android library project:

apply plugin: 'com.android.library'
apply plugin: 'com.twilio.apkscale'

apkscale {
    // Optional parameter to provide size reports for each ABI in addition to the default universal ABI
    abis = ['x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a']
}

Apkscale adds a measureSize task to your Android library module and, when run, scans the output directory of your library and measures the size of each .aar file present. Apkscale outputs the size report to a json file located at <yourProjectBuildDir>/apkscale/build/outputs/reports/apkscale.json. The json file contains an array of elements that provide a size report for each .aar file measured. Apkscale provides the size in a --human-readable format as specified by apkanalyzer. Reference the example below.

 {
    "library": "your-library-release.aar"

Ultimately, you will want to do something meaningful with the apkscale output. The following task demonstrates how to read the apkscale output and convert it to a markdown table similar to the table found on the Voice Android SDKs changelog.

task generateSizeReport {
    dependsOn('measureSize')

    doLast {
        def sizeReport = "Size Report\n" +
                "\n" +
                "| ABI             | APK Size Impact |\n" +
                "| --------------- | --------------- |\n"
        def apkscaleOutputFile = file("$buildDir/apkscale/build/outputs/reports/apkscale.json")
        def jsonSlurper = new JsonSlurper()
        def apkscaleOutput = jsonSlurper.parseText(apkscaleOutputFile.text).get(0)

        apkscaleOutput.size.each { arch, sizeImpact ->
            videoAndroidSizeReport += "| ${arch.padRight(16)}| ${sizeImpact.padRight(16)}|\n"
        }
        println(sizeReport)
    }
}

Onward!

Apkscale provides a simple approach to Android library measurement:

  1. Create an APK without the library
  2. Create an APK with the library
  3. Measure the size difference between the two APKs using apkanalyzer

Apkscale works for our needs, but we are eager to hear from you about what we can do to improve!

Aaron Alaniz is a Programmable Video and Voice tech lead at Twilio. He is interested in helping mobile developers build more engaging real-time communication experiences in their apps. He can be reached at aalaniz [at] twilio.com