title: Autoincrement VersionCode with gradle extra properties
body: <p>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 depending if it is the release flavor or the debug flavor increase the versionCode. I tried the extra properties, but you can't save them, which means that next time I build it I'm getting the same versionCode.
  Any help would be very much appreciated!</p>
  
  <pre><code>project.ext{
      devVersionCode = 13
      releaseVersionCode = 1
  }
  
  buildscript {
      repositories {
          mavenCentral()
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:0.6.+'
      }
  }
  
  apply plugin: 'android'
  
  repositories {
      mavenCentral()
  }
  
  dependencies {
      compile project(':Cropper')
      compile "com.android.support:appcompat-v7:18.0.+"
      compile "com.android.support:support-v4:18.0.+"
      compile fileTree(dir: 'libs', include: '*.jar')
  }
  
  def getReleaseVersionCode() {
      def version = project.releaseVersionCode + 1
      project.releaseVersionCode = version
      println sprintf("Returning version %d", version)
      return version
  }
  
  def getDevVersionCode() {
      def version = project.devVersionCode + 1
      project.devVersionCode = version
      println sprintf("Returning version %d", version)
      return version
  }
  
  
  def getLastVersioName(versionCode) {
      return "0.0." + versionCode
  }
  
  android {
      compileSdkVersion 19
      buildToolsVersion "19.0.0"
  
      defaultConfig {
          minSdkVersion 9
          targetSdkVersion 19
      }
  
      sourceSets {
          main {
              manifest.srcFile 'AndroidManifest.xml'
              java.srcDirs = ['src']
              resources.srcDirs = ['src']
              aidl.srcDirs = ['src']
              renderscript.srcDirs = ['src']
              res.srcDirs = ['res']
              assets.srcDirs = ['assets']
          }
      }
  
      buildTypes {
          release {
              runProguard true
              proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
              proguardFile 'proguard.cfg'
              debuggable false
              signingConfig null
              zipAlign false
          }
          debug {
              versionNameSuffix "-DEBUG"
          }
      }
      productFlavors {
          dev {
              packageName = 'com.swisscom.docsafe.debug'
              versionCode getDevVersionCode()
              versionName getLastVersioName(project.devVersionCode)
          }
          prod {
              packageName = 'com.swisscom.docsafe'
              versionCode getReleaseVersionCode()
              versionName getLastVersioName(project.releaseVersionCode)
          }
      }
  }
  
  task wrapper(type: Wrapper) {
      gradleVersion = '1.8'
  }
  </code></pre>