6

I have some simple android application for internal use of the company. I would like to create a simple version tracking. So to implement it I added some code to my build.gradle file:

apply plugin: 'com.android.application'
   buildscript {
      repositories {
         jcenter()
         maven {
           url "https://plugins.gradle.org/m2/"
         }
    }
    dependencies {
       classpath "com.github.ben-manes:gradle-versions-plugin:0.12.0"
       classpath 'org.moallemi.gradle.advanced-build-version:gradle-pugin:1.5.3'
   }
apply plugin: 'org.moallemi.advanced-build-version'
apply plugin: "com.github.ben-manes.versions"

advancedVersioning {
   outputOptions {
      renameOutput true
      nameFormat '${projectName}-${versionCode}'
   }
}

All I want to do is to set versionCode to current date (long). How can I do it? And Is it a correct direction?

Thanks in advance!

Ilia S.
  • 780
  • 7
  • 19

3 Answers3

5

I'm doing the following to have current date as the versionCode in the Android APK. However, that means you can't publish multiple times a day to Google Play, so I'd recommend using "yyyyMMdd00" as the format string, and to temporarily increment the 00 for hotfix pushes.

def versionCodeDate() {
    return new Date().format("yyyyMMdd").toInteger()
}

android {
    defaultConfig {
        versionCode versionCodeDate()
    }
}

This was inspired by https://stackoverflow.com/a/30639651/539443

ge0rg
  • 1,816
  • 1
  • 26
  • 38
0

There is a powerful gradle plugin on github called gradle-advanced-build-version it is very simple and will let you auto generate version-code based on current datetime

After installing the plugin you can use the below code to automatically assign versioncode to current date:

    defaultConfig { 
      advancedVersioning {
            nameOptions {
                versionMajor 4
                versionMinor 5
                versionPatch 6
                versionBuild 7
            }
            codeOptions {
                versionCodeType VersionCodeType.DATE
            }
        }
        versionName advancedVersioning.versionName
        versionCode advancedVersioning.versionCode
    }
Melad
  • 1,184
  • 14
  • 18
  • Melad, please don't just post some tool / library or plugin as an answer. At least demonstrate [how it solves the problem](http://meta.stackoverflow.com/a/251605) in the answer itself. – 4b0 May 28 '19 at 10:59
-5

If you want to set the versionCode just add it in your build.gradle(Module: app) file:

defaultConfig {
  versionCode 1 //Set the number you want
}
Itiel Maimon
  • 834
  • 2
  • 9
  • 26