1

I am using a modified version of this.

This is my code:

    def mAppName = "App"
    def mVersionName = "0.0." //Increase this version manually

    //https://stackoverflow.com/a/23265711
    def versionPropsFile = file('version.properties')
    Properties versionProps = new Properties()
    if (!versionPropsFile.exists()) {
        //Initial version is 0.0.1 (0)
        //With version code 1
        versionProps['VERSION_PATCH'] = "1"
        //Only gets auto incremented on release. So start at 1 (debug)
        versionProps['VERSION_BUILD'] = "0"
        versionProps['VERSION_CODE'] = "0" //Starts at zero gets auto incremented
        versionProps['VERSION_NAME'] = mVersionName
        versionProps.store(versionPropsFile.newWriter(), null)
    }

    def runTasks = gradle.startParameter.taskNames
    def value = 0
    if ('assembleRelease' in runTasks) {
        //todo check task name, before building release builds
        value = 1
    }

    def mFileName = ""
    def mVersionNameComplete = ""

    if (versionPropsFile.canRead()) {
        versionProps.load(new FileInputStream(versionPropsFile))
        versionProps['VERSION_CODE'] = (versionProps['VERSION_CODE'].toInteger() + 1).toString()

        if (versionProps['VERSION_NAME'].equals(mVersionName)) {
            versionProps['VERSION_PATCH'] = (versionProps['VERSION_PATCH'].toInteger() + value).toString()
            versionProps['VERSION_BUILD'] = (versionProps['VERSION_BUILD'].toInteger() + 1).toString()
        } else {
            versionProps['VERSION_PATCH'] = "0"
            versionProps['VERSION_BUILD'] = "0"
            versionProps['VERSION_NAME'] = mVersionName
        }
        mVersionNameComplete = "${versionProps['VERSION_NAME']}${versionProps['VERSION_PATCH']}"
        mFileName = "${mAppName}-${mVersionNameComplete}.apk"

        versionProps.store(versionPropsFile.newWriter(), null)

        defaultConfig {
            minSdkVersion 21
            targetSdkVersion 30
            applicationId "com.test.app"
            versionCode versionProps['VERSION_CODE'].toInteger()
            versionName "${mVersionNameComplete} Build: ${versionProps['VERSION_BUILD']}"
            vectorDrawables.useSupportLibrary = false
            minSdkVersion 21
            targetSdkVersion 30
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            resConfigs "en", "de" // And any other languages you support
            //Required when setting minSdkVersion to 20 or lower
            //multiDexEnabled true
        }
       
//-------------------------------------------------------------------------------------------
        file("../git_commit.bat").text = """
        @echo off
        git add .
        git commit -m "Auto commit. Version name: ${mVersionNameComplete} Build: ${versionProps['VERSION_BUILD']}, Version code: ${versionProps['VERSION_CODE']}"
        git push
        """
        //-------------------------------------------------------------------------------------------

    } else {
        throw new FileNotFoundException("Could not read version.properties!")
    }

    task autoCommit(type: Exec) {
        workingDir '../.'
        commandLine 'cmd', '/c', 'git_commit.bat'
    }

    if ('assembleRelease' in runTasks) {
        applicationVariants.all { variant ->
            variant.outputs.all { output ->
                if (output.outputFile != null && output.outputFile.name.endsWith('.apk')) {
                    outputFileName = mFileName
                }
            }
        }
    }

    task copyApkFiles(type: Copy) {
        from 'build/outputs/apk/release'
        into '../apk'
        include mFileName
    }

    afterEvaluate {
        assembleRelease.doLast {
            tasks.copyApkFiles.execute()
        }
    }

    //build.finalizedBy(autoCommit)

What I want is that

 task autoCommit(type: Exec) {
        workingDir '../.'
        commandLine 'cmd', '/c', 'git_commit.bat'
    }

This task gets automatically executed on every compilation. Sadly build.finalizedBy(autoCommit) does not work. What is the trick here?

(I managed to execute the task on every build using the 'Run/Debug configuration -> Before launch' section. But doing so executes the gradle part that increases my version number twice. That way my commit is automatically pushed but has the version number of the compilation AFTER the current one)

Avinta
  • 678
  • 1
  • 9
  • 26

0 Answers0