//android.applicationVariants.all { variant ->
//    variant.outputs.each { output ->
//        // Redirect your apks to new defined location to outputDirPath
//        def outputDirPath = new File("${project.rootDir.absolutePath}/apks/${variant.flavorName}/${variant.buildType.name}")
//        variant.packageApplicationProvider.get().outputDirectory = outputDirPath
//
//        def apkFileName = "${rootProject.name}_${android.defaultConfig.versionName}.apk"
//        output.outputFileName = apkFileName // directly assign the new name back to outputFileName
//    }
//}
//
android.applicationVariants.all { variant ->
    ;
    def appName
    // Use applicationName if available, else use parent name
    if (project.hasProperty("applicationName")) {
        appName = applicationName
    } else {
        appName = parent.name
    }

    variant.outputs.each { output ->
        def apkFileName = "${appName}_${android.defaultConfig.versionName}.apk"
        output.outputFileName = apkFileName // directly assign the new name back to outputFileName

        //-- original causes error
        //output.outputFileName = new File(output.outputFile.parent, newApkName)
    }
}
