Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - Automatic version name from package.json to android build manifest

Currently I have a react native app and the issue that I have is that is very time consuming to update the version on every build or commit.

Also, I have Sentry enabled so every time I build, some builds get the same version so some crashes are hard to determine where they came from.

Lastly, updating the version manually is error prone.

How can I setup my builds to generate an automatic version every time I build and forget about all of this manual task?

like image 925
sebastianf182 Avatar asked Nov 26 '17 01:11

sebastianf182


1 Answers

While the currently accepted answer will work, there is a much simpler, and therefore more reliable way to do it. You can actually read the value set in package.json right from build.gradle.

Modify your android/app/build.gradle:

// On top of your file import a JSON parser
import groovy.json.JsonSlurper

// Create an easy to use function
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)

    // Return the version, you can get any value this way
    return packageJson["version"]
}

android {
    defaultConfig {
        applicationId "your.app.id"
        versionName getVersionFromNpm()
    }
}

This way you won't need a pre-build script or anything, it will just work.

like image 183
MacRusher Avatar answered Nov 11 '22 16:11

MacRusher