PhilipSa
PhilipSa

Reputation: 255

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?

Upvotes: 8

Views: 3283

Answers (1)

EgorD
EgorD

Reputation: 896

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
   }
}

Upvotes: 8

Related Questions