Autoincrement VersionCode with gradle extra properties

前端 未结 16 2126
逝去的感伤
逝去的感伤 2020-11-27 08:47

I\'m building an Android app with gradle. Until now I used the Manifest file to increase the versionCode, but I would like to read the versionCode from an external file and

相关标签:
16条回答
  • 2020-11-27 09:27

    Credits to CommonsWare (Accepted Answer) Paul Cantrell (Create file if it doesn't exist) ahmad aghazadeh (Version name and code)

    So I mashed all their ideas together and came up with this. This is the drag and drop solution to exactly what the first post asked.

    It will automatically update the versionCode and versionName according to release status. Of course you can move the variables around to suite your needs.

    def _versionCode=0
    def versionPropsFile = file('version.properties')
    def Properties versionProps = new Properties()
    if(versionPropsFile.exists())
        versionProps.load(new FileInputStream(versionPropsFile))
        def _patch = (versionProps['PATCH'] ?: "0").toInteger() + 1
        def _major = (versionProps['MAJOR'] ?: "0").toInteger()
        def _minor = (versionProps['MINOR'] ?: "0").toInteger()
        List<String> runTasks = gradle.startParameter.getTaskNames();
        def value = 0
        for (String item : runTasks)
            if ( item.contains("assembleRelease")) {
                value = 1;
            }
        _versionCode = (versionProps['VERSION_CODE'] ?: "0").toInteger() + value
        if(_patch==99)
        {
            _patch=0
            _minor=_minor+1
        }
        if(_major==99){
            _major=0
            _major=_major+1
        }
    
    versionProps['MAJOR']=_major.toString()
    versionProps['MINOR']=_minor.toString()
    versionProps['PATCH']=_patch.toString()
    versionProps['VERSION_CODE']=_versionCode.toString()
    versionProps.store(versionPropsFile.newWriter(), null)
    def _versionName = "${_major}.${_versionCode}.${_minor}.${_patch}"
    
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    
    defaultConfig {
        applicationId "com.yourhost.yourapp"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode _versionCode
        versionName _versionName
    }
    
    0 讨论(0)
  • 2020-11-27 09:28

    There are two solutions I really like. The first depends on the Play Store and the other depends on Git.

    Using the Play Store, you can increment the version code by looking at the highest available uploaded version code. The benefit of this solution is that an APK upload will never fail since your version code is always one higher than whatever is on the Play Store. The downside is that distributing your APK outside of the Play Store becomes more difficult. You can set this up using Gradle Play Publisher by following the quickstart guide and telling the plugin to resolve version codes automatically:

    plugins {
        id 'com.android.application'
        id 'com.github.triplet.play' version 'x.x.x'
    }
    
    android {
        ...
    }
    
    play {
        serviceAccountCredentials = file("your-credentials.json")
        resolutionStrategy = "auto"
    }
    

    Using Git, you can increment the version code based on how many commits and tags your repository has. The benefit here is that your output is reproducible and doesn't depend on anything outside your repo. The downside is that you have to make a new commit or tag to bump your version code. You can set this up by adding the Version Master Gradle plugin:

    plugins {
        id 'com.android.application'
        id 'com.supercilex.gradle.versions' version 'x.x.x'
    }
    
    android {
        ...
    }
    
    0 讨论(0)
  • 2020-11-27 09:29

    in the Gradle 5.1.1 version on mac ive changed how the task names got retrieved, i althought tried to get build flavour / type from build but was to lazy to split the task name:

    def versionPropsFile = file('version.properties')
    if (versionPropsFile.canRead()) {
        def Properties versionProps = new Properties()
    
        versionProps.load(new FileInputStream(versionPropsFile))
    
        def value = 0
    
        def runTasks = gradle.getStartParameter().getTaskRequests().toString()
    
        if (runTasks.contains('assemble') || runTasks.contains('assembleRelease') || runTasks.contains('aR')) {
            value = 1
        }
    
        def versionMajor = 1
        def versionMinor = 0
        def versionPatch = versionProps['VERSION_PATCH'].toInteger() + value
        def versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
        def versionNumber = versionProps['VERSION_NUMBER'].toInteger() + value
    
        versionProps['VERSION_PATCH'] = versionPatch.toString()
        versionProps['VERSION_BUILD'] = versionBuild.toString()
        versionProps['VERSION_NUMBER'] = versionNumber.toString()
    
        versionProps.store(versionPropsFile.newWriter(), null)
    
        defaultConfig {
            applicationId "de.evomotion.ms10"
            minSdkVersion 21
            targetSdkVersion 28
            versionCode versionNumber
            versionName "${versionMajor}.${versionMinor}.${versionPatch} (${versionBuild})"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            signingConfig signingConfigs.debug
        }
    
    } else {
        throw new GradleException("Could not read version.properties!")
    }
    

    code is from @just_user this one

    0 讨论(0)
  • 2020-11-27 09:30

    Another option, for incrementing the versionCode and the versionName, is using a timestamp.

    defaultConfig {
       versionName "${getVersionNameTimestamp()}"
       versionCode getVersionCodeTimestamp()
    }
    
    
    def getVersionNameTimestamp() {
        return new Date().format('yy.MM.ddHHmm')
    }
    
    def getVersionCodeTimestamp() {
        def date = new Date()
        def formattedDate = date.format('yyMMddHHmm')
        def code = formattedDate.toInteger()
        println sprintf("VersionCode: %d", code)
        return code
    }
    

    Starting on January,1 2022 formattedDate = date.format('yyMMddHHmm') exceeds the capacity of Integers

    0 讨论(0)
  • 2020-11-27 09:32

    Recently I was working on a gradle plugin for Android that makes generating versionCode and versionName automatically. there are lots of customization. here you can find more info about it https://github.com/moallemi/gradle-advanced-build-version

    0 讨论(0)
  • 2020-11-27 09:33

    Create file version.properties

    MAJOR=1
    MINOR=3
    PATCH=6
    VERSION_CODE=1
    

    Change build.gradle :

    android {
    def _versionCode=0
    def _major=0
    def _minor=0
    def _patch=0
    
    def _applicationId = "com.example.test"
    
    def versionPropsFile = file('version.properties')
    
    if (versionPropsFile.canRead()) {
        def Properties versionProps = new Properties()
    
        versionProps.load(new FileInputStream(versionPropsFile))
    
        _patch = versionProps['PATCH'].toInteger() + 1
        _major = versionProps['MAJOR'].toInteger()
        _minor = versionProps['MINOR'].toInteger() 
        _versionCode= versionProps['VERSION_CODE'].toInteger()+1
        if(_patch==99)
        {
            _patch=0
            _minor=_minor+1
        }
        if(_major==99){
            _major=0
            _major=_major+1
        }
    
        versionProps['MAJOR']=_major.toString()
        versionProps['MINOR']=_minor.toString()
        versionProps['PATCH']=_patch.toString()
        versionProps['VERSION_CODE']=_versionCode.toString()
        versionProps.store(versionPropsFile.newWriter(), null)
    }
    else {
        throw new GradleException("Could not read version.properties!")
    }
    def _versionName = "${_major}.${_versionCode}.${_minor}.${_patch}"
    
    
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    
    defaultConfig {
        applicationId _applicationId
        minSdkVersion 11
        targetSdkVersion 23
        versionCode _versionCode
        versionName _versionName
    }
    

    }

    Output : 1.1.3.6

    0 讨论(0)
提交回复
热议问题