-
[Gradle] 안드로이드 빌드 버전 자동으로 관리하기Mobile App/Android 2021. 12. 14. 13:16
플레이스토어에 앱 업로드 시
versionCode
가 기존에 올라가있는 Bundle과 같거나 작을 경우 업로드가 차단된다.매번 까먹고 다시 빌드해서 다시 올리는건 상당히 귀찮은 일이다.
다행히도 Gradle 문법에서 Programmatically Versioning이 가능하다.
먼저 Module단계의 폴더(기본: app)에
version.properties
파일을 하나 생성해 주고관리하고 싶은 버전의 이름을 넣는다.
VERSION_MAIN=0 VERSION_SUB=1 VERSION_CHILD=5 VERSION_RELEASE=5
이후 Module 단계의
build.gradle
을 수정해주면 된다.나는 Main은 신규기능 추가나 시스템 개편 등 업데이트를 설치하지 않으면 앱 사용이 불가할 정도의 업데이트
Sub는 UI개편 등의 중규모 업데이트
Child는 자잘한 버그수정을 할 때 수정한다.Release는
versionCode
에 사용할 예정이다.파일은 아래와 같이 불러오면 된다.
// 파일 열기 def vPropertiesFile = file('version.properties') if( vPropertiesFile.canRead() ) { def props = new Properties() props.load(new FileInputStream(vPropertiesFile)) // Properties 불러오기. String타입으로 받기 때문에 toInteger를 해줘야 한다. def VERSION_RELEASE = props.getProperty('VERSION_RELEASE').toInteger() def VERSION_MAIN = props.getProperty('VERSION_MAIN').toInteger() def VERSION_SUB = props.getProperty('VERSION_SUB').toInteger() def VERSION_CHILD = props.getProperty('VERSION_CHILD').toInteger() ...
여기서 바로 버전을 수정해줄 수도 있지만 그러면
targetSdkVersion
을 수정하는 등gradle만 빌드할 때도 수정되기 때문에
앱을 apk나 aab로 archive할 때에만 버전을 올리고 싶으면 아래와 같이 코드를 넣어주면 된다.
def runTasks = gradle.startParameter.taskNames if (':app:assembleRelease' in runTasks || ':app:bundleRelease' in runTasks) { VERSION_RELEASE = VERSION_RELEASE + 1 VERSION_CHILD = VERSION_CHILD + 1 ... }
이제 1씩 증가시킨 버전코드를
version.properties
에 저장해주고,... props.setProperty("VERSION_RELEASE", VERSION_RELEASE.toString()) props.setProperty("VERSION_CHILD", VERSION_CHILD.toString()) props.store(vPropertiesFile.newWriter(), null) ...
versionCode
와versionName
에 변수를 넣어준다.defaultConfig { ... versionCode VERSION_RELEASE versionName "${VERSION_MAIN}.${VERSION_SUB}.${VERSION_CHILD}" ...
아래는
build.gradle
의 전체 코드이다.plugins { id 'com.android.application' } android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } buildToolsVersion '30.0.3' ndkVersion '21.0.6113669' def vPropertiesFile = file('version.properties') if (vPropertiesFile.canRead()) { def props = new Properties() props.load(new FileInputStream(vPropertiesFile)) def VERSION_RELEASE = props.getProperty('VERSION_RELEASE').toInteger() def VERSION_MAIN = props.getProperty('VERSION_MAIN').toInteger() def VERSION_SUB = props.getProperty('VERSION_SUB').toInteger() def VERSION_CHILD = props.getProperty('VERSION_CHILD').toInteger() def runTasks = gradle.startParameter.taskNames if (':app:assembleRelease' in runTasks || ':app:bundleRelease' in runTasks) { VERSION_RELEASE = VERSION_RELEASE + 1 VERSION_CHILD = VERSION_CHILD + 1 props.setProperty("VERSION_RELEASE", VERSION_RELEASE.toString()) props.setProperty("VERSION_CHILD", VERSION_CHILD.toString()) props.store(vPropertiesFile.newWriter(), null) def aabName = "${VERSION_MAIN}.${VERSION_SUB}.${VERSION_CHILD}_r${VERSION_RELEASE}" setProperty("archivesBaseName", aabName) } buildTypes { debug{ minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileSdkVersion 31 defaultConfig { applicationId "com.application.id" minSdkVersion 24 targetSdkVersion 31 versionCode VERSION_RELEASE versionName "${VERSION_MAIN}.${VERSION_SUB}.${VERSION_CHILD}" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } } } dependencies { ... }
'Mobile App > Android' 카테고리의 다른 글
갤럭시 S8+ TWRP 설치하기 (0) 2022.04.14 [Android] FCM 메세지 background에서 실행 안될때 - API level 31 (안드로이드 12) (0) 2022.03.15 [Gradle] 안드로이드 빌드 버전 자동으로 관리하기 (0) 2021.12.14 Cross-App Scripting Reject 해결방법 (4) 2021.09.12 하이브리드 앱 테스트 환경에서 카톡로그인 intent 처리 (0) 2021.05.24 안드로이드 웹뷰 뒤로가기 버튼 이벤트 만들기 (0) 2021.05.12