美文网首页Android知识Android技术知识Android开发
【译文】配置 Android 项目系列2——版本号

【译文】配置 Android 项目系列2——版本号

作者: bincn | 来源:发表于2017-03-15 17:21 被阅读106次

此博停更,博客新地址

说明:由于翻译水平有限,可能会存在一些不恰当的地方,欢迎指出,我会马上改正。

本文是配置 Android 项目系列的一部分:

  1. Little Things That Matter
  2. Version Name & Code
  3. Static Code Analyses Tools
  4. Continuous Integration

我们在这篇文章中讨论的一切都可以在 template 项目中找到

版本号

开发人员通常使用硬编码指定 Android 的 versionName 和 versionCode。

defaultConfig {
    ...
    versionCode 1
    versionName "1.0.0"
}

这种方法有几个缺点:

  • 你永远不知道哪个 commit 代表一个特定的版本。
  • 每当你增加 versionCode 和修改 versionName,你必须要修改 build.gradle 文件。

如果你使用 git 作为你的源代码控制系统,它也可以帮助你生成 Android versionName 和 versionCode。通常的做法是使用 git tags 标示出新发布的版本。

git_tags.png

Version Name

我们可以使用 git describe 命令描述 versionName。

a. 该命令从一个 commit 中找到可以访问的最新的标签。

b. 如果该标签指向一个 commit,则只显示该标签。

c. 否则,它将标记名称与标记对象顶部的附加提交数以及最近提交的缩写对象名称后缀。

Example (a-b)

git_describe.png
  1. 用标签 1.0 标识特定的 commit
  2. checkout 这个 commit
  3. 调用 git describe -tags
  4. 输出:1.0

正如你所看见的,如果你在一个 HEAD commit 中使用 git describe 加上一些标签,它会输出这个标签。

Example (a-c)

git_describe_2.png
  1. 用标签 1.0 标识一个 commit
  2. 增加两个 commit
  3. 调用 git describe -tags
  4. 输出:1.0-2-gdca226a
git_describe_3.png

使用 git commit 的哈希值 “1.0–2-gdca226a”,我们可以很容易的找到建立了哪个特定的 commit。

git_hash.png

Version Code

对于 versionCode 我们可以使用标签总数。因为每一个 git tag 标识一些版本,下一个版本的 versionCode 永远大于前一个的。

git_code.png

在上面的例子中有3个标签。这个值可以用来指定 versionCode。

但是我们不会为每一个中间版本创建 git tags,所以在开发版本中 HEAD commit 我们可以使用时间戳。

git_timestamp.png

在上面的例子中一个 HEAD commit 的时间戳等于1484407970(
UNIX 中自1970年1月1日00:00:00 UTC以来的秒数)。这个值将用于我们的 versionCode。如果你希望把它转换为人类可读的时间,可以使用 currentmillis.com。在我们的例子中是 Sat Jan 14 2017 15:32:50 UTC。

Groovy way to use git

配合 git 一起使用,我建议使用库 grgit。创建以下内容的 script-git-version.gradle 文件。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.ajoberstar:grgit:1.5.0'
    }
}

import org.ajoberstar.grgit.Grgit

ext {
    git = Grgit.open(currentDir: projectDir)
    gitVersionName = git.describe()
    gitVersionCode = git.tag.list().size()
    gitVersionCodeTime = git.head().time
}

task printVersion() {
    println("Version Name: $gitVersionName")
    println("Version Code: $gitVersionCode")
    println("Version Code Time: $gitVersionCodeTime")
}

将它应用到你的 build.gradle 文件中。

apply plugin: 'com.android.application'
apply from: "$project.rootDir/tools/script-git-version.gradle"

检查版本名称和版本号正确地生成,可以使用 gradle 任务 ./gradlew printVersion,输出类似的:

Version Name: 1.0-2-gdca226a
Version Code: 2
Version Code Time: 1484407970

最后在你的 build.gradle 文件中使用变量 gitVersionName,gitVersionCode 和 gitVersionCodeTime。

productFlavors {
    dev {
        versionCode gitVersionCodeTime
        versionName gitVersionName
    }

    prod {
        versionCode gitVersionCode
        versionName gitVersionName
    }
}

运行项目检查应用版本。

app_info.png

这种方法的好处:

  • 不要修改 build.gradle 文件,versionCode 和 versionName 是自动生成的。
  • 你可以很容易地找到已经建立了什么 commit。

Note: 你可以尝试更多的版本名称:包括分支名、时间戳等。

译文

  1. 小事情
  2. 版本号
  3. 静态代码分析工具
  4. 持续集成

相关文章

网友评论

    本文标题:【译文】配置 Android 项目系列2——版本号

    本文链接:https://www.haomeiwen.com/subject/hpxrnttx.html