title: Android Studio Gradle programming
body: <p>Okay, I've watched the YouTube video with Xavier Ducrohet on the new <a href="https://developers.google.com/events/io/sessions/325236644">Android build system</a>.  I've even switched to using Android Studio and am happy with it.  Now I need to customize the build rules to do things the way I want, and one of which is automatically setting the <code>codeVersion</code> and <code>codeName</code> in the manifest file.</p>
  
  <p>Xavier show the start of how to do this in one of his slides:</p>
  
  <pre><code>def getVersionCode() {
      def code = ...
      return code
  }
  
  android {
      defaultConfig {
          versionCode getVersionCode()
      }
  }
  </code></pre>
  
  <p>So could some one be so kind as to point me to good resource for filling in the dots?</p>
  
  <p>To be more specific I want to run a script like <code>git describe --dirty | sed -e 's/^v//'</code> to determine the <code>versionName</code> and <code>git tag | grep -c ^v</code> to get the <code>versionCode</code>.</p>
  
  <p>Thanks</p>
  
  <p><strong>Update</strong></p>
  
  <p>I've tried the following <code>gradle.build</code> script without success.  It builds just fine but the version name in the App Info page of my installed apps doesn't change.</p>
  
  <pre><code>task getVersionName(type:Exec) {
    commandLine '../scripts/version-name.sh'
  
    //store the output instead of printing to the console:
    standardOutput = new ByteArrayOutputStream()
  
    //extension method stopTomcat.output() can be used to obtain the output:
    ext.output = {
      return standardOutput.toString()
    }
  }
  
  buildscript {
      repositories {
          maven { url 'http://repo1.maven.org/maven2' }
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:0.4'
      }
  }
  apply plugin: 'android'
  
  dependencies {
      compile project(':Common')
  }
  
  android {
      compileSdkVersion 17
      buildToolsVersion "17.0.0"
  
      defaultConfig {
          minSdkVersion 7
          targetSdkVersion 16
  
          versionName getVersionName()
      }
  }
  </code></pre>
  
  <p>If I replace the config <code>versionName getVersionName()</code> with <code>versionName 'Some Text'</code> then it works and the build name becomes <code>Some Text</code> in the App Info.  So why doesn't my getVersionName function work?</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>Still not working - but almost!</p>
  
  <p>Shell script:</p>
  
  <pre><code>#/bin/bash
  
  NAME=`git describe --dirty | sed -e 's/^v//'`
  COMMITS=`echo ${NAME} | sed -e 's/[0-9\.]*//'`
  
  if [ "x${COMMITS}x" = "xx" ] ; then
  
      VERSION="${NAME}"
  
  else
  
      BRANCH=" (`git branch | grep "^\*" | sed -e 's/^..//'`)"
      VERSION="${NAME}${BRANCH}"
  
  fi
  
  logger "Build version: ${VERSION}"
  
  echo ${VERSION}
  </code></pre>
  
  <p>This works, and the log line confirms that the script is called multiple times when making the project.  But the versionName is still being blank.  I suspect that it is the Gradle side of things that is still not getting stdout.</p>
  
  <pre><code>task getVersionCode(type: Exec) {
      exec { commandLine '../scripts/version-code.sh' }
  
      //store the output instead of printing to the console:
      standardOutput = new ByteArrayOutputStream()
  
      ext.output = {
          return standardOutput.toString()
      }
  }
  
  task getVersionName(type: Exec) {
      exec { commandLine '../scripts/grMobile/scripts/version-name.sh' }
  
      //store the output instead of printing to the console:
      standardOutput = new ByteArrayOutputStream()
  
      ext.output = {
          return standardOutput.toString()
      }
  }
  
  buildscript {
      repositories {
          maven { url 'http://repo1.maven.org/maven2' }
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:0.4'
      }
  }
  apply plugin: 'android'
  
  dependencies {
      compile project(':Common')
  }
  
  android {
      compileSdkVersion 17
      buildToolsVersion "17.0.0"
  
      defaultConfig {
          minSdkVersion 7
          targetSdkVersion 16
  
          versionCode getVersionCode()
          versionName getVersionName.output()
      }
  }
  </code></pre>