Auto versioning my builds (Android-Studio)


(Steve Dobson) #1

I’m new to Gradle, having be introduced to it now I’ve switched to using the new build system for Android.

In converting to this new build I am having a problem getting my version script to work with Gradle.

The script looks like this:

#/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}

I know it runs each time Gradle builds because I can see the “logger” out in the syslog.

My problem is trying to get the stdout from the script applied to the versionName in the build.gradle script.

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

versionName getVersionName.output()

}

}

The string that gets report when the application is installed on an Android device is blank. So how do I get the output from my script to be returned by the getVersionName task?


(Luke Daley) #2

Are you sure that the ‘android.versionName’ property is not blank? i.e. that your script is actually returning the right thing?


(Steve Dobson) #3

Yes, I’m sure. I log the value I echo back to the system logger (on Linux) and that I see.

Over the weekend I found another way of doing it:

def buildCode = file("…/scripts/version-code.sh").toString().execute().text.trim().toInteger()

def buildName = file("…/scripts/version-name.sh").toString().execute().text.trim()

But it would be nice to understand how to get the stdout from the task.


(Luke Daley) #4

When does this task get run?

It may be being executed after the Android plugin is reading that value from the ‘android.versionNumber’ property. You’ll need to either look at the android plugin source code, or ask on the adt-dev list about this.


(Steve Dobson) #5

To be honest I don’t know when the script is run, relative to the ‘android.versionNumber’ property being read. I do know it was late on in the hold build processes. I’ll have to check that out.

Thanks