
图片发自简书App
在Android studio中默认生成的apk、aar的名称都是module的名称加上构建的类型,也就是buildType,经常看到的就是app-release.apk、app-debug.apk、app-release.aar、app-debug.aar这一类。
如果使用了
productFlavors则会显示成这样app-ceshi-debug.apk
可以通过配置在build.gradle配置来修改文件名
在build.gradle的android中添加一下代码
android{
//...
android.applicationVariants.all{ variant ->
variant.outputs.all{
def fileName = "${project.name}_${buildType.name}_v${defaultConfig.versionName}_${defaultConfig.versionCode}.apk"
outputFileName = fileName
}
}
//...
}
如果是aar文件,则是使用android.libraryVariants.all来配置
android{
//...
android.libraryVariants.all{ variant ->
variant.outputs.all{
def fileName = "${project.name}_${buildType.name}_v${defaultConfig.versionName}_${defaultConfig.versionCode}.aar"
outputFileName = fileName
}
}
//...
}
-
${project.name}也就是当前module的名字 -
${buildType.name}就是当前构建的类型,例如 debug 或者 release -
${defaultConfig.versionName}和${defaultConfig.versionCode}则分别对应了defaultConfig中的versionName和versionCode
如果有需要,可以添加更多的一些配置,例如渠道、打包时间等等
顺带附上以前可以使用的配置代码
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = "自定义.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
但是在3.4以后的Android Studio在编译是会报错
Cannot set the value of read-only property 'outputFile' for object of type com.android.build.gradle.internal.api.LibraryVariantOutputImpl.