apkscale
apkscale copied to clipboard
A Gradle plugin to measure the app size impact of Android libraries
apkscale
A Gradle plugin to measure the app size impact of Android libraries.
Requirements
- Android SDK
- Apkscale can only be applied within a
com.android.library
project. - apkanalyzer must be in your machine's path
- Android Gradle Plugin 7.0.0+
- Java 11
Usage
Add the following to your project's buildscript section.
buildscript {
repositories {
mavenCentral()
maven { url 'https://repo.gradle.org/gradle/libs-releases' }
// Include this line if you would like to use snapshots
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
mavenContent {
snapshotsOnly()
}
}
}
// Append -SNAPSHOT for snapshot versions
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']
// Optional parameter to specify whether the output is human readable. Defaults to true.
humanReadable = true
}
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 writes the size in a --human-readable
format as specified by apkanalyzer. Reference the example below.
[
{
"library": "your-library-release.aar",
"size": {
// Included in all reports
"universal": "21.9MB",
// Included as specified by the abis parameter
"x86": "6MB",
"x86_64": "6.1MB",
"armeabi-v7a": "4.8MB",
"arm64-v8a": "5.7MB"
}
}
]
The following demonstrates how to read the Apkscale output and convert it to a markdown table.
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)
}
}
Development
Developing the plugin requires the Android SDK to be installed and apkanalyzer
needs to be in your machine's path. The
project can be imported into Intellij IDEA CE as a Gradle project.
Publishing to Maven Local
Reference the following snippet to publish the plugin to your local maven repository. Publishing to your local maven repository can be useful when validating changes directly in an Android library project.
./gradlew publishApkscaleReleasePublicationToMavenLocal
Implementation
Apkscale builds two flavors of a mock Android application: one without the library and one with the library included. Apkscale then uses the apkanalyzer
diff operation to produce the size impact of the library.
License
Apache 2.0 license. See LICENSE for details.