在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.