Search code examples
androidkotlinbuilddsl

how to generate versionName string in build.gradle.kts to be used in settings xml file in android app?


I have file build.gradle for module level recently changed to be new Kotline DSL version rename to build.gradle.kts

I have a line to generate versionName to be used as string in xml (settings activity to show version of application)

old code

applicationVariants.all { variant ->
        variant.resValue "string", "versionName", variant.versionName }

I tried to convert it to Kotlin DSL but no luck

I tried this but not working

applicationVariants.all { variant -> variant.resValue("string", "versionName", variant.versionName)}

gives error

Type mismatch: inferred type is Unit but Boolean was expected


Solution

  • The problem is that there is 2 version of applicationVariants on returns a Boolean, so when you use your code

    applicationVariants.all { variant -> variant.resValue("string", "versionName", variant.versionName)}
    

    applicationVariants.all is expecting a Boolean, so you need to return either false or true, but the one you are looking is without the boolean option

    applicationVariants.all { resValue("string", "versionName",versionName)}
    

    The signature seems the same, but when you are using the IDE select the one that is not "Boolean" as return, is quite confusing but I had this issue as well.