KTS 脚本
// 顶层 build.gradle.kts
val apkBaseName: String = "VoiceAssistant"
allprojects {
project.extra.apply {
set("apkBaseName", apkBaseName)
}
}
// 模块 build.gradle.kts
android {
applicationVariants.all {
val apkBaseName = project.ext.get("apkBaseName") as String
val moduleName = project.name
val apkName = if (apkBaseName.isNotBlank()) {
"${apkBaseName}_${moduleName}_${versionName}.apk"
} else {
"${moduleName}_${versionName}.apk"
}
if (buildType.name == "release") {
// 也可以用 assembleProvider.configure {
assembleProvider.get().doLast {
// 可以这样过滤 if (name == "assembleRelease")
project.copy {
val fromDir = packageApplicationProvider.get().outputDirectory.asFile
val outDir = File(project.rootDir, "outputs")
println("> Task : copy from $fromDir into $outDir")
from(fromDir)
into(outDir)
include("**/*.apk")
rename { apkName }
}
}
}
}
}
Gradle
// 模块 build.gradle
android {
applicationVariants.all { variant ->
// apk 改名
variant.outputs.all { output ->
outputFileName = "Factory" + "-" + buildType.name + "-" + defaultConfig.versionName + "-" + defaultConfig.versionCode + ".apk"
}
variant.assemble.doLast {
// 将生成的 APK 复制到 app/release 文件夹
File outDir = new File('release')
variant.outputs.forEach { file ->
copy {
from file.outputFile
into outDir
}
}
}
}
}
标签:name,val,apkBaseName,project,apk,studio,Android,outDir
From: https://www.cnblogs.com/qianjiahong/p/18245661