Android 'react-native-gesture-handler' error - react-native

When I follow the instruction by the react-navigation website : https://reactnavigation.org/docs/en/getting-started.html.
react-native link react-native-gesture-handler
and then change the MainActivity.java like it asks.
Then run react-native run-android, and error pop up :
error React Native CLI uses autolinking for native dependencies, but the following modules are linked manually:
react-native-gesture-handler (to unlink run: "react-native unlink react-native-gesture-handler")
This is likely happening when upgrading React Native from below 0.60 to 0.60 or above. Going forward, you can unlink this dependency via "react-native unlink " and it will be included in your app automatically. If a library isn't compatible with autolinking, disregard this message and notify the library maintainers.
my react-native version :
react-native-cli: 2.0.1
react-native: 0.60.0
However, when I follow the instruction, to unlink the library
use : react-native unlink react-native-gesture-handler
The app successfully built, but now it pops up another error in the app :
null is not an object (evaluating 'rngesturehandlermodule.direction')
I tried a lot of solution in a lot of websites, like
undefined is not an object (evaluating 'RNGestureHandlerModule.State'
non of them is working for me

Looks like this is to do with the new autolinking feature in RN 0.60.
For iOS
To fix for now just add the following podspec for RNGesureHandler to ios/Podfile
pod 'RNGestureHandler', :podspec => '../node_modules/react-native-gesture-handler/RNGestureHandler.podspec'
Then in the ios directory run pod install to install it.
For Android
Path: android/app/src/main/java/com/projectname/MainApplication.java
import com.swmansion.gesturehandler.react.RNGestureHandlerPackage;
....
protected List<ReactPackage> getPackages() {
// Add additional packages you require here
// No need to add RnnPackage and MainReactPackage
return Arrays.<ReactPackage>asList(
....
new RNGestureHandlerPackage()
);
}
Have a good day.

I'm not sure about what went wrong, but very possibly because of the react-native-cli that upgraded from below 0.60.0 to above it.
BEFORE 0.60.0 when running react-native run-android, the command will run a metro packager, along with building the android app, and then run the app that connect to the metro packager.
That's why in the past, we only run one command "react-native run-android" to run the project.
But AFTER 0.60.0 when running react-native run-android, the command will only build the android-app, and run the app to connect to the metro packager.
I saw a command prompt blinked once, might also be how the command start the metro packager went wrong and the packager just closed itself somehow.
Also as for the problem with react-native link react-native-gesture-handler I believe it is the new feature of react-native 0.60.0 to autolink library, so manual linking of library is not necessary anymore. As a lot of the library provider might need to start providing auto-linking features, or might face the same error, not just react-native-gesture-handler.
Temporary Solution until someone fix the react-native-cli to run the metro packager again:
Open 2 command prompt instead of 1. And run the command below on each of the command prompt:
react-native start
react-native run-android
You might want to run the first command first and wait for it to be ready before running the 2nd.
Otherwise, you app might turn out to be blank.
Source : I tried it myself by creating new project and running the command as mentioned by the question.

What you can do is, first uninstall react-native-gesture-handler.
Then delete IOS and Android folder.
Then run below command.
1.react-native upgrade --legacy true // to recover ios and android folder.
2.npm install react-native-gesture-handler
3.react-native link
4.react-native run-android
5.react-native run-ios
Also update your MainActivity.java file , follow official website:
https://kmagiera.github.io/react-native-gesture-handler/docs/getting-started.html

Did you run react-native run-android or react-native run-ios ?

Related

Invariant Violation: requireNativeComponent: "RNSScreen" was not found in the UIManager

I am getting RNSScreen error even though I have installed all related packages and followed react-navigation guide (https://reactnavigation.org/docs/getting-started#installation) but nothing worked for me.
Faced the same issue while implementing Navigation. Run following commands
npm install #react-navigation/native
React Navigation is made up of some core utilities and those are then used by navigators to create the navigation structure in your app.
In your project directory, run:
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context #react-native-community/masked-view
This will install versions of these libraries that are compatible.
If you're on a Mac and developing for iOS, you need to install the pods to complete the linking.
npx pod-install ios
Now run
react-native run-ios
That usually happens when you install all packages to add navigation without terminating the running build. Make sure to stop the running process for ( android or IOS ) then run it again after installing them and adding the routing components
I think you are missing to install react-native-screens components. That is required part in install react navigation guideline.
Dont forget to run pod install in ios folder after install package from npm.
stop all the terminal servers and then run
npx react-native run-android
if it still does not work,
npm i react-native-screens
and then
npx react-native run-android
Close Metro and run npx run android to rebuild your code
This solution (source) always works for me, here is the exact solution:
If you're facing with this error
Invariant Violation: requireNativeComponent: "RNSScreen" was not found
in the UIManager.
Follow these steps:
1. Make sure in your `package.json` file if you have the following packages:
#react-native-community/masked-view#react-navigation/native#react-navigation/stackreact-native-gesture-handlerreact-native-reanimatedreact-native-safe-area-contextreact-native-screens
2. Next ensure if you have pods up-to-date (For iOS only) by making sure cd ios --> pod install --> cd ..
3. And finally cancel the current build by pressing Ctrl+C (twice/thrice) and then do npx react-native run-ios
RNSSCreens error because the react-native-screens isn't installed on the your project
just run npm install react-native-screens react-native-safe-area-context or if your using yarn run 'yarn add install react-native-screens react-native-safe-area-context'
As I don't have enough reputations to upVote the answer, I am mentioning the answer which worked out to me here
Solution mentioned by #Gurjinder Singh , worked smoothly..
Recapturing:
Issue : With Expo, Sanity.io app was working on device in dev, but once apk was generated using "eas build -p android --profile preview" , build was successful but when I tried opening the app in my mobile, after splash screen app was crashing.
Adblogcat showed : com.facebook.react.common.JavascriptException: Invariant Violation: requireNativeComponent: "RNSScreen" was not found in the UIManager.
Solution worked:
npm install #react-navigation/native
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context #react-native-community/masked-view
thanks to community and #Gurjinder Singh (top voted)
Hey i was also facing same problem . Just use
npm install react-native-screens
.
This was happening to me on both iOS and Android. I managed to get Android working again by updating the package:
"react-native-screens": "^3.18.1"
Then adding this to dependencies in: android/app/build.gradle
implementation project(':react-native-screens')
Then adding this to protected List getPackages() in: android/app/src/main/java/com/myapp/MainApplication.java
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
packages.add(new com.swmansion.rnscreens.RNScreensPackage());
return packages;
}
NOTE: Make sure you don't already have an import for com.swmansion.rnscreens otherwise the app will keep rebooting due to duplicate screen views
Then finally adding this to android/settings.gradle
include ':react-native-screens'
project(':react-native-screens').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-screens/android')
Make sure you clean gradle before running the app, for example navigate to android folder and run:
./gradlew clean
As for iOS, just add this to target 'myapp' do in: ios/Podfile
pod 'RNScreens', :path => '../node_modules/react-native-screens'
Make sure you delete your Podfile.lock and then run pod install --repo-update in your iOS folder before running the app.
Happy Update: react-native-screens 3.18.2 is out with a fix for this, if you were impacted by the 3.18.1 release that caused compile problems (which you can check by seeing the message below, if needed)
So, mostly obsolete now answer preserved below, but just updating packages should get you moving. Cheers
Nowadays if this is happening it is because react-native-screens 3.18.1 includes new configuration that is only compatible with react-native 0.69+
If you are using an older react-native you need to make sure your react-native-screens dependency is exactly 3.18.0:
"react-native-screens": "3.18.0",
then likely a npx react-native start --reset-cache to restart your javascript bundler with a fresh cache.
You will know if this is your problem if you see something like this when you say npx react-native run-android:
warn Package react-native-screens has been ignored because it contains invalid configuration. Reason: "dependency.platforms.android.componentDescriptors" is not allowed
There may be a 3.18.2 soon with a fix - reference: https://github.com/software-mansion/react-native-screens/issues/1614#issuecomment-1276245207
I've encountered the same problem, and solved it.
You can just close the terminal and app on Android Emulator(or device), and just run npm run android or react-native run-android again.
I think they might have some cache and occur the error.
I hope you can fix the problem.
Close the Metro and IOS simulator.
Ensure you go to your ios folder run pod install
Reopen your simulator
This should solve the problem. I also use quite a while to figure it out
I worked on this problem for several days and this is want worked for me:
In the android\app\src\main\java(your project name)\MainApplication.java
add to Imports:
import com.swmansion.rnscreens.RNScreensPackage;
Then add new RNScreensPackage() to the ReactPackage List:
#Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
...
new RNScreensPackage()
);
}
Edit: This was after I tried everything else here.
This helped me. install this package
npm i react-native-screens
and add
import { enableScreens } from 'react-native-screens';
enableScreens();
You go into the ios folder of your project folder and run the following command.
pod install
Then run the simulator.
You must have react-native-screens installed and I'm guessing that you have done it. So, I will just you a Problem that maybe is not really clear in the documentation.
So you probably just Install React Navigation on your app.
Once you installed it, you need to restart you app.
So close your app, run react-native run-ios or react-native run-android again.
React navigation Install some native modules. Thus, you need to restart that app as I mention.
This is really clear on their documentation.
I also came across the same problem. For Android, I uninstall the app from the emulator then build the app again.Problem solved. Assuming that you already installed packages you need.
Just stop running your node server and build the app again from Xcode/Android Studio or using command react-native run-ios or react-native run-android
In case you are on android, you need to rebuild using npx run android or react-native run-android
The new installed apk will work fine
I also get this error and after doing these steps my app starting running
cd android
./gradlew clean (for windows or linux)
npm install react-native-screen
react-native start & react-native run-android
worked for me yarn add react-native-reanimated
"#react-navigation/native": "^6.0.10", "#react-navigation/native-stack": "^6.6.2", "react": "17.0.2", "react-native": "0.68.2", "react-native-reanimated": "^2.8.0", "react-native-safe-area-context": "^4.3.1", "react-native-screens": "^3.13.1", "styled-components": "^5.3.5"
you must clear the Metro bundler cache
run the following command :
expo start -c
Finally found the solutions, first by creating new project and secondly re-setup of the react-google-maps library.
Workaround Solution: Created new project, installed all dependencies from scratch, followed step-by-step installation guidelines of react-native screens, and finally moved all my code files into a newly created project. This worked for me :D
After digging into the project code and compared project code with newly generated code, here are my findings:
Actual Problem: We had integrated react-native-google-maps library some time ago, for which I and other developers followed step-by-step guidelines and made changes in the MainActivity.java file in the android folder. There were some misconfigurations due to which RNSScreen issue was getting thrown.
Actual Solution Removed react-native-google-maps completely (uninstalled npm packages) and removed Google Maps configuration and did set up the react-google-maps library again
In conclusion, if any of the developers, try to set up google maps in react native, please check the version of react native and react-native-google-maps library and then follow the steps carefully.
Hopefully, this would answer your queries.
In my case, the following code block helped!
npm install react-native-screens
npm run android/ ios
(It Workes For Me..!)
for React-native developers..
Some times the changes of packages Not reflacted in xcode so follow this steps for Solution..
Open Project in xcode ->
Clean Buid Folder ->
(Rebuid Your Project)Run on any Simulator and wait for Build..
->Once Build either You can run again by Terminal or continue work by xcode..
none of the recommended answers worked until I tried what I found in this comment plus restart my steps:
deleting the node_modules directory
turn Metro and actually quit the simulator completely
yarn to install again
start metro via npx react-native start
install pods via npx pod-install ios
run simulator again via npx react-native run-ios
Make sure the below plugin install and linked with android & ios?
npm install react-native-screens
If not linked then perform the below command
react-native link react-native-screens
after successfully linked for android and ios, "Sync Gradle" from android studio and "pod install" for ios
Now close the terminal and again build and run app
I updated Expo to 45.0.0. That fixed the error and solved a problem with the app crashing after launch in production mode but working fine on device with QR code or simulator.
You need to do a few things:
Open android/app/src/main/java/MainApplication.
Replace the function getPackages() with:
#Override protected
List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new ReactNativeFirebaseAuthPackage());
return packages;
}
yarn start --reset-cache or npm start -- --reset-cache
Clean:
cd android
./gradlew clean

