161

I am using React native with Android. How can I update version number in the app? As I am getting this error.

I am generating file as per this url https://facebook.github.io/react-native/docs/signed-apk-android.html

I have tried modifying AndroidManifest.xml file, but after I build it, that file gets automatically modified back.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.facebook.react"
    android:versionCode="1"
    android:versionName="1.0" >

Here, I modified the XML:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.facebook.react"
    android:versionCode="2"
    android:versionName="1.1" >

After, build file automatically changes back.

enter image description here

2
  • 1
    android:versionCode="2" mean whenever you want to upload apk on play store that is necessary to increase version code & version code you can keep whatever you want doesn't impact Commented Mar 10, 2016 at 18:48
  • doesn't work, I have already done it. I have corrected my question. Commented Mar 10, 2016 at 18:49

10 Answers 10

297

You should be changing your versionCode and versionName in android/app/build.gradle:

android {

    defaultConfig {

        versionCode 1
        versionName "1.0"
        
        {...}
    }

    {...}
}

Note that versionCode has to be in an integer that is larger than the ones used for previous releases while versionName is the human readable version that may be shown to users.

Sign up to request clarification or add additional context in comments.

10 Comments

I found the solution in the file that you pointed to, but it was actually slightly further down. There was a section that started with applicationVariants and in there the output.versionCodeOverride was getting set, which did as the name suggested and overrode the one at the top of the file.
@Noitidart if you're building with Android Studio and Gradle, changing it here will be enough. It will override anything you put in AndroidManifest.xml
If I want my version to be "2.2" should I set versionCode to 2.2? I don't understand versionCode :(
@Noitidart versionCode is a value that's meant for Google/you. When you upload to Google Play, it expects versionCode to be greater than the previous versionCode, and also unique for every uploaded file. Personally, I just manually increment versionCode by one each time I prepare an upload to Google Play. Other people automate it to increment based on the git commit, or other factor. versionName is what you would change to "2.2" so that your users will see that version number when they update/download your app.
@KTWorks yes, versionCode must be an integer. versionName can be set to 1.0.1 and will display that value to users.
|
116

@Joseph Roque is correct, you need to update the version numbers in android/app/build.gradle.

Here's how I automate this and tie it into the package's version in package.json and git commits.

In android/app/build.gradle:

/* Near the top */

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"
        }
    }

Notes:

  • It's important that versionCode is an integer - so we can't use semantic versioning here. This is used on the play store to tell which versions come after others - that's why it's tied to git commits in getGitVersion

  • versionName however is shown to users - I'm using semantic versioning here and storing the real value in my package.json. Thanks to https://medium.com/@andr3wjack/versioning-react-native-apps-407469707661

6 Comments

Andrew Jack discusses a very similar method, and a method for iOS in his Medium article: medium.com/@andr3wjack/… . It is a great read for people wanting to keep their versions matching with their builds.
Does it mean that every time I used git commit the version changes incrementally? And which update type is incremented? I mean, will it start in the patch number?
@EdisonPebojot Yes, though only one of the versions will change. There are two version types - the versionCode which must be incremental and is based on number of commits. And the versionName which is taken from your package.json can be a string and is visible to the user. You may want to read this answer: stackoverflow.com/a/10269683/1760776 for more details
Andrew Jack's article on Medium has been deleted. Here's his blog post on the topic, though: blog.andrewjack.uk/versioning-react-native-apps.
Need change master to main def process = "git rev-list main --first-parent --count".execute()
|
50

For those wanting to automate this, and have iOS at the same time, you can use react-native-version to set the version numbers.

All you need to do is update your version number inside the package.json file and run the following:

$ npx react-native-version --never-amend

[RNV] Versioning Android...
[RNV] Android updated
[RNV] Versioning iOS...
[RNV] iOS updated
[RNV] Done
✨  Done in 0.39s.

I hope this can help others.

2 Comments

If you have ejected from expo, make sure the app.json is deleted otherwise the android and ios files will not get updated.
@bjorn-reppen Deleting app.json in Bare Workflow Expo projects is not the best solution, as App.json is still used by Expo (Updates for example) The issue is being discussed here: github.com/stovmascript/react-native-version/issues/105 I patched react-native-version 4.0.0 as a temporary workaround until it gets fixed.
4

I had the same problem and I checked all the above answer, I had a made a silly mistake because of which nothing worked for me. Just in case any of you do same mistake as mine try this.

  1. Version can be a decimal number like 1.0 or 1.0.1 etc
  2. But VersionCode cannot be decimal number It should be 1,2,3 etc and not 1.1 or 2.2

So in project/app/build.gradle

android {
defaultConfig {
    versionCode 1 // do not use decimal number here
    versionName "1.0" // you can use decimal number here.
    {...}
}
{...}
}

Comments

2

Set the versionCode under android in app.json:

{
  "expo": {
    "name": "App Name",
...
    "android": {
      "package": "com.app.name",
      "permissions": [],
      "versionCode": 2
    }
  }
}

ref:https://docs.expo.io/versions/latest/workflow/configuration/#versioncodeversion-number-required-by-google-play-increment-by-one-for-each-release-must-be-an-integer-httpsdeveloperandroidcomstudiopublishversioninghtml

Comments

1

All these responses are more difficult than this should really be!

