2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Androidの自動バージョンインクリメントをgradleのタスクで実行

Last updated at Posted at 2022-06-02

CIなどで自動化するときにバージョンもついでに上げたくなるのでこの設定で最近は落ち着いたので備忘録メモ
ktsしたいけど、taskはまとめてgroovyにまだしてる。アプリのbuild.gradleはktsが主流なのでそっちに合わせるってことで。

appフォルダの下にversion.propertiesファイルを用意

version.properties
VERSION_NAME=1.0.0
VERSION_CODE=1

build.gradleに下記のtaskを用意

build.gradle
task updateVersion {
    doFirst {
        def pattern = "^(\\d+).(\\d+).(\\d+)"
        def versionPropsFile = file('./app/version.properties')
        def Properties versionProps = new Properties()
        versionProps.load(new FileInputStream(versionPropsFile))

        def currentName = versionProps['VERSION_NAME']
        def versionNames = (currentName =~ pattern)[0]
        def major = versionNames[1].toInteger()
        def minor =  versionNames[2].toInteger()
        def patch = versionNames[3].toInteger()
        def updateName = major + "." + minor + "." + (patch + 1)   // Default
        if (project.hasProperty("type")) {
            def type = project.property("type")
            if (type == "minor") {
                updateName = major + "." + (minor + 1) + ".0"
            } else if (type == "major") {
                updateName = (major + 1) + ".0.0"
            }
        }
        def updateCode = versionProps['VERSION_CODE'].toInteger() + 1

        println("Complete VersionName : " + updateName.toString())
        println("Complete VersionCode : " + updateCode.toString())

        versionProps['VERSION_NAME'] = updateName.toString()
        versionProps['VERSION_CODE'] = updateCode.toString()
        versionProps.store(versionPropsFile.newWriter(), null)
    }
}

パッチバージョンをあげる場合(デフォルト)

./gradlew updateVersion

マイナーバージョンををあげる場合

./gradlew updateVersion -p type=minor

メジャーバージョンををあげる場合

./gradlew updateVersion -p type=major

あとはCIでこれらをbuildの前にタスク実行して、コミットしてbuildすればOK

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?