Skip to content

Commit 3c9f431

Browse files
authored
Repack apk files into zip format to generate stable size measurements. (#1719)
1 parent ce9baa7 commit 3c9f431

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

apk-size/apk-size.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ buildscript {
2424
}
2525
}
2626

27+
plugins {
28+
id "com.dorongold.task-tree" version "1.5"
29+
}
30+
2731
apply from: '../sdkProperties.gradle'
2832

2933
task clean(type: Delete) {

apk-size/app/configure.gradle

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,41 @@ if (project.hasProperty('sdks')) {
4848
}
4949
}
5050
}
51+
52+
53+
// Configures two new tasks `unpack` and `repack` for each `assemble` task created for all
54+
// configured application build variants.
55+
//
56+
// `unpack` task takes the apk file and unpacks it into a folder `archive`. `repack` task compresses
57+
// the folder back into a zip file. The size of the generated zip file will then be collected as the
58+
// stable version of the size for its corresponding apk file.
59+
project.afterEvaluate {
60+
def unpack = project.tasks.create("unpack")
61+
def repack = project.tasks.create("repack")
62+
63+
android.applicationVariants.all {
64+
def assemble = it.assembleProvider.get()
65+
def packageApplication = it.packageApplicationProvider.get()
66+
def apk = packageApplication.apkNames[0]
67+
def dir = packageApplication.outputDirectory
68+
def unpackTaskName = "unpack" + it.name.capitalize()
69+
def repackTaskName = "repack" + it.name.capitalize()
70+
71+
def unpackVariant = project.tasks.create(unpackTaskName, Copy.class) {
72+
dependsOn assemble
73+
74+
from zipTree(file("$dir/$apk"))
75+
into file("$dir/archive")
76+
}
77+
unpack.dependsOn unpackVariant
78+
79+
def repackVariant = project.tasks.create(repackTaskName, Zip.class) {
80+
dependsOn unpackTaskName
81+
82+
archiveFileName = apk.replace(".apk", ".zip")
83+
destinationDirectory = file(dir)
84+
from "$dir/archive"
85+
}
86+
repack.dependsOn repackVariant
87+
}
88+
}

ci/fireci/fireciplugins/binary_size.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def binary_size(pull_request, log, metrics_service_url, access_token):
5757
artifacts = affected_artifacts if pull_request else all_artifacts
5858
sdks = ','.join(artifacts)
5959

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

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

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

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

9494
return test_results

0 commit comments

Comments
 (0)