Even the "React Native Version" package is not up to the task. It forgets to update some files such as info.plist, suffers from pointless "SyntaxError" errors (which I can see people having issues getting around), and relies on deletion of app.json (which you may not wish to do) to update ejected expo projects properly.

Better to just manage your own scripted solution for versioning.

  1. You can copy the node script from the gist link below to update the following files in a single command:
  • package.json
  • android/app/build.gradle
  • ios/info.plist
  • ios/app.xcodeproj/project.pbxproj

Link: https://gist.github.com/uxinnuendo/1be49a8faec7031044a87dcf79234cbd

  1. Save to your root project folder and refer to the code comments to update the path references as appropriate for your project.

  2. Then run: $ node patch.js

  3. Modify the regex patterns to meet your project config files, since this can differ based on your react native / expo version. Some projects may have surrounding double quotes on version numbers, etc.

The script only deals with "patch" versioning for the moment, and is based on the following integer version format, both of which you can modify as you desire.

[SDK version: xx][Major version: xx][Minor version: xxx][Patch version: xx]

eg. 330100001

Comments

0

If someone is facing

wrong version code eg - 31284

Then make sure to not use SeparateBuildPerCPUArchitecture in android/app/build.gradle

def enableSeparateBuildPerCPUArchitecture = false

and

to update the version code and name change in android/app/build.gradle:

android {

defaultConfig {

    versionCode 1
    versionName "1.0"

    {...}
}

{...}
}

Comments

0

What @tgf has is good for Android, but I wanted ios as well. The default Info.plist sets CFBundleShortVersionString to $(MARKETING_VERSION), which is the same as versionName in android/build.gradle, and CFBundleVersion is set to $(CURRENT_PROJECT_VERSION), which is the same as versionCode in android/build.gradle. These are defined in the project.pbxproj in ios/AppName.xcodeproj/. Here is a prerelease script that can update the xcode file. If you have modifications that have no been committed yet, it will add 1 to the build number so that building without any changes will not result in a file change.

yarn add glob --dev
    "ios-update-version": "node scripts/iosUpdateVersion.js",

Use pre scripts to run this command.

scripts/iosUpdateVersion.js:

const { globSync } = require('glob');
var fs = require('fs');
const { execSync } = require('child_process');
const APP_VERSION = require('../package.json').version;
const GET_COMMIT_COUNT = 'git rev-list master --first-parent --count';

let BUILD_NUMBER = 0;

if (execSync('git status --porcelain').length) {
    console.log('increasing build number by one to include untracked changes');
    BUILD_NUMBER++;
}

const response = execSync(GET_COMMIT_COUNT).toString();
BUILD_NUMBER += Number.parseInt(response.trim());
// android (taken care of in build.gradle)
// versionCode googleVerver  // BUILD_NUMBER
// versionName userVer      // APP_VERSION
// ios
// CFBundleVersion CURRENT_PROJECT_VERSION = 1;            // BUILD_NUMBER
// CFBundleShortVersionString MARKETING_VERSION = 1.0;     // APP_VERSION
const plist = '/ios/SplitTheTank/Info.plist'
const xcodeprojs = globSync('ios/*.xcodeproj');
if (xcodeprojs.length === 0) {
    throw 'Could not find any xcodeproj folder'
}
const pbxproj = `${xcodeprojs[0]}/project.pbxproj`;
const data = fs.readFileSync(pbxproj, 'utf8');

let result = data.replace(/(CURRENT_PROJECT_VERSION = )(.*);/g, `$1${BUILD_NUMBER};`);
result = result.replace(/(MARKETING_VERSION = )(.*);/g, `$1${APP_VERSION};`);
fs.writeFileSync(pbxproj, result, 'utf8');

Comments

0

enter image description here i have till now best every solution to auto changed and upgrade version using this library

install it in your current react-native project: rn-build-version

and than add this script inside your package.json under script tag:

 "build:android:apk": "rn-build-version && cd android && ./gradlew clean assembleRelease",
    "build:android:aab": "rn-build-version && cd android && ./gradlew clean bundleRelease",
    "build:android:apk:fast": "rn-build-version && cd android && ./gradlew assembleRelease",
    "build:android:aab:fast": "rn-build-version && cd android && ./gradlew bundleRelease",
    "build:android:apk:local": "cd android && ./gradlew clean assembleRelease",
    "build:android:aab:local": "cd android && ./gradlew clean bundleRelease",
    "build:android:apk:local:fast": "cd android && ./gradlew assembleRelease",
    "build:android:aab:local:fast": "cd android && ./gradlew bundleRelease"

and than now you can use that command to build apk and aab using latest version.

here is command example:

npm run build:android:apk
npm run build:android:aab

that will ask for want to upgrade or not, you can see in screenshotx

1 Comment

very easy solution, if you need to publish you app multi times on play store
-1

If you're using expo and getting this issue, go to app.json and change the version to a higher number.

{
  "expo": {
    "name": "nameofapp", // btw dont copy this
    "slug": "appslug",   // btw dont copy this
    "version": "1.0.0",  // here is where you change the version
    ...
    ...
    ...
  }
}

You need to also change the version in package.json

{
  ...
  "name": "appname",  // btw dont copy this
  "version": "2.0.0", // here is where you change the version
  ...
  "android": {
    "versionCode": 2,
    ...
  }
  ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.