Skip to content

Instantly share code, notes, and snippets.

@naishe
Created October 15, 2021 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save naishe/3f013bd9fac779406531f90a695dd5ab to your computer and use it in GitHub Desktop.
Save naishe/3f013bd9fac779406531f90a695dd5ab to your computer and use it in GitHub Desktop.
Add Scripts to android/app/build.gradle to pick version code and version name from package.json
// Import JSON parser, place it at the top of the file
import groovy.json.JsonSlurper
// Read package.json and return `version` field
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)
println ("Version Name: " + packageJson["version"]);
// Return the version, you can get any value this way
return packageJson["version"]
}
// Convert version name to a version number
def getNumericVersionFromNpm() {
def versionName = getVersionFromNpm()
def parts = versionName.split("\\.")
int hotfix = parts[2] as int
int minor = parts[1] as int
int major = parts[0] as int
def numericVersion = major*100000 + minor*1000 + hotfix
println('Version Code: ' + numericVersion)
return numericVersion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment