[Gradle] Manage versionName/versionCode dynamically

660 views
Skip to first unread message

Kloon

unread,
Sep 7, 2013, 6:02:17 AM9/7/13
to adt...@googlegroups.com
Hi,

I'm desperately trying to parse the svn revision number into the versionName/versionCode by using a mixture of gradle and ant build scripts (since there's no svn plugin for gradle yet). I'm a complete newbie to gradle and even groovy, but have managed to accomplish the desired functionality by overwriting the needed fields in the source AndroidManifest.xml file and just revert the fields back to their original content after build. This really is an overkill since we have the versionName/versionCode property in defaultConfig, which is supposed to override the values provided by the manifest file. However I cannot accomplish this by defining a function as mentioned in the user guide. Even a simple return value will not work:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android'

ant.importBuild 'build.xml' 

def getVersionName() {
    return "2.0.0"
}

def getVersionCode() {
    return 2
}

android {
    compileSdkVersion 17
    buildToolsVersion "18.0.1"

    defaultConfig {
minSdkVersion 14
        targetSdkVersion 17
versionName getVersionName()
versionCode getVersionCode()
    }
}
... 

I really have no idea how to get this to work. Also, how to execute the ant task and have its property set as the versionCode? Would this work?

def getVersionCode() {
tasks.getSvnRev.execute() // run the ant task to get the local svn revision
return tasks.getSvnRev.revisionVersion // revisionVersion is the ant property containing the revision number
} 

Thanks in advance,
Kloon


Xavier Ducrohet

unread,
Sep 11, 2013, 5:16:59 PM9/11/13
to adt...@googlegroups.com
The reason this is not working is that getVersionCode() and getVersionName() conflicts with the getters of the same name on the defaultConfig object! Rename them to something else and it'll work.


--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Xavier Ducrohet
Android SDK Tech Lead
Google Inc.
http://developer.android.com | http://tools.android.com

Please do not send me questions directly. Thanks!

Kloon

unread,
Sep 16, 2013, 4:49:13 AM9/16/13
to adt...@googlegroups.com
Ah, lol, would've never guessed that! I was just following the user guide, seems that have also been corrected since then.

Cheers,
Kloon
Reply all
Reply to author
Forward
0 new messages