【问题标题】:How to automate app versioning in Android如何在 Android 中自动化应用程序版本控制
【发布时间】:2018-05-19 03:56:30
【问题描述】:

我有一个 CI 工具 (Bamboo),它运行我的单元测试并构建 app-release.apk。每当我在我的应用程序中进行更改并成功构建时,我都会创建一个新的版本号。我将版本号和构建时间存储在一个文件中。我可以让 Gradle 读取该文件并相应地更改应用版本吗?

我的文件内容如下所示:

ProductVersion=0.0.0.0.0
ProductBuildDateTime=2017-01-01T00:00:00.000

【问题讨论】:

    标签: android gradle versioning


    【解决方案1】:

    是的,当然,我就是这样做的:

    我将我的版本存储在 version.properties 文件中。

    然后在应用程序的 build.gradle 中使用以下代码:

    //Path to the file of your properties file that contains the version number
    final String VERSION_PROPERTIES_FILE = projectDir.getParentFile().getParent() + "/file.properties"
    
     /**
     * Returns the id of the property name from config file, null if it doesn't exist
         * @param propertyName
         * @param properties file name in assets dir
         * @return
         */
        def getPropertyFromFile(propertiesFilePath, propertyName) {
            def propsFile = new File(propertiesFilePath);
            if (propsFile.canRead()) {
                def props = new Properties();
                props.load(new FileInputStream(propsFile));
                return props.getProperty(propertyName, null);
            } else {
                throw new GradleException("Could not read $propertiesFilePath !")
            }
        }
    
    int vCode = getPropertyFromFile(VERSION_PROPERTIES_FILE, "versionCode") as Integer
    String vName = getPropertyFromFile(VERSION_PROPERTIES_FILE, "versionName")
    
    defaultConfig {
        applicationId "com.x.x"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode vCode
        versionName vName
    }
    

    在您的情况下,您可以将 "ProductBuildDateTime""ProductVersion" 传递给读取文件的方法。

    【讨论】:

    • 你没有关闭文件流,所以 Gradle 守护进程会锁定文件,尤其是在 Windows 上。你最好做类似new File(path).withInputStream { properties.load it } 的事情,它会自动处理流的创建和关闭。这类似于 Java 中的 try-with-resources。
    【解决方案2】:

    是的

    如果您的意思是如何做到这一点,那就简单地做类似

    ....def 属性 = 新属性()
    ....def 文件 = 新文件('version.properties')
    ....file.withInputStream {
    ........属性。加载它
    ....}
    ....properties.ProductVersion = ...
    ....properties.ProductBuildDateTime = ...
    ....file.withOutputStream {
    ........properties.store 它,真的
    ....}

    【讨论】:

    • 你是真正的吸血鬼!
    【解决方案3】:

    如果您想从文件中读取构建信息,您可以使用给定答案的更简化版本,如下所示:

    def getCIProperty(String key)
    {
        Properties properties = new Properties()
        properties.load( file("Bamboo File Absolute Path").readLines().join("\n"))
        properties.getProperty(key)
    }
    
    getCIProperty('ProductVersion')
    getCIProperty('ProductBuildDateTime')
    

    只需将其复制并粘贴到您的主构建文件中,而不是 settings.gradle 文件中

    或者如果你使用的是竹子,你可以通过让竹子在构建命令中传递这些属性来更容易地做到这一点:

    将这些代码复制并粘贴到构建脚本中的任何位置(甚至在 settings.gradle 中)

    gradle.productVersion = getProperty('ProductVersion')
    gradle.productBuildDateTime = getProperty('ProductBuildDateTime')
    

    现在只需要求竹子将这两个参数添加到构建命令中,如下所示:

    gradle -PProductVersion=0.0.0.0.0   -PProductBuildDateTime=2017-01-01T00:00:00.000   clean   assembleRelease
    

    提示:clean 和 assembleRelease 是构建脚本中的任务,你可以调用任何你想要的任务。

    【讨论】:

      猜你喜欢
      • 2012-07-06
      • 1970-01-01
      • 2017-07-17
      • 2020-02-27
      • 2011-03-23
      • 2014-03-25
      • 2015-04-12
      • 2019-04-11
      • 1970-01-01
      相关资源
      最近更新 更多