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

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?

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.

If you want the version number in your package.json, you can also do:
var pkg = require('./package.json');
console.log(pkg.version);

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

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.

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.

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.

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

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

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.

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.
}
});

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.

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

Related

How to change android package name in React native 0.70 and above

I've came up with this problem as in my recent project in which I'm using react native 0.70, I need to change the android package name and all the solutions which I found were on pervious versions but the structure changed a little in React native 0.70 so I was getting errors
I tried many solutions which were provided many they were telling to change these files
android/app/src/main/java/yourApp/MainActivity.java
android/app/src/main/java/yourApp/MainApplication.java
android/app/src/main/AndroidManifest.xml
android/app/build.gradle
android/app/Buck:
So I've found the solution to this and sharing it here so if anyone going through the same can get help from this
As the structure has changed a little in react native 0.70 we need to make changes in some extra files also
So these are the changes which we need to make, i'ive used newApp but you've to use your package name there
In:android/app/src/main/java/yourApp/MainActivity.java:
package com.newApp
In:android/app/src/main/java/yourApp/MainApplication.java:
package com.newApp
In: android/app/src/main/AndroidManifest.xml:
package="com.newApp"
In: android/app/build.gradle:
applicationId "com.newApp"
In:android/app/_BUCK:
android_build_config(
package="com.newApp"
)
android_resource(
package="com.newApp"
)
In: android/app/src/main/jni/MainApplicationTurboModuleManagerDelegate.h
static constexpr auto kJavaDescriptor =
"Lcom/newApp/newarchitecture/modules/MainApplicationTurboModuleManagerDelegate;";
In: android/app/src/main/jni/MainComponentsRegistry.h
constexpr static auto kJavaDescriptor =
"Lcom/yournewpackagename/newarchitecture/components/MainComponentsRegistry;";
In:android/app/src/main/java/yourApp/newarchitecture/MainApplicationReactNativeHost.java: make changes in these 4 lines
package com.newApp.newarchitecture;
import com.newApp.BuildConfig;
import com.newApp.newarchitecture.components.MainComponentsRegistry;
import com.newApp.newarchitecture.modules.MainApplicationTurboModuleManagerDelegate;
In:android/app/src/main/java/yourApp/newarchitecture/components/MainComponentsRegistry.java:
package com.newApp.newarchitecture.components;
In:android/app/src/main/java/yourApp/newarchitecture/modules/MainApplicationTurboModuleManagerDelegate.java:
package com.newApp.newarchitecture.modules;
after these steps just clean your project by typing this in your project terminal
cd android
./gradlew clean
now just go back and run your project and it would be a good approach to use android studio to run it for the first time
How to change android package name com.companyname.appname

How to get Package name and version Code, Version name in react native [duplicate]

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?
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.
If you want the version number in your package.json, you can also do:
var pkg = require('./package.json');
console.log(pkg.version);
import { version } from './package.json';
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.
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.
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.
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
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>
)
}
...
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.
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.
}
});
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.
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

How to use react-native-batch-push on android? RNBatch is undefined when trying to access RNBatch.NOTIFICATION_TYPES

I'm trying to use this package in my react native project.
So far I've followed all the steps in their installation guide and I made it work on iOS.
However, on Android, every time I try to import Batch or BatchPush in my code like this:
import { BatchPush, Batch } from "#bam.tech/react-native-batch";
I get an error on Android only:
null is not an object (evaluating 'RNBatch.NOTIFICATION_TYPES')
So when I go to node_modules/#bam.tech/react-native-batch/dist/BatchPush.js I see this
const RNBatch = react_native_1.NativeModules.RNBatch;
exports.AndroidNotificationTypes = RNBatch.NOTIFICATION_TYPES;
So somehow the native module is not being imported correctly. Do I need to follow extra steps for this to work?
#bam.tech/react-native-batch is being used with version 5.2.1
npm version 6.14.7
react-native version 0.60.5
Update: it turns out that the package was not linked correctly and that I had to manually add the package in MainApplication.java (I don't know why react-native link did not add this automatically)
Add the import:
import tech.bam.RNBatchPush.RNBatchPackage;
And then add
new RNBatchPackage(), in the getPackages() method.

Android Package name changes when building an app from a Flutter Module

I'm having a small problem. I'm trying to build an apk from a flutter module. I specified:
module:
androidX: true
androidPackage: com.my.package.name
The issue is that after I run flutter pub get, the applicationId is set to com.my.package.name.host.
I'd like to remove the .host part somehow as I'm trying to automate this and upload it to Firebase app distribution.
Any help is greatly appreciated.
Cheers,
Vlad
I ended up doing something like this:
lane :change_package_name do
path = "../../.android/app/build.gradle"
text = File.read(path)
updated = text.gsub(".host", "")
File.open(path, "w") { |file| file.puts updated }
end
it's a small fastlane script that removes the occurance of .host. Not the best solution but it works for now.

include.gradle file being generated is causing problems

When building a project I get the following error:
Flavor 'nativescript-telerik-ui' has unknown dimension 'nativescript-telerik-ui'.
It happens only when using the pro version through the #progress registry. Doesn't happen with the local .tgz pro version.
I noticed the error has to do with the include.gradle file it generates. I read the following article: https://docs.nativescript.org/plugins/plugins#includegradle-specification
It says that when the plugin doesn't have the include.gradle, at build time gradle creates a default one with default elements. When I saw the include.gradle it generated for the plugin it seems to have generated a default one like so:
android {
productFlavors {
"nativescript-telerik-ui" {
dimension "nativescript-telerik-ui"
}
}
}
The include.gradle generated for the local .tgz version of the plugin is like this:
android {
productFlavors {
"F6" {
dimension "nativescripttelerikuipro"
}
}
}
I replaced the default include.gradle with the latter and it got past the error. You can recreate the problem by following these steps:
create a new hello world app
use the command npm login --registry=https://registry.npm.telerik.com/ --scope=#progress to log in if you're a paying customer.
use the command npm install --save #progress/nativescript-telerik-ui-pro to install the plugin
use tns run android
Is there anything I can do to solve this problem? Really need help on this.
My name is Vladimir and I am part of the nativescript-telerik-ui-pro team. Thank you for logging this issue in our feedback portal. We are going to review it as soon as possible and update you regarding its status, but from what I currently see there is some incorrect "parameters" passed to the 'pro' version of the plugin that are going to be resolved very fast.
We apologize for any inconvenience that this is causing.

Categories

Resources