Skip to content

Repack apk files into zip format to generate stable size measurements. #1719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apk-size/apk-size.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ buildscript {
}
}

plugins {
id "com.dorongold.task-tree" version "1.5"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this plugin for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/dorongold/gradle-task-tree

This is used to print out a tree of task dependencies. It is added to the apk-size project only. The main project will not be affected.

}

apply from: '../sdkProperties.gradle'

task clean(type: Delete) {
Expand Down
38 changes: 38 additions & 0 deletions apk-size/app/configure.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,41 @@ if (project.hasProperty('sdks')) {
}
}
}


// Configures two new tasks `unpack` and `repack` for each `assemble` task created for all
// configured application build variants.
//
// `unpack` task takes the apk file and unpacks it into a folder `archive`. `repack` task compresses
// the folder back into a zip file. The size of the generated zip file will then be collected as the
// stable version of the size for its corresponding apk file.
project.afterEvaluate {
def unpack = project.tasks.create("unpack")
def repack = project.tasks.create("repack")

android.applicationVariants.all {
def assemble = it.assembleProvider.get()
def packageApplication = it.packageApplicationProvider.get()
def apk = packageApplication.apkNames[0]
def dir = packageApplication.outputDirectory
def unpackTaskName = "unpack" + it.name.capitalize()
def repackTaskName = "repack" + it.name.capitalize()

def unpackVariant = project.tasks.create(unpackTaskName, Copy.class) {
dependsOn assemble

from zipTree(file("$dir/$apk"))
into file("$dir/archive")
}
unpack.dependsOn unpackVariant

def repackVariant = project.tasks.create(repackTaskName, Zip.class) {
dependsOn unpackTaskName

archiveFileName = apk.replace(".apk", ".zip")
destinationDirectory = file(dir)
from "$dir/archive"
}
repack.dependsOn repackVariant
}
}
10 changes: 5 additions & 5 deletions ci/fireci/fireciplugins/binary_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def binary_size(pull_request, log, metrics_service_url, access_token):
artifacts = affected_artifacts if pull_request else all_artifacts
sdks = ','.join(artifacts)

gradle.run('assemble', '-p', 'apk-size', '--continue', gradle.P('sdks', sdks), check=False)
gradle.run('repack', '-p', 'apk-size', '--continue', gradle.P('sdks', sdks), check=False)

test_results = _measure_aar_sizes(artifacts) + _measure_apk_sizes(artifacts)
test_report = {'metric': 'BinarySize', 'results': test_results, 'log': log}
Expand All @@ -84,11 +84,11 @@ def _measure_apk_sizes(artifacts):

for artifact in artifacts:
group_id, artifact_id, version = artifact.split(':')
apk_files = glob.glob(f'./apk-size/**/{artifact_id}/**/*.apk', recursive=True)
apk_zip_files = glob.glob(f'./apk-size/**/{artifact_id}/**/*.zip', recursive=True)

for apk_file in apk_files:
build_type = re.search(fr'{artifact_id}/([^/]*)/', apk_file).group(1)
apk_size = os.path.getsize(apk_file)
for zip_file in apk_zip_files:
build_type = re.search(fr'{artifact_id}/([^/]*)/', zip_file).group(1)
apk_size = os.path.getsize(zip_file)
test_results.append({'sdk': artifact_id, 'type': f'apk ({build_type})', 'value': apk_size})

return test_results
Expand Down