코딩하는 일용직 노동자

APK파일 자동 네이밍 방법. 본문

안드로이드

APK파일 자동 네이밍 방법.

bacass 2022. 7. 16. 21:31
안드로이드 debug/release 앱을 빌드하면 생성되는 apk 파일에는 별다른 이름이 없습니다.
만들어진 apk파일

앱을 빌드할때 자동으로 버전과 날짜를 파일명에 추가해주는 방법을 사용해보겠습니다.
앱의 이름과 버전, 버전코드, 날짜를 파일명에 자동으로 만들어지게 한다.

app레벨의 build.gradle 파일에 아래의 소스를 추가해줍니다.
android {
    ...

    defaultConfig {
        applicationId 'co.kr.test.slx'
        minSdkVersion minSdk
        targetSdkVersion targetSdk
        versionCode 102
        versionName "1.0.2"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    applicationVariants.all{
        variant ->
            variant.outputs.each{
                    // on below line we are setting a name to our apk
                output->
                    // 앱의 이름을 지정해줍니다.
                    def appName = "MyApp"
                    // 파일생성 날짜를 이용하기 위한 처리.
                    def formattedDate = new Date().format('yyyyMMdd')
                    // 버전_(버전코드)_날짜
                    def verName = "v${variant.versionName}(${variant.versionCode})_$formattedDate"
                    // on below line we are creating a new name for our apk.
                    def newName = output.outputFileName
                    // on below line we are replacing our previous name with our app name.
                    newName = newName.replace("app-", "${appName}-")
                    // on below line we are replacing -release with our formatted date.
                    newName = newName.replace("-release", "_release_" + verName)
                    newName = newName.replace("-debug", "_debug_" + verName)
                    // at last we are setting our apk name to it.
                    output.outputFileName  = newName
            }
    }
}

단! 이 방법은 APK파일만 자동네이밍이 됩니다. aab파일의 자동네이밍은 따로 포스팅을 했습니다.