Search code examples
grepgitlabyamlgradle-kotlin-dsl

How to get versionName/versionCode from kotlin-dsl Gradle file in Gitlab ci script?


How can I get versionName and versionCode of my android project in Gradle(Kotlin-DSL) File from Gitlab ci script(gitlab-ci.yaml)?

I have these two commands for getting them from gradle-groovy-dsl, but they don't work on gradle-kotlin-dsl.

- export VERSION_NAME=`egrep '^[[:blank:]]+versionName[[:blank:]]'  app/build.gradle | awk '{print $2}' | sed s/\"//g`
- export VERSION_CODE=`egrep '^[[:blank:]]+versionCode[[:blank:]]'  app/build.gradle | awk '{print $2}'`

sample gradle script in groovy-dsl:

android {
    defaultConfig {
        versionCode 1
        versionName "1.0"
    }
}

sample gradle script in kotlin-dsl:

android {
    defaultConfig {
        versionCode = 1
        versionName = "1.0"
    }
}

Solution

  • I Read about Regex, and i find out how to do it.

    you should change the regex to get the versionName/versionCode because the way of assigning them changed.

    you should do it this way:

    - export VERSION_NAME=`egrep '[[:blank:]]*versionName[[:blank:]]?=[[:blank:]]?\"((\w|\.)+)\"'  app/build.gradle.kts | awk '{print $3}'| sed s/\"//g`
    - export VERSION_CODE=`egrep '[[:blank:]]*versionCode[[:blank:]]?=[[:blank:]]?([0-9]+)'  app/build.gradle.kts | awk '{print $3}'`
    

    if you want to learn about regex, i recommend you to see this video on youTube