versioncode 修改android_Android 项目配置之 Version Name & Code

简评:优雅的设置 Android 应用的 versionCode & Name。

Android 开发者一定对下面的这两个属性很熟悉吧:

defaultConfig {

...

versionCode 1

versionName "1.0.0"

}

但直接这样设置有两个不好的地方:不知道这个 version 对应的是哪一个 commit。

每次修改 versionCode 和 versionName,都要更新 build.gradle 文件。

如果你使用 git 作为你的版本控制工具,这篇文章就可以帮助你快捷设置 versionName & versionCode。

Version Name对于 versionName 我们可以用 git describe 命令。

a. git describe会从当前 commit 找到最近的一个 tag。

b. 如果这个 tag 就指向当前 commit,那就直接输出 tag。

c. 否则输出之前的 tag + 中间间隔的 commit 数 + 当前的 commit ID。比如:1.0-2-gdc226a

Version Code

versionCode是一个数字,通常每一个 git tag 对应一个 version。

当然,对于开发中的内部版本是没有必要每个都打 tag 的,这时我们可以用时间戳来作为 versionCode。

因此,可以创建一个 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"

productFlavors {

dev {

versionCode gitVersionCodeTime

versionName gitVersionName

}

prod {

versionCode gitVersionCode

versionName gitVersionName

}

}Version Name: 1.0-2-gdca226a

Version Code: 2

Version Code Time: 1484407970

这样来设置 versionCode 和 versionName 就可以很方便的分辨出每个版本对应的 commit,也不用每次都手动去改 build.gradle 文件了。

相关阅读:

欢迎关注:知乎专栏「极光日报」,每天为 Makers 导读三篇优质英文文章。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,Android的build.gradle文件可以读取YAML文件,并将其中的内容用作变量进行配置。以下是一个完整的例子,可以帮助您实现这一目标: 1. 首先,您需要在您的项目根目录下创建一个名为config.yaml的文件,并在其中定义您需要的变量。例如,您可以在config.yaml文件中定义以下内容: ``` # config.yaml app_name: "My Awesome App" version_code: 1 version_name: "1.0.0" ``` 2. 接下来,您需要在build.gradle文件中添加以下代码,以读取config.yaml文件并将其中的内容用作变量: ``` // build.gradle import org.yaml.snakeyaml.Yaml def config = new File("${rootDir}/config.yaml") def yaml = new Yaml().load(config.text) android { defaultConfig { applicationId "com.example.myawesomeapp" minSdkVersion 21 targetSdkVersion 30 versionCode yaml.version_code versionName yaml.version_name } } // You can also use the variables in other parts of your build.gradle file task printAppName { doLast { println "The app name is: ${yaml.app_name}" } } ``` 在上面的代码中,我们使用YAML库来读取config.yaml文件的内容,并将其存储在yaml变量中。然后,我们可以在build.gradle文件的其他部分使用这些变量,例如在defaultConfig块中设置versionCodeversionName属性。 3. 最后,在您的命令行终端中运行以下命令,以确保您的项目可以正确读取config.yaml文件: ``` ./gradlew printAppName ``` 这将运行我们在build.gradle文件中定义的printAppName任务,并输出应用程序名称(即我们在config.yaml文件中定义的“My Awesome App”)。如果输出正确,那么您的项目已经可以正确读取config.yaml文件中的内容,并将其用作变量进行配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值