How to config your output file name in Gradle Kotlin DSL – Android

Tram Ho

Config your output file name in Gradle Kotlin DSL

Last time I worked a lot with Gradle Kotlin DSL . Once, I need to configure output file name of .apk file after building to support CI / CD. If using Groovy then Okie, the story is quite simple, you can find it here .

But with Gradle Kotlin DSL, it’s different, I also spent half of my “oh my god” to find a solution to this problem. Finally I found this post .

After that, I also edited it a bit to make it work. Available here to share with you guys …

./app/build.gradle.kts

  • applicationVariants : Contains all information about buildTypes, productFlavors, outputs, bla.bla …
  • variant.outputs : Contains information about the output file, you need to cast it to BaseVariantOutputImpl to change its file name.

When building App, applicationVariants will call the execute() of ApplicationVariantAction . Next, variant.outputs will call execute() of VariantOutputAction .

Regarding the file name, then we will format AppName_variantName_versionCode_DateTime.apk . Everyone can change as you like, see function createFileName() .

Here is the final result,

You can view my full source demo here .

Or gist it neatly Config your output file name in Gradle Kotlin DSL

Wished everyone success,

** Happy coding !!! **

Share the news now

Source : Viblo