This article will explore how to rename your artifacts, to convey meaning.
Why?
Use cases:
- When your application has different "flavors", you need identifiable outputs.
- When you produce multiple versions at the same time, you need to tell which artifact it is.
To check your current artifact name, simply run the Gradle task, assembleDebug
. If you haven't modified it, your output will be app-debug.apk
It is better to have a more informative and meaningful name, such as the following:
1 2 | ${applicationId}-v${versionName}(${versionCode})-${buildType}.apk |
The above sample will create a name like:
1 2 | com.example.sampleproject-v1.0.0(101)-release.apk |
By using this template, you know what this APK is.
Option 1
You can find this naming in many answers on StackOverflow.
1 2 3 4 5 6 | applicationVariants.all { variant -> variant.outputs.all { outputFileName = "${applicationId}-v${versionName}(${versionCode})-${buildType}.apk" } } |
DSL version:
1 2 3 4 5 6 7 8 9 | applicationVariants.all { outputs.forEach { output -> if (output is com.android.build.gradle.internal.api.BaseVariantOutputImpl) { output.outputFileName = "${applicationId}-v${versionName}(${this.versionCode})-${name}.${output.outputFile.extension}" } } } |
This works well for APKs but not for app bundles.
Output:
1 2 | com.example.sampleproject-v1.0.0(101)_release.apk |
Option 2
The following implementation is easy and simple, but as the attribute of "archivesBaseName", it will only set the base name, not the full file name.
1 2 3 4 5 6 7 8 | android { ... defaultConfig { ... archivesBaseName = "${applicationId}-v${versionName}(${versionCode})" } } |
DSL version:
1 2 3 4 5 6 7 8 | android { ... defaultConfig { ... setProperty("archivesBaseName", "${applicationId}-v${versionName}(${versionCode})") } } |
Output:
1 2 | com.example.sampleproject-v1.0.0(101)-release.apk |
The "-release" section is added automatically.
Compare performance
In this section, Option 1 will be compared to Option 2.
The following metrics are implemented on a 7-module project by running the following tasks:
- clean
- assembleDebug
Runs | Option 1 | Option 2 |
---|---|---|
first | 15,643 | 16,519 |
2 | 16,539 | 14.515 |
3 | 14.39 | 16,592 |
4 | 16,536 | 14.506 |
5 | 16.9 | 15,391 |
medium | 16.0016 | 15.5046 |
Compare 2 options
Conclude
As you can see from the charts and figures above, the two options have identical performance, the difference is not obvious.
Remember: When naming your artifacts, make sure you are giving them meaningful names.
Ref: https://www.giorgosneokleous.com/2019/12/01/name-your-apk-aab-files/