I want to start with react native without expo method.. i found this error

I got this error when i am trying to run my app in original device, everything according to react-native documentation
Try running your app with the yarn command, otherwise run npm-doctor to make sure all your packages are updated and fixed.
otherwise try installing dependencies for expo using :
npm install -g yarn
yarn add global react-native
yarn add global react-native-cli
react-native init sample
here's a quick link to the npm-doctor documentation:
https://docs.npmjs.com/cli-commands/doctor.html
Also here's some tutorial on running react-native app using yarn command:
How do I create a React Native project using Yarn?
I find this solution .. i just replace 6.2 with 6.3
in android/gradle/ gradle-wrapper.properties
distributionUrl=https://services.gradle.org/distributions/gradle-6.3-all.zip

Cannot run program "npx": error=2, No such file or directory when running React Native app

I have been working with react-native project in another pc and it has been working correctly. Now I cloned project to another device where I have react-native installed since I work with other project and I cannot run it.
When I run react-native run-android I get this error:
Cannot run program "npx": error=2, No such file or directory
I'm using Android Studio 4 on Mac OS Catalina 10.15.6.
I solved the problem by running Android Studio with bellow command on console.
$ open -a "Android Studio.app"
Simple steps you need to go through to make it work with npx
sudo npm uninstall -g react-native-cli
sudo npm i -g npx
npx react-native run-android
More detailed explanation why this is happenig
Issue was that Facebook is not using anymore react-native-cli as they are using npx.
As stated in Facebook page:
If you previously installed a global react-native-cli package, please remove it as it may cause unexpected issues.
So i had to run sudo npm uninstall -g react-native-cli to remove react-native-cli
They also say that npx is shipped with nodejs but that wasn't my case.
React Native has a built-in command line interface, which you can use to generate a new project. You can access it without installing anything globally using npx, which ships with Node.js.
So I had to install it using sudo npm i -g npx
After installing npx then just need to run npx react-native run-android.
Be aware of npx before react-native run-android
Now you don't have any command react-native now we only have 'npx' and react-native run-android is just a parameter for npx. For ex. to start metro we should run npx react-native start
I had the same issues, so I followed these steps:
Start by installing the latest version of Android Studio (Don't delete the old version, just copy paste that to another directory or in my case I just added the Android Studio 3.2.1 that I had to a folder called olderAndroidStudio, on mac).
Make sure you don't click on import settings from Existing.
Follow along the installation, and complete it.
Build and Run, app runs without any problems.
I additionally uninstalled react-native-cli using the method given in the accepted answer.
In my case, I just quit Android Studio and reopened, and the error was gone.
Maybe throw in a ./gradlew clean for goo measure.
I solved it by finding the command that was throwing error and ran it directly
node -e "console.log(require('react-native/cli').bin)"
which outputs a path, something like
$HOME/my-app/node_modules/#react-native-community/cli/build/bin.js
then I pasted that path in android/app/build.gradle
project.ext.react = [
entryFile: "index.js",
enableHermes: true, // clean and rebuild if changing
cliPath: "$HOME/my-app/node_modules/#react-native-community/cli/build/bin.js" // <--- add this
]

How to run an old version of react native

Just a quick question, how to run an existing older version of react native project. Lets say version 0.59.10 , without upgrading it. what would be the command line commands. Thanks
You should navigate to project directory (if you aren't yet)
cd PATH_TO_PROJECT_DIRECTORY
run the following command to install the dependencies
npm install
then if the react-native version is 0.59.10 and lowers you should also run the command below to link the dependencies to your native android and ios project.
react-native link
and then run one the following commands based on your device
for android
react-native run-android
for ios
react-native run-ios

"react-native eject": Unrecognized command "eject"

I am trying to recreate ios and android folders for my react-native project. As far as I know this is done with the command:
react-native eject
But I get the error:
error Unrecognized command "eject".
I am doing something wrong? How should I recreate android and ios folders?
react-native-cli: 2.0.1
npm i react-native-eject
npm i #react-native-community/cli
react-native eject
will generate the both android and ios folder
It did work in 0.59.9. This threw me too this morning after I upgraded to 0.60. Apparently you now have to run react-native upgrade --legacy true. react-native eject was much easier IMHO.
yarn add react-native-eject
react-native eject
This two-line of code solved my problem with the eject error.
Nikita is 100% correct. If the project was initialized in expo running react native eject will "eject" the project from the expo dependencies (and expo environment) and create a standalone react native project outside the expo environment. This typically adjusts the dependencies in the pakage.json file.
The command exists as a part of expo.
To initialize a project from scratch react-native init will create android and ios folders. Also ensure if you have cloned the directory to run either npm install or yarn to install the dependencies.
If the project was initialized the way I imagine yours was the commmand you are looking for would be one of the following:
react-native run ios or react-native run ios --device
react-native run android or react-native run android --device
For RN projects, which are initiated by react-native init there is no such command, by default RN is using metro bundler instead of web-version of React, where you can configure Parcel/Webpack/Rollup or run create-react-app where actually you can eject.
This question is suitable for expo where you can actually eject, if you need some native modules that are not included in expo.
Try to use expo
react-native eject and upgrade --legacy true aren't working for me.
In the end, I just created a new blank RN project and this had an ios folder. Copied the folder over to my existing project and ... seems to work.
Run npm i react-native-eject
Run npm i #react-native-community/cli --force
Note: use npx instead of npm
npx react-native eject
cd android
./gradlew.bat installDebug
cd..
npm run android