How to get a versionName in react-native app on Android?

AndroidReact Native

Android Problem Overview


I've made a timestamped versionName in build.gradle like 20150707.1125. I want to show the version of the package in react-native app in about window. How I could get versionName in code?

Android Solutions


Solution 1 - Android

I've successfully used the React Native Device Info component to get the build details as specified in the Gradle config.

Once installed you can use:

DeviceInfo.getVersion()

To output the version, and:

DeviceInfo.getBuildNumber()

To get the build number.

Solution 2 - Android

If you want the version number in your package.json, you can also do:

var pkg = require('./package.json');
console.log(pkg.version);

Solution 3 - Android

import { version } from './package.json';

Solution 4 - Android

I couldn't get the package react-native-device-info to work. Ran into [this issue][1] Might need some gradle and java changes to make it fly.

Anyhow I got what I needed [react-native-version-number][2]. And I am happy with it.

import VersionNumber from 'react-native-version-number';

console.log('appVersion:', VersionNumber.appVersion)

Oh, and as it relates to gleaning the version from package.json. It feels wrong to me. I mean I had to try it just to see if it would work. I didn't realize that resource would be available at runtime on the device. It does work, but I also have some buildTypes debug foo going on in my build.gradle I learned [here][3]. So its nice to be getting the versionName like 0.1.7-debug straight from the horses mouth.

[1]: https://github.com/rebeccahughes/react-native-device-info/issues/236 "Cannot read property 'appVersion' of undefined" [2]: https://github.com/APSL/react-native-version-number "react-native-version-number" [3]: https://medium.com/@manas/manage-your-android-app-s-versioncode-versionname-with-gradle-7f9c5dcf09bf

Solution 5 - Android

I used as reference the answer by @Marcos Demetrio

But I was using expo, so I did this:

import {expo} from '../../app.json'

And in the Component:

<Label>{expo.version}</Label>          

No package install needed.

Solution 6 - Android

You can use react-native-device-info

And you can get app version for both iOS and Android by calling following method.

const version = DeviceInfo.getVersion();
 
// iOS: "1.0"
// Android: "1.0"

Hope this will help.

Solution 7 - Android

react-native-version-number library works for both Android and iOS. You can find installation instructions here. Remember that in current versions of ReactNative linking libraries is not needed anymore (omit linking while installing - it was not written in the instruction)

https://github.com/APSL/react-native-version-number

import VersionNumber from 'react-native-version-number';

...

    render() {
        var versionNumber = `${VersionNumber.appVersion}.${VersionNumber.buildVersion}`;

        return (
            <Text style={ styles.versionText }>v. {versionNumber}</Text>
        )
    }
...

Solution 8 - Android

I used the following snippet to extract version info using package.json:

import PackageJson from '../../../package'    // where package is the package.json file

console.log('version', PackageJson.version)  

Hope it helps.

Solution 9 - Android

I tried most of the thing to fix this nicely and I and happy to see detailed description for doing everything that I needed react-native-version-check

import { Linking } from 'react-native';
import VersionCheck from 'react-native-version-check';

VersionCheck.needUpdate()
  .then(async res => {
    console.log(res.isNeeded);    // true
    if (res.isNeeded) {
      Linking.openURL(await VersionCheck.getStoreUrl());  // open store if update is needed.
    }
  });

Solution 10 - Android

If you are using expo and you want lighter lib, use expo-application:

import * as Application from 'expo-application';

Application.nativeApplicationVersion //returns a string
// or
Application.nativeBuildVersion //returns a string

Solution 11 - Android

If you use Expo Framework, you can use expo-constants package as documented in https://docs.expo.dev/versions/latest/sdk/constants/.

For me, this is working: grabs the version and description from the app.json: https://github.com/marcoXbresciani/TKCompanionApp/blob/0.1.12/screens/about/Version.tsx

So, only one point where to store things.

Solution 12 - Android

I think the easiest way is to set a version number in App.js just like a variable, and ensure it is global (gettable from the whole app)

const version = "1.0.0"

and do this i every build

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionusegoView Question on Stackoverflow
Solution 1 - AndroidTom WaltersView Answer on Stackoverflow
Solution 2 - AndroidSnowmanView Answer on Stackoverflow
Solution 3 - AndroidMarcos DemétrioView Answer on Stackoverflow
Solution 4 - AndroidnavanjrView Answer on Stackoverflow
Solution 5 - AndroidSanchitosView Answer on Stackoverflow
Solution 6 - AndroidAnilkumar iOS - ReactNativeView Answer on Stackoverflow
Solution 7 - AndroidRadView Answer on Stackoverflow
Solution 8 - AndroidQasimView Answer on Stackoverflow
Solution 9 - AndroidRahul MishraView Answer on Stackoverflow
Solution 10 - AndroideyalyoliView Answer on Stackoverflow
Solution 11 - AndroidMarco BrescianiView Answer on Stackoverflow
Solution 12 - AndroidIngenious_HansView Answer on Stackoverflow