原文:https://blog.csdn.net/gu1920948999/article/details/117984844
- package.json
"version": "1.0.0",
- android配置
android/app/build.gradle
import groovy.json.JsonSlurper
...
/**
* 获取版本号
*/
def getAppVersion() {
def inputFile = new File("../package.json")
def packageJson = new JsonSlurper().parseText(inputFile.text)
return packageJson["version"]
}
def appVersion = getAppVersion()
android {
...
defaultConfig {
...
versionName appVersion
...
}
...
}
...
3.ios配置
- 打开xcode
- 点击项目名称
- 选择Build Phases
- 点击(Build Phases)标题栏下的加号+, New Run Script Phase
添加下列代码
PACKAGE_VERSION=$(cat ../package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $PACKAGE_VERSION" "${PROJECT_DIR}/${INFOPLIST_FILE}"
脚本解释: