react-native-community / discussions-and-proposals

Discussions and proposals related to the main React Native project
https://reactnative.dev
1.69k stars 127 forks source link

Template react native android version the same as found on package.json #665

Open Angelk90 opened 1 year ago

Angelk90 commented 1 year ago

Introduction

Every time I change the package I would like the android version of the app to change, because it can happen that I forget about it and then it takes some time before I notice it.

Details

In the file:

https://github.com/facebook/react-native/packages/react-native/template/android/app/build.gradle

Add and edit like this:

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

// Create an easy to use function
def getVersionFromPackageJson() {
    //  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"]
}

....

    defaultConfig {
        applicationId "com.helloworld"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName getVersionFromPackageJson() /* This */
    }

Discussion points

In order to always have the package version aligned with the android app version.

The same could be said for ios.

cortinico commented 1 year ago

Every time I change the package I would like the android version of the app to change, because it can happen that I forget about it and then it takes some time before I notice it.

That's a good point, but there are also a number of use cases where users don't want this behavior because the Android & iOS versioning scheme are following different numbering. Say they apply a fix on iOS only and don't want to bump the Android version.

The snippet you suggested is valid though and helps you achieve what you're looking for

Angelk90 commented 1 year ago

@cortinico : So what would you suggest? I wish this functionality was in the react native template, do you think it can be accepted or not?

cortinico commented 1 year ago

Sadly this can't be accepted as it is:

    def inputFile = new File("$rootDir/../package.json")

here you're making an assumption on where the package.json lives, which is not going to work well for users for monorepos.

Plus as I mentioned, we update the template adding functionalities that are needed for the majority of our users. So far you're the first one I've heard needing this feature. If there is enough demand around this, we can consider updating the template to support this feature.