1、在package.json里修改版本号
{ "name": "xxx","version": "2.0.0","versionCode": 127,"private": true,"scripts": {...},...}
2、Android 动态获取版本号
修改文件 android/app/build.gradle
import ...
...
def getAppVersion() {
def inputFile = new File("../package.json")
def packageJson = new JsonSlurper().parseText(inputFile.text)
return packageJson
}
def appVersion = getAppVersion()
...
android {
...
defaultConfig {
...
versionCode appVersion["versionCode"]
versionName appVersion["version"]
...
}
...
}
3、IOS动态获取版本号
按照以下的路径(如图所示)
xcode -> xxxproject.xcodeproj->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}"
BUILD_NUMBER=$(cat ../package.json | grep versionCode | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "${PROJECT_DIR}/${INFOPLIST_FILE}"
4、重新打包
xcode需要重新运行,app就可以获取最新的版本号了。后续只需要修改package.json里的版本号然后重新打包就可以更新版本号。