自定义task任务,实现productFlavors中不同flavor的自动打包。代码如下:
flavors.keySet().each { flavor ->
buildTypes.each { buildType ->
def flavorCNName = getFlavorCNName(flavor)
tasks.create(name: "打包-" + "$flavorCNName" + "-${buildType}") {
group groupName
def flavorFirst = flavor.subSequence(0, 1)
def flavorRemainder = flavor.subSequence(1, flavor.length())
def buildTypeFirst = buildType.subSequence(0, 1)
def buildTypeRemainder = buildType.subSequence(1, buildType.length())
dependsOn "assemble${flavorFirst.toUpperCase()}${flavorRemainder}${buildTypeFirst.toUpperCase()}${buildTypeRemainder}"
doLast {
def fromPath = buildDir + "/outputs/apk/${flavor}/${buildType}"
def toPath = "$distDir/$flavorCNName/${buildType}"
println("fromPath:$fromPath")
println("distDir:$distDir")
println("toPath:$toPath")
copy {
from(fromPath)
into(toPath)
include('*.apk')
}
}
}
}
}