Android로 APK를 생성하면서 자동으로 파일 이름 생성 및 버전 명을 업데이트 하는 스크립트를 구성해 보았다
미래의 나를 위하여 기록을 남긴다
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
def versionPropsFile = file('versioninfo.properties')
def Properties versionInfo = new Properties()
if (versionPropsFile.canRead()) {
versionInfo.load(new FileInputStream(versionPropsFile))
} else {
versionInfo['VERSION_HEADER'] = "0.1105"
versionInfo['VERSION_NUMBER'] = 0
versionInfo['VERSION_COUNT'] = 0
versionInfo['BUILD_COUNT'] = 0
}
def versionHeader = versionInfo['VERSION_HEADER']
def versionNubmer = versionInfo['VERSION_NUMBER'].toInteger()
def versionCount = versionInfo['VERSION_COUNT'].toInteger()
def buildCount = versionInfo['BUILD_COUNT'].toInteger()
def taskName = gradle.startParameter.taskNames
def addVersion = 1
def isReleaseVersion = false
if (":app:bundleRelease" in taskName) {
println '---[bundleRelease]---'
versionNubmer += addVersion
versionCount += addVersion
isReleaseVersion = true
}
buildCount += addVersion
versionInfo['VERSION_HEADER'] = versionHeader.toString()
versionInfo['VERSION_NUMBER'] = versionNubmer.toString()
versionInfo['VERSION_COUNT'] = versionCount.toString()
versionInfo['BUILD_COUNT'] = isReleaseVersion ? "1" : buildCount.toString()
versionInfo.store(versionPropsFile.newWriter(), null)
def date = new Date();
def alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def thisYear = alphabet.charAt(date.getYear() + 1900 - 2001)
def thisMonth = alphabet.charAt(date.getMonth())
def monthVersion = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(versionCount)
println '-------------------'
println taskName
println "${versionHeader}.${thisYear}${thisMonth}${monthVersion}.${buildCount}"
println '-------------------'
defaultConfig {
applicationId "com.chobocho.ColorMatch"
minSdkVersion 23
targetSdkVersion 28
versionCode versionNubmer
versionName "${versionHeader}.${thisYear}${thisMonth}${monthVersion}.${buildCount}"
setProperty("archivesBaseName", "imagematch_$versionName")
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
미래의 우둔한 나를 위하여, 주석을 남긴다.
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
// 프로퍼티 파일 이름
def versionPropsFile = file('versioninfo.properties')
def Properties versionInfo = new Properties()
if (versionPropsFile.canRead()) {
versionInfo.load(new FileInputStream(versionPropsFile))
} else {
// 프로퍼티 파일이 없으면 생성을 하기위해 기본 값을 설정한다
versionInfo['VERSION_HEADER'] = "0.1105"
versionInfo['VERSION_NUMBER'] = 0
versionInfo['VERSION_COUNT'] = 0
versionInfo['BUILD_COUNT'] = 0
}
def versionHeader = versionInfo['VERSION_HEADER']
def versionNubmer = versionInfo['VERSION_NUMBER'].toInteger()
def versionCount = versionInfo['VERSION_COUNT'].toInteger()
def buildCount = versionInfo['BUILD_COUNT'].toInteger()
def taskName = gradle.startParameter.taskNames
def addVersion = 1
def isReleaseVersion = false
// Signed APK 빌드인 경우만 versionCount를 증가 시킨다
if (":app:bundleRelease" in taskName) {
println '---[bundleRelease]---'
versionNubmer += addVersion
versionCount += addVersion
isReleaseVersion = true
}
buildCount += addVersion
// 수정된 값을 프로퍼티에 업데이트 한다
versionInfo['VERSION_HEADER'] = versionHeader.toString()
versionInfo['VERSION_NUMBER'] = versionNubmer.toString()
versionInfo['VERSION_COUNT'] = versionCount.toString()
versionInfo['BUILD_COUNT'] = isReleaseVersion ? "1" : buildCount.toString()
versionInfo.store(versionPropsFile.newWriter(), null)
// 빌드한 연도와 월을 남기기 위한 부분
// 2001년 1월 > AA
// 2020년 3월 > TC
def date = new Date();
def alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def thisYear = alphabet.charAt(date.getYear() + 1900 - 2001)
def thisMonth = alphabet.charAt(date.getMonth())
def monthVersion = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(versionCount)
println '-------------------'
println taskName
println "${versionHeader}.${thisYear}${thisMonth}${monthVersion}.${buildCount}"
println '-------------------'
defaultConfig {
applicationId "com.chobocho.ColorMatch"
minSdkVersion 23
targetSdkVersion 28
versionCode versionNubmer
// 버전명 정보 -> 0.1105.TD1.2
versionName "${versionHeader}.${thisYear}${thisMonth}${monthVersion}.${buildCount}"
// 생성 되는 파일이름을 설정한다
setProperty("archivesBaseName", "imagematch_$versionName")
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}