Reputation: 1673
How do I add the subversion revision number to an Android APK file using Gradle? Ex. AppName-1.1.123.apk, where 123 is the subversion revision number. I am using Android studio.
Thanks Markus
Upvotes: 1
Views: 2917
Reputation: 2642
The code of 'pyus13' and 'Markus K' gets the current svn revision of the working copy. This is for most cases ok. But if you want to get the last revision of the server (which is may be greater as the revision of your working copy) you must get the SVN revision from the server and not from your working copy:
import org.tmatesoft.svn.core.wc.*
import org.tmatesoft.svn.core.*
import org.tmatesoft.svn.core.io.*
import org.tmatesoft.svn.core.auth.*
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
...
project.ext {
svnUser = 'user'
svnPwd = 'topsecret'
}
...
/****************************************************************************
* Add SVN revision number to version
****************************************************************************/
buildscript {
repositories {
mavenCentral()
}
dependencies {
// needed for getting current SVN rev nmbr
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.14'
}
}
/** Gets the current subversion revision number.
*/
def getSvnRevision() {
// init needed stuff for connecting SVN server
DAVRepositoryFactory.setup();
SVNRepositoryFactoryImpl.setup();
FSRepositoryFactory.setup();
// getting SVN Url from local working copy ...
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options);
SVNStatusClient statusClient = clientManager.getStatusClient();
SVNStatus status = statusClient.doStatus(projectDir, false);
SVNURL url = status.getRepositoryRootURL();
// connecting SVN server and getting last revision number
SVNRepository repository = SVNRepositoryFactory.create(url);
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(svnUser, svnPwd);
repository.setAuthenticationManager(authManager);
return repository.getLatestRevision();
}
...
Upvotes: 0
Reputation: 25858
Add this dependency classpath in your root level build.gradle file's dependencies closure
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.7.11'
It will look like this
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.7.11'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Now its time to make changes in your module's build.gradle file
import org.tmatesoft.svn.core.wc.*
apply plugin: 'com.android.application'
android {
//YOUR OTHER CONFIGURATION
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(
output.outputFile.parent,
output.outputFile.name.replace(".apk", "-${variant.versionName}."+ getSvnRevision() + ".apk"))
}
}
}
dependencies {
//YOUR DEPENDENCIES HERE
}
def getSvnRevision(){
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options);
SVNStatusClient statusClient = clientManager.getStatusClient();
SVNStatus status = statusClient.doStatus(projectDir, false);
SVNRevision revision = status.getRevision();
return revision.getNumber();
}
I don't use SVN these days, but I have complied and run this script in my local environment with hardcoded value and it works fine for me. Let me know it works for you too.
Upvotes: 4
Reputation: 1673
Here is the code that I put in the "root" build.gradle file that solved it:
import org.tmatesoft.svn.core.wc.*
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.7.11'
}
}
def getSvnRevision(){
ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options);
SVNStatusClient statusClient = clientManager.getStatusClient();
SVNStatus status = statusClient.doStatus(projectDir, false);
SVNRevision revision = status.getRevision();
return revision.getNumber();
}
allprojects {
version = '1.2.3.' + getSvnRevision()
}
Upvotes: 3