目前我有一个React Native应用程序,问题在于每次构建或提交时更新版本非常耗时。此外,我启用了Sentry,因此每次构建时,某些构建获得相同的版本,因此有些崩溃很难确定它们来自何处。最后,手动更新版本容易出错。我应该如何设置我的构建以生成自动版本并忘记所有这些手动任务呢?
虽然目前被接受的答案是可行的,但有更简单、更可靠的方法来实现。
你可以直接从build.gradle
中读取设置在package.json
中的值。
修改你的android/app/build.gradle
文件:
// On top of your file import a JSON parser
import groovy.json.JsonSlurper
// Create an easy to use function
def getVersionFromNpm() {
// Read and parse package.json file from project root
def inputFile = new File("$rootDir/../package.json")
def packageJson = new JsonSlurper().parseText(inputFile.text)
// Return the version, you can get any value this way
return packageJson["version"]
}
android {
defaultConfig {
applicationId "your.app.id"
versionName getVersionFromNpm()
}
}
这样,您将不需要预先构建脚本或其他任何东西,它会自动运行。
这样,您将不需要预先构建脚本或其他任何东西,它会自动工作。
由于我已经花了几天的时间在此工作上,所以我决定与大家分享我的做法,因为这可能会帮助其他人。
使用的工具:
只需要在 app gradle 的末尾添加一行即可。在我的情况下,我已经使用了 Google Play Services gradle,并在其后添加了它。
apply from: 'version.gradle'
该文件应与您的应用gradle文件位于同一文件夹中,内容如下:
task updatePackage(type: Exec, description: 'Updating package.json') {
commandLine 'powershell', ' -command ' , '$semver=(gitversion /showvariable Semver); Set-Content -path version.properties -value semver=$semver; npm version --no-git-tag-version --allow-same-version $semver'
}
preBuild.dependsOn updatePackage
task setVariantVersion {
doLast {
if (plugins.hasPlugin('android') || plugins.hasPlugin('android-library')) {
def autoIncrementVariant = { variant ->
variant.mergedFlavor.versionName = calculateVersionName()
}
if (plugins.hasPlugin('android')){
//Fails without putting android. first
android.applicationVariants.all { variant -> autoIncrementVariant(variant) }
}
if (plugins.hasPlugin('android-library')) {
//Probably needs android-library before libraryVariants. Needs testing
libraryVariants.all { variant -> autoIncrementVariant(variant) }
}
}
}
}
preBuild.dependsOn setVariantVersion
setVariantVersion.mustRunAfter updatePackage
ext {
versionFile = new File('version.properties')
calculateVersionName = {
def version = readVersion()
def semver = "Unknown"
if (version != null){
semver = version.getProperty('semver')
}
return semver
}
}
Properties readVersion() {
//It gets called once for every variant but all get the same version
def version = new Properties()
try {
file(versionFile).withInputStream { version.load(it) }
} catch (Exception error) {
version = null
}
return version
}
这是第一个运行的脚本。让我们来看看它在做什么:
$semver=(gitversion /showvariable Semver);
Set-Content -path props.properties -value semver=$semver;
npm version --no-git-tag-version --allow-same-version $semver
就是这样。现在,每次构建时,版本应该会自动生成,您的package.json已更新,您的构建应该具有特定的版本名称。
由于我正在使用App Center进行构建,因此我将告诉您如何在Build机器中使用此功能。您只需要使用自定义脚本即可。
#!/usr/bin/env sh
#Installing GitVersion
OS=$(uname -s)
if [[ $OS == *"W64"* ]]; then
echo "Installing GitVersion with Choco"
choco install GitVersion.Portable -y
else
echo "Installing GitVersion with Homebrew"
brew install --ignore-dependencies gitversion
fi
这是必需的,因为GitVersion目前不是构建机器的一部分。此外,在安装时需要忽略mono依赖项,否则当brew尝试链接文件时会出现错误。
对我来说,@MacRusher版本很好。但是对于其他读者来说,他们可能需要加上.toInteger()才能使它正常工作。由于我正在使用yarn version --patch自动升级package.json中的版本号,因此我还必须仅取前两个字符。
以下是新版本:
// On top of your file import a JSON parser
import groovy.json.JsonSlurper
def getVersionFromPackageJson() {
// Read and parse package.json file from project root
def inputFile = new File("$rootDir/../package.json")
def packageJson = new JsonSlurper().parseText(inputFile.text)
// Return the version, you can get any value this way
return packageJson["version"].substring(0,2).toInteger()
}
android {
defaultConfig {
applicationId "your.app.id"
versionName getVersionFromPackageJson()
}
}
package.json
读取版本号。我认为这对很多人可能有用(当我自己寻找这样的解决方案时,我也偶然遇到了你的问题),因为根据项目和使用情况实际设置版本会有很大的不同。有些人可能会使用npm version
命令,其他人可能像你一样使用 semantic-release 或 GitVersion。 - MacRusher