- gradle 基本文件
- Android自定义输出APK名称
gradle 基本文件
- 修改
项目build.gradle
// 版本号相关
versionCode = 1
versionName = "1.0.0"
// 相关依赖库版本
compileSdkVersion = 28
buildToolsVersion = "29.0.0"
minSdkVersion = 17
targetSdkVersion = 28
supportLibraryVersion = '27.1.1'
buildscript {
repositories {
google()
jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
maven {
url 'https://maven.google.com/'
name 'Google'
}
maven {
url 'https://dl.bintray.com/cjt/maven'
}
google()
}
}
- 修改gradle文件夹下的配置
distributionUrl=https://services.gradle.org/distributions/gradle-4.6-all.zip
Android自定义输出APK名称
两种写法
1 Android plugin 3.0 前
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
def fileName
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 输出apk名称为XXapp1.4.0_2017-02-14.apk
if (variant.buildType.name.equals('release')) {
fileName = "XXapp${defaultConfig.versionName}_${releaseTime()}.apk"
} else if (variant.buildType.name.equals('debug')) {
fileName = "XXapp${defaultConfig.versionName}_${releaseTime()}_debug.apk"
}
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
2 3.0后
//配置自定义打包名称
applicationVariants.all { variant ->
variant.outputs.all {
def fileName
def date = new Date()
def formattedDate = date.format('yyyyMMdd')
if (variant.buildType.name.equals('release')) {
fileName = "${variant.mergedFlavor.versionName}_release_${formattedDate}.apk"
} else if (variant.buildType.name.equals('debug')) {
fileName = "${variant.mergedFlavor.versionName}_debug_${formattedDate}.apk"
}
outputFileName = fileName;
}
}
Android plugin 3.0 建议使用
写法变化一:Use all() instead of each()
写法变化二:Use outputFileName instead of output.outputFile if you change only file name (that is your case)