Agregue el siguiente código en app.gradle
Puede modificarse según sus necesidades
afterEvaluate {
android.applicationVariants.all { variants ->
if (variants.getName().endsWith("Release") || variants.getName().endsWith("release")) {
variants.outputs.all { output ->
if (output.outputFile != null &&
output.outputFile.name.endsWith('.apk')) {
output.outputFileName = "${variants.flavorName}_${variants.versionName.replace(".", "_")}.apk"
}
}
} else if (variants.getName().endsWith("Debug") || variants.getName().endsWith("debug")) {
variants.outputs.all { output ->
if (output.outputFile != null &&
output.outputFile.name.endsWith('.apk')) {
output.outputFileName = "${variants.flavorName}_${variants.versionName.replace(".", "_")}_debug.apk"
}
}
}
}
}