Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

versionCodeOverride equivalent for Gradle Kotlin DSL

    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            int newVersionCode = android.defaultConfig.versionCode * 10 + abiVersionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0)

            output.versionCodeOverride = newVersionCode
        }
    }

I am trying to convert this Gradle Groovy DSL code to the new Gradle Kotlin DSL. I want the code to work exactly like it used to were APK splitted variant follows my versionCode pattern

This is what i have tried to write in Kotlin DSL:

    applicationVariants.all(object : Action<ApplicationVariant> {
        override fun execute(variant: ApplicationVariant) {
            variant.outputs.forEach {output ->
                val newVersionCode = defaultConfig.versionCode ?: 0 * 10 + abiVersionCodes[output.filters.first { it.identifier == com.android.build.OutputFile.ABI }]
                output.versionCodeOverride = newVersionCode
            }
        }
    })

But it says: "Unresolved reference: versionCodeOverride"

What is the correct way of doing this with Kotlin DSL?

like image 691
PhilipSa Avatar asked Dec 08 '18 07:12

PhilipSa


People also ask

What is Kotlin DSL?

The Kotlin DSL provides built-in support for three destination types: Fragment , Activity , and NavGraph destinations, each of which has its own inline extension function available for building and configuring the destination.

What is Kotlin DSL precompiled script plugins?

Precompiled script plugins Precompiled script plugins are basically Kotlin scripts that can be placed together with other Kotlin source sets ( src/main/kotlin ). Precompiled scripts have accessors, and don't need extensions ( Project. android ) to be added, like Binary plugins.

What is Kotlin DSL Android?

Kotlin DSL brings the simplicity of the Kotlin language syntax and rich API set right into the script files on top of that code completion makes it perfect to work with Gradle script files. We will use this to manage our dependencies and project settings configurations more elegantly.

Does gradle work with Kotlin?

Gradle is a build system that is very commonly used in the Java, Android, and other ecosystems. It is the default choice for Kotlin/Native and Multiplatform when it comes to build systems.


1 Answers

output actually has ApkVariantOutputImpl type, that has setVersionCodeOverride(int versionCodeOverride) method. So you just can cast output to this type explicitly to use this method in Kotlin:

(output as ApkVariantOutputImpl).versionCodeOverride = ...

Also, to get abi version, you should use this code:

val abi = output.filters.find { it.filterType == OutputFile.ABI }?.identifier

and abi will be x86, armeabi-v7a, etc.

After all, your code should look something like this:

android.applicationVariants.all {
   outputs.forEach { output ->
      val newVersionCode = defaultConfig.versionCode ?: 0 * 10 + abiVersionCodes[output.filters.find { it.filterType == OutputFile.ABI }?.identifier]
      (output as ApkVariantOutputImpl).versionCodeOverride = newVersionCode
   }
}
like image 104
EgorD Avatar answered Oct 13 '22 19:10

EgorD