Como atualizar o número da versão do aplicativo nativo react


103

Estou usando o React nativo com Android. Como posso atualizar o número da versão no aplicativo? Como estou recebendo este erro.

Estou gerando o arquivo de acordo com este url https://facebook.github.io/react-native/docs/signed-apk-android.html

Tentei modificar o arquivo AndroidManifest.xml, mas depois de criá-lo, esse arquivo é modificado automaticamente de volta.

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

Aqui, eu modifiquei o XML:

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

Depois, o arquivo de construção muda automaticamente de volta.

insira a descrição da imagem aqui


1
android: versionCode = "2" significa que sempre que você quiser fazer o upload do apk na Play Store que for necessário para aumentar o código da versão e o código da versão, você pode manter o que quiser sem impacto
Ajinkya,

não funciona, eu já fiz. Eu corrigi minha pergunta.
Praveen Prasad

Respostas:


198

Você deve mudar seu versionCodee versionNameem android/app/build.gradle:

android {

    defaultConfig {

        versionCode 1
        versionName "1.0"

        {...}
    }

    {...}
}

Thanks, I myself saw that file multiple times, but just missed that part.
Praveen Prasad

3
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.
AidenMontgomery

1
@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
Joseph Roque

14
@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.
Joseph Roque

2
@KTWorks yes, versionCode must be an integer. versionName can be set to 1.0.1 and will display that value to users.
Joseph Roque

91

@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
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.
DBrown

21

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.



1

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.
    {...}
}
{...}
}

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"

    {...}
}

{...}
}
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.