I want to start automating my android app releases...
# random
c
I want to start automating my android app releases just via command line. Everything seems pretty easy except I'm not sure how to update versionCode or versionString via bash. Am I just better off putting those two values in a .properties file and updating that file instead?
s
Possible. Or you retrieve it from environment variables. Using GitHub actions this is my preferred way.
r
Yeah, putting it in
gradle.properties
is probably the easiest solution. In fact, I'll do you one better. This is what I usually do in my projects: `gradle.properties`:
Copy code
majorVersion=1
minorVersion=0
patchVersion=0
testVersion=94
`build.gradle.kts`:
Copy code
val majorVersion = (findProperty("majorVersion") as String).toInt()
    val minorVersion = (findProperty("minorVersion") as String).toInt()
    val patchVersion = (findProperty("patchVersion") as String).toInt()
    val testVersion = (findProperty("testVersion") as String).toInt()

        versionCode = (majorVersion * 100000) + (minorVersion * 1000) + (patchVersion * 100) + testVersion
        versionName = if (testVersion > 0) {
            "${majorVersion}.${minorVersion}.${patchVersion}.${testVersion}"
        } else {
            "${majorVersion}.${minorVersion}.${patchVersion}"
        }
That way the
versionCode
is automatically derived from the
versionName
. So, for example, version 1.0.0.94 has a versionCode of 100094. This way all you need to do is bump the minor or patch version number in
gradle.properties
and everything else is taken care of.
👍 1
c
ooh. i didn't think about shoving it in gradle.properties
c
In case this approach interests you, my app doesn’t store version code/name anywhere. When I push a git tag formatted as semver, CI parses the tag and sets env vars for that build. So under this approach, the git tag is the source of truth. And we treat git tags as immutable, so any given release can always be tracked to a specific commit, and that never changes. If QA rejects a release build, the next tag increases the version number
s
For Ashampoo Photos I don't need a semver at all. The version number is created based on the Git timestamp. I use this plugin for that: https://github.com/StefanOltmann/gradle-git-versioning-plugin
When the plugin is applied I do this for Android.
Copy code
val extension: de.stefan_oltmann.VersioningExtension =
    project.rootProject.extensions.findByName("gitVersioning")
        as de.stefan_oltmann.VersioningExtension

val commitTime: Int = extension.commitTime

android {

    [... snip ...]

    defaultConfig {

        applicationId = "com.ashampoo.photos"

        minSdk = Versions.androidMinSdk
        targetSdk = Versions.androidTargetSdk


        val fiftyYearsInSeconds = 1_600_000_000

        // The versionCode can have a max value of 2000000000.
        // As a timestamp this is the year 2033 and near future.
        // Subtracting 1600000000 gives another 50 years on top.
        versionCode = commitTime - fiftyYearsInSeconds
        versionName = project.rootProject.version.toString()
    }
}
Mind that this is my temporary solution until the year 2255 for desktop. And it will only work for Android for the next 60 years. 👀 After that I will need to come up with something better and more future-proof. 🙂