Okay, I've watched the YouTube video with Xavier Ducrohet on the new Android build system. 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 codeVersion
and codeName
in the manifest file.
Xavier show the start of how to do this in one of his slides:
def getVersionCode() {
def code = ...
return code
}
android {
defaultConfig {
versionCode getVersionCode()
}
}
So could some one be so kind as to point me to good resource for filling in the dots?
To be more specific I want to run a script like git describe --dirty | sed -e 's/^v//'
to determine the versionName
and git tag | grep -c ^v
to get the versionCode
.
Thanks
Update
I've tried the following gradle.build
script without success. It builds just fine but the version name in the App Info page of my installed apps doesn't change.
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()
}
}
If I replace the config versionName getVersionName()
with versionName 'Some Text'
then it works and the build name becomes Some Text
in the App Info. So why doesn't my getVersionName function work?
Update 2
Still not working - but almost!
Shell script:
#/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}
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.
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()
}
}
Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.
Gradle is a build automation tool for multi-language software development. It controls the development process in the tasks of compilation and packaging to testing, deployment, and publishing. Supported languages include Java (as well as Kotlin, Groovy, Scala), C/C++, and JavaScript.
Gradle is a build system (open source) that is used to automate building, testing, deployment, etc. “build. gradle” are scripts where one can automate the tasks.
After hunting around I finally found a solution for this.
Groovy, the language of the build.gradle
file, allows commands to be easily run. Here is the solution:
def buildCode
= file("../scripts/version-code.sh")
.toString().execute().text.trim().toInteger()
def buildName
= file("../scripts/version-name.sh")
toString().execute().text.trim()
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 12
targetSdkVersion 16
versionCode buildCode
versionName buildName
}
}
file() should be used to reference all file within Gradle.
"<some-command".execute()
will run the command, and .text
gives you simple access to
stdout
. I found I needed to run trim()
to remove the trailing carriage return. I suppose I could have used echo -n ${VERSION}
in my script, but I think the trim()
method is better as it allows the script to be run from the command line.
The build script just counts the number of release tags from git. As I tag my releases in the form: 'v' <major-no> '.' <minor-no> [ '.' <bug-fix> ]
it just could the tags that start with a lower case 'v' followed by any digit:
#/bin/bash
git tag | grep -c ^v[0-9]
Before you build with this configuration don't forget to create at least one release tag. I tag right at the start of the project in the following way:
$ git tag -m "Start of development" v0.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With