sfr*_*ini 8 android gradle node.js gitversion react-native
目前我有一个 react native 应用程序,我遇到的问题是在每次构建或提交时更新版本非常耗时。
此外,我启用了 Sentry,因此每次构建时,某些构建都会获得相同的版本,因此某些崩溃很难确定它们来自何处。
最后,手动更新版本容易出错。
我如何设置我的构建以在每次构建并忘记所有这些手动任务时生成自动版本?
Mac*_*her 13
虽然目前接受的答案会起作用,但有一种更简单、因此更可靠的方法来做到这一点。您实际上可以package.json从build.gradle.
修改您的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()
}
}
Run Code Online (Sandbox Code Playgroud)
这样你就不需要预构建脚本或任何东西,它就可以工作。
由于我为此工作了几天,所以我决定与大家分享我是如何做到的,因为它可以帮助其他人。
使用的工具:
应用程序 gradle 只需要在它的末尾添加一行。就我而言,我有 Google Play Services gradle,然后我添加了它。
apply from: 'version.gradle'
Run Code Online (Sandbox Code Playgroud)
此文件应与您的应用程序 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
}
Run Code Online (Sandbox Code Playgroud)
现在,让我们回顾一下脚本实际在做什么:
这是首先运行的脚本。让我们回顾一下正在做什么:
$semver=(gitversion /showvariable Semver);
Set-Content -path props.properties -value semver=$semver;
npm version --no-git-tag-version --allow-same-version $semver
Run Code Online (Sandbox Code Playgroud)
就是这样。现在每次进行构建时,都应该自动生成版本,更新 package.json 并且构建应该具有特定的版本名称。
由于我使用App Center进行构建,因此我将告诉您如何在构建机器中使用它。您只需要使用自定义脚本。
#!/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
Run Code Online (Sandbox Code Playgroud)
这是必需的,因为 GitVersion 当前不是构建机器的一部分。此外,安装时需要忽略单声道依赖项,否则在 brew 尝试链接文件时会出现错误。
| 归档时间: |
|
| 查看次数: |
3205 次 |
| 最近记录: |