1

I would like to use the Subversion revision number as an additional specifier of the Version of my App. I am working with Android Studio, therefore I am using Gradle.
I did not find a friendly way to access the Subversion revision and then read it from code.

It would be fantastic if the SVN revision number could be added to BuildConfig class generated by Gradle.

Matteo
  • 482
  • 3
  • 11

1 Answers1

2

I made it work using mostly code from this comment: https://stackoverflow.com/a/25872141/5132130

I added the following to the top of the gradle file:

def getSvnRevisionNr() {
    description 'Get SVN revision number.'
    new ByteArrayOutputStream().withStream { os ->
        def result = exec {
            executable = 'svnversion'
            standardOutput = os
        }
        return '"' + os.toString().trim() + '"'
    }
}

The changes I made in relation to the comment I linked:

  • I wanted a result that could be used for the String, so I needed to add the '"' parts and the trim()
  • I returned it as is, without storing in variable inside method

And in my buildTypes:

buildTypes {
    debug {
        minifyEnabled false
        buildConfigField("String", "SVN_REVISION", getSvnRevisionNr())
    }
    release {
        minifyEnabled false
        buildConfigField("String", "SVN_REVISION", getSvnRevisionNr())

    }
}

Then in my code i can access it using:

BuildConfig.SVN_REVISION
Community
  • 1
  • 1
Julius
  • 159
  • 1
  • 1
  • 13