"version": "0.0.1",
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
...
}
...
}
...
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}"
在更新package的version后xcode必须先运行一次才能生效