Last active
July 30, 2024 20:46
-
-
Save huuphuoc1396/1487f317f3001b78b77776391e084956 to your computer and use it in GitHub Desktop.
Config your output file name in Gradle Kotlin DSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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