如何使用 Gradle 将 svn 版本号添加到 Android APK?

How to add svn rev number to Android APK using Gradle?

如何使用 Gradle 将颠覆版本号添加到 Android APK 文件?前任。 AppName-1.1.123.apk,其中 123 是颠覆版本号。我正在使用 Android 工作室。

谢谢 马库斯

这是我放入 "root" build.gradle 文件中的代码:

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()
}

在您的根级别添加此依赖类路径build.gradle 文件的依赖闭包

classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.7.11'

看起来像这样

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
    }
}

现在是时候更改模块的 build.gradle 文件了

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();
}

这些天我不使用 SVN,但我已经在我的本地环境中使用硬编码值编译并 运行 这个脚本,它对我来说工作正常。让我知道它也适用于你。

'pyus13'和'Markus K'的代码获取工作副本的当前svn版本。这在大多数情况下都可以。但是如果你想获得服务器的最新版本(它可能比你的工作副本的版本更大)你必须从服务器而不是你的工作副本获得 SVN 版本:

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();
}

...