Skip to content

Instantly share code, notes, and snippets.

@huuphuoc1396
Last active July 30, 2024 20:46
Show Gist options
  • Save huuphuoc1396/1487f317f3001b78b77776391e084956 to your computer and use it in GitHub Desktop.
Save huuphuoc1396/1487f317f3001b78b77776391e084956 to your computer and use it in GitHub Desktop.
Config your output file name in Gradle Kotlin DSL
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.api.BaseVariantOutput
import com.android.build.gradle.internal.api.BaseVariantOutputImpl
android {
//...
applicationVariants.all(ApplicationVariantAction())
}
class ApplicationVariantAction : Action<ApplicationVariant> {
override fun execute(variant: ApplicationVariant) {
val fileName = createFileName(variant)
variant.outputs.all(VariantOutputAction(fileName))
}
private fun createFileName(variant: ApplicationVariant): String {
return "YourAppName" +
"_${variant.name}" +
"_verCode${Android.versionCode}" +
"_${getDateTimeFormat()}.apk"
}
private fun getDateTimeFormat(): String {
val simpleDateFormat = SimpleDateFormat("yyMdHms", Locale.US)
return simpleDateFormat.format(Date())
}
class VariantOutputAction(
private val fileName: String
) : Action<BaseVariantOutput> {
override fun execute(output: BaseVariantOutput) {
if (output is BaseVariantOutputImpl) {
output.outputFileName = fileName
}
}
}
}
@hinotesfreak
Copy link

hinotesfreak commented Jun 17, 2024

also notice that this classes is deprecated
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.api.BaseVariantOutput
..any plan updating this script?
thanks

@pankajXdev
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment