Skip to content

Instantly share code, notes, and snippets.

@mrkpatchaa
Last active November 1, 2018 13:15
Show Gist options
  • Save mrkpatchaa/ed8af95c71b921cc994ffd7f5ba405dd to your computer and use it in GitHub Desktop.
Save mrkpatchaa/ed8af95c71b921cc994ffd7f5ba405dd to your computer and use it in GitHub Desktop.
React Native build number
android/app/build.gradle
import groovy.json.JsonSlurper
def getNpmVersion() {
def inputFile = new File("../package.json")
def packageJson = new JsonSlurper().parseText(inputFile.text)
return packageJson["version"]
}
/* calculated from git commits to give sequential integers */
def getGitVersion() {
def process = "git rev-list master --first-parent --count".execute()
return process.text.toInteger()
}
......
def userVer = getNpmVersion()
def googleVer = getGitVersion()
android {
...
defaultConfig {
.....
versionCode googleVer
versionName userVer
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
// android/build.gradl
def getNpmVersionArray() { // major [0], minor [1], patch [2]
def (major, minor, patch) = getNpmVersion().tokenize('.')
return [Integer.parseInt(major), Integer.parseInt(minor), Integer.parseInt(patch)] as int[]
}
subprojects {
ext {
def npmVersion = getNpmVersionArray()
versionMajor = npmVersion[0]
versionMinor = npmVersion[1]
versionPatch = npmVersion[2]
}
}
// android/app/build.gradle
android {
...
defaultConfig {
...
versionCode versionMajor * 10000 + versionMinor * 100 + versionPatch
versionName "${versionMajor}.${versionMinor}.${versionPatch}"
}
}
{
"scripts": {
"version": "./version-ios.sh"
}
}
#!/usr/bin/env bash -e
PROJECT_DIR="ios/ReactNativeApp"
INFOPLIST_FILE="Info.plist"
INFOPLIST_DIR="${PROJECT_DIR}/${INFOPLIST_FILE}"
PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[\",]//g' | tr -d '[[:space:]]')
BUILD_NUMBER=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFOPLIST_DIR}")
BUILD_NUMBER=$(($BUILD_NUMBER + 1))
# Update plist with new values
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${PACKAGE_VERSION#*v}" "${INFOPLIST_DIR}"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "${INFOPLIST_DIR}"
git add "${INFOPLIST_DIR}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment