'How to access variant.outputFileName in Kotlin

We've been using a snippet like this one to rename the APK file generated by our Gradle build:

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "${variant.name}-${variant.versionName}.apk"
    }
}

Source: https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration#variant_output

I am now in the process of converting my build.gradle to build.gradle.kts, i. e. to the Gradle Kotlin DSL. This is one of the last missing pieces: I can't figure out how to access outputFileName.

According to the API docs it does not even seem to exist:

  • BaseVariant.getOutputs() returns a DomainObjectCollection<BaseVariantOutput> which provides the all method used in the snippet.
  • BaseVariantOutput extends OutputFile which extends VariantOutput but none of these has an outputFileName or any getters or setters of a matching name.

So, I suspect there is some advanced Groovy magic at work to make this work - but how do I get there in Kotlin?



Solution 1:[1]

A little simplified version of @david.mihola answer:

android {

    /**
    * Notes Impl: Use DomainObjectCollection#all
    */
    applicationVariants.all {
        val variant = this
        variant.outputs
            .map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl }
            .forEach { output ->
                val outputFileName = "YourAppName - ${variant.baseName} - ${variant.versionName} ${variant.versionCode}.apk"
                println("OutputFileName: $outputFileName")
                output.outputFileName = outputFileName
            }
    }

}

Solution 2:[2]

Browsing through the source code of the Android Gradle plugin, I think I found the answer - here we go:

We are actually dealing with objects of type BaseVariantOutputImpl and this class does have both these methods:

public String getOutputFileName() {
    return apkData.getOutputFileName();
}

public void setOutputFileName(String outputFileName) {
    if (new File(outputFileName).isAbsolute()) {
        throw new GradleException("Absolute path are not supported when setting " +
                    "an output file name");
    }
    apkData.setOutputFileName(outputFileName);
}

Using this knowledge we can now:

import com.android.build.gradle.internal.api.BaseVariantOutputImpl

and then cast our target objects like so:

applicationVariants.all(object : Action<ApplicationVariant> {
    override fun execute(variant: ApplicationVariant) {
        println("variant: ${variant}")
        variant.outputs.all(object : Action<BaseVariantOutput> {
            override fun execute(output: BaseVariantOutput) {

                val outputImpl = output as BaseVariantOutputImpl
                val fileName = output.outputFileName
                        .replace("-release", "-release-v${defaultConfig.versionName}-vc${defaultConfig.versionCode}-$gitHash")
                        .replace("-debug", "-debug-v${defaultConfig.versionName}-vc${defaultConfig.versionCode}-$gitHash")
                println("output file name: ${fileName}")
                outputImpl.outputFileName = fileName
            }
        })
    }
})

So, I guess: Yes, there is some Groovy magic at work, namely that Groovy's dynamic type system allows you to just access getOutputFileName and setOutputFileName (by way of the abbreviated outputImpl.outputFileName syntax, as in Kotlin) from your code, hoping they will be there at runtime, even if the compile time interfaces that you know about don't have them.

Solution 3:[3]

Shorter version using lambdas:

    applicationVariants.all{
        outputs.all {
            if(name.contains("release"))
                (this as BaseVariantOutputImpl).outputFileName = "../../apk/$name-$versionName.apk"
        }
    }

This will place APK into app/apk folder with name made of variant name and version code.
You can change the format of filename as you wish.
Important: it must be done only on release builds, because ".." in path corrupts debug build process with strange errors.

Solution 4:[4]

For libraryVariants it is possible to change output file name without accessing internal api:

    libraryVariants.all {
        outputs.all {
            packageLibraryProvider {
                archiveFileName.set("yourlibrary-${buildType.name}.aar")
            }
        }
    }

Solution 5:[5]

For Kotlin KTS.

NOTE: This is considered a temporal soluciĆ³n, until a proper way to do it in KTS is released by Android team.

Working in AGP v7.1.2 it might work also in lower versions of AGP.

:app build.gradle

android {

// ...

    this.buildOutputs.all {

        val variantOutputImpl = this as com.android.build.gradle.internal.api.BaseVariantOutputImpl

        val variantName: String = variantOutputImpl.name

        val outputFileName = "custom-name-${variantName}.apk"
        
        variantOutputImpl.outputFileName = outputFileName
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Braian Coronel
Solution 2
Solution 3 Pointer Null
Solution 4 nsk
Solution 5 Akhha8