Android Studio Gradle Programming

Well, I watched a video on YouTube with Xavier Ducroet in the new Android build system. I even switched to using Android Studio and am happy with it. Now I need to configure the assembly rules to do what I want, and one of them automatically sets codeVersion and codeName in the manifest file.

Xavier shows the beginning of how to do this in one of his slides:

 def getVersionCode() { def code = ... return code } android { defaultConfig { versionCode getVersionCode() } } 

So can anyone be so kind as to point me to a good resource for filling out the points?

To be more specific, I want to run a script as git describe --dirty | sed -e 's/^v//' git describe --dirty | sed -e 's/^v//' to specify versionName and git tag | grep -c ^v git tag | grep -c ^v to get versionCode .

thanks

Update

I tried the following gradle.build script with no success. It works fine, but the version name on the application details page of my installed applications does not 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 replaced config versionName getVersionName() with versionName 'Some Text' , then it will work and the assembly name will become Some Text in the Info application. So why is my getVersionName function not working?

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 several times when creating the project. But versionName is still empty. I suspect this is the Gradle side 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() } } 
+8
android-studio build.gradle gradle
source share
2 answers

After the hunt, I finally found a solution for this.

Groovy, the language of the build.gradle file, makes it easy to run commands. 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 the entire file in Gradle.

"<some-command".execute() will execute the command, and .text will give you easy access to stdout . I found that I need to run trim() to remove the returned carriage return. I suppose I could use echo -n ${VERSION} in my script, but I think the trim() method is better because it allows the script to be run from the command line.

The script line simply counts the number of release tags from git. As I mark my releases in the form: 'v' <major-no> '.' <minor-no> [ '.' <bug-fix> ] 'v' <major-no> '.' <minor-no> [ '.' <bug-fix> ] 'v' <major-no> '.' <minor-no> [ '.' <bug-fix> ] , it can only be those tags that begin with the lower case "v", followed by any digit:

 #/bin/bash git tag | grep -c ^v[0-9] 

Before creating this configuration, be sure to create at least one release tag. I mark at the beginning of the project as follows:

 $ git tag -m "Start of development" v0.0 
+2
source share

You missed comments on the sample. versionName getVersionName.output() should work.

EDIT: change the task code to the following.

 task getVersionName(type:Exec) { exec { commandLine '../scripts/version-name.sh' } ... } 

I'm also new to gradle, it seems like they have some errors or no documentation. Since the code you tried is exactly from the sample available in the document.

0
source share

All Articles