简体   繁体   English

如何在Android中自动执行应用版本控制

[英]How to automate app versioning in Android

I have a CI tool(Bamboo) which runs my Unit Tests and builds the app-release.apk. 我有一个CI工具(Bamboo),可运行我的单元测试并构建app-release.apk。 Whenever I make changes in my app and the build in successful I create a new Version number.I store the version number and build time in a file. 每当我在应用程序中进行更改并成功构建时,我都会创建一个新的版本号,并将版本号和构建时间存储在文件中。 Can I make Gradle to read from that file and change the app version accordingly? 我可以让Gradle读取该文件并相应地更改应用程序版本吗?

Content of my file looks like this: 我文件的内容如下所示:

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

Yes of course, this is how I do it: 是的,当然,这是我的方法:

I store my versions in a version.properties file. 我将版本存储在version.properties文件中。

Then in the app's build.gradle I use the following code: 然后在应用程序的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
}

In your case, you could pass "ProductBuildDateTime" and "ProductVersion" to the method that reads the file. 在您的情况下,可以将"ProductBuildDateTime""ProductVersion"传递给读取文件的方法。

Yes

If you meant how to do it, well simply do something like 如果您打算如何做,那么只需执行类似

....def properties = new Properties() .... def属性=新的Properties()
....def file = new File('version.properties') .... def文件=新文件('version.properties')
....file.withInputStream { .... file.withInputStream {
........properties.load it ........ properties。加载它
....} ....}
....properties.ProductVersion = ... .... properties.ProductVersion = ...
....properties.ProductBuildDateTime = ... .... properties.ProductBuildDateTime = ...
....file.withOuputStream { .... file.withOuputStream {
........properties.store it, true ........ properties.store it,true
....} ....}

if you want to read build info from a file you can use a more simplified version of the given answers like this : 如果您想从文件中读取构建信息,则可以使用给定答案的更简化版本,如下所示:

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')

Just copy and paste it into your main build file , not the settings.gradle file 只需将其复制并粘贴到您的主构建文件中,而不是将其粘贴到settings.gradle文件中即可

or if you are using bamboo , you can do it more easily by just asking the bamboo to pass those properties in the build command like this: 或者,如果您使用的是Bamboo,则只需让Bamboo在build命令中传递这些属性,就可以更轻松地做到这一点:

Copy and past these codes anywhere in your build script (even in settings.gradle) 将这些代码复制并粘贴到构建脚本中的任何位置(甚至在settings.gradle中)

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

and now simply ask bamboo to add these two arguments to the build command like so: 现在只需让Bamboo将如下两个参数添加到build命令中即可:

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

Tip: clean and assembleRelease are tasks in the build script and you can call any tasks you want . 提示:clean和assembleRelease是构建脚本中的任务,您可以调用所需的任何任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM