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

后端 未结 8 989
故里飘歌
故里飘歌 2020-12-25 09:55

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 vers

相关标签:
8条回答
  • 2020-12-25 10:09

    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.

    0 讨论(0)
  • 2020-12-25 10:12
    import { version } from './package.json';
    
    0 讨论(0)
  • 2020-12-25 10:14

    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.
        }
      });
    
    0 讨论(0)
  • 2020-12-25 10:18

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

    Anyhow I got what I needed react-native-version-number. 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. So its nice to be getting the versionName like 0.1.7-debug straight from the horses mouth.

    0 讨论(0)
  • 2020-12-25 10:22

    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.

    0 讨论(0)
  • 2020-12-25 10:23

    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.

    0 讨论(0)
提交回复
热议问题