app/build.grardle下:
task copyArtifactsToCustomLocation { doLast { def appName = "Fichero" def flavor = "fichero" def outputDirBase = new File("D:/LujiangResources/apks/Fichero") def buildTypes = ["debug", "release"] def versionName = android.productFlavors.fichero.versionName buildTypes.each { buildType -> def apkSourceDir = file("${buildDir}/outputs/apk/${flavor}/${buildType}") def aabSourceDir = file("${buildDir}/outputs/bundle/${flavor}${buildType.capitalize()}") def mappingFile = file("${buildDir}/outputs/mapping/" + "${flavor}${buildType.capitalize()}/mapping.txt") def apkFiles = apkSourceDir.listFiles()?.findAll { it.name.endsWith(".apk") } ?: [] def aabFiles = aabSourceDir.listFiles()?.findAll { it.name.endsWith(".aab") } ?: [] def outputDir = new File(outputDirBase, "${versionName}") if (!outputDir.exists()) { outputDir.mkdirs() } apkFiles.each { apkFile -> def targetFile = new File(outputDir, "${appName}-${versionName}-${buildType}-${releaseDate()}.apk") copy { from apkFile into outputDir rename { targetFile.name } } } aabFiles.each { aabFile -> def targetFile = new File(outputDir, "${appName}-${versionName}-${buildType}-${releaseDate()}.aab") copy { from aabFile into outputDir rename { targetFile.name } } } copy { from mappingFile into outputDir } } } } tasks.whenTaskAdded { task -> if (task.name.startsWith('assemble') || task.name.startsWith('bundle')) { task.dependsOn rootProject.tasks.named('cleanAll') task.finalizedBy(copyArtifactsToCustomLocation) } }
根目录build.gradle下:
task cleanAll { def cleanTasks = subprojects.collect { it.tasks.withType(org.gradle.api.tasks.Delete) .matching { task -> task.name.endsWith('clean') } } dependsOn cleanTasks }