Coder Social home page Coder Social logo

twilio / apkscale Goto Github PK

View Code? Open in Web Editor NEW
93.0 16.0 9.0 160 KB

A Gradle plugin to measure the app size impact of Android libraries

License: Apache License 2.0

Kotlin 100.00%
android gradle-plugin kotlin android-libraries android-library android-development android-sdk

apkscale's Introduction

apkscale

CircleCI

A Gradle plugin to measure the app size impact of Android libraries.

apkscale-logo

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 8.0.0+
  • Java 17

Usage

Maven Central

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.

apkscale's People

Contributors

afalls-twilio avatar paynerc avatar twilio-sdk-build avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

apkscale's Issues

Does not work when the library uses third-party resources

Describe the bug
The library for which I want to measure the size depends on Android Support AppCompat and uses the theme Theme.AppCompat.Light.Dialog.Alert. When I run the measureSize task, I get the following error:

Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-6.1.1-bin.zip'.`

When I try to assemble the apkscale project manually to get more details about the error, I get the following error:

AAPT: error: resource style/Theme.AppCompat.Light.Dialog.Alert (aka com.twilio.apkscale.withlibrary:style/Theme.AppCompat.Light.Dialog.Alert) not found.`

And that seems normal since the apkscale project only depends on the AAR of the library but not on its dependencies.

Expected behavior
I would expect apkscale to be able to measure the size of the library even if it depends on other dependencies. But I'm unsure if it should also measure the size impact of the library dependencies.

Actual behavior
The measureSize task fails.

Version

  • Version: 0.1.1

Does not support project depending on another project

Describe the bug
In my case, this does not work as I have 3 libraries (A, B and C) in my project and libraries B and C depends on library A. Running measureSize for library A works but it throws an error for B and C:

Caused by: org.gradle.internal.resolve.ModuleVersionNotFoundException: Could not find prj-android:gssdk-core:unspecified.
Searched in the following locations:

From what I understand apkScale tries to add a dependency to library A as an external dependency, rather than a local dependency. And it shouldn't be needed to add local library dependencies since their code should already present since you depend on the current library AAR.

An alternative to excluding local dependencies would to depend on the library project rather than on its AAR (as you suggested here), this way you would get all the dependencies for free.

Version

  • Version: 0.1.3

Task fails with Gradle 7.0.2

Before filing an issue please check the issue is not already addressed by the following:

Describe the bug
When running the task with Gradle 7.0.2, an error is thrown:

A problem was found with the configuration of task ':myApp:measureSize' (type 'MeasureAndroidLibrarySizeTask').
  - Type 'com.twilio.apkscale.tasks.MeasureAndroidLibrarySizeTask' property 'ndkVersion$apkscale' is missing an input or output annotation.
    
    Reason: A property without annotation isn't considered during up-to-date checking.
    
    Possible solutions:
      1. Add an input or output annotation.
      2. Mark it as @Internal.
    
    Please refer to https://docs.gradle.org/7.0.2/userguide/validation_problems.html#missing_annotation for more details about this problem.

To Reproduce
Steps to reproduce the behavior:

  1. run gradle myApp:measureSize

Version

  • Version: 0.1.2
  • Gradle: 7.0.2
  • AGP: 7.0.2

Report is deleted when another Gradle task is run

Describe the bug
After running the measureSize task, the report is correctly reported at build/apkscale/build/outputs/reports/apkscale.json but as soon as I run another Gradle task, the report is deleted.

To Reproduce
Steps to reproduce the behavior:

  1. Run ./gradlew measureSize
  2. Run another Gradle task such as ./gradlew test
  3. Report is no longer present in build/apkscale folder

Expected behavior
I would expect the report to be kept.

Actual behavior
The report is not present anymore.

Version

  • Version: 0.1.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.