Skip to content

Instantly share code, notes, and snippets.

Created October 15, 2021 11:23
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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