Skip to content

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

Closed

Description

@lehang123

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
#494
https://stackoverflow.com/questions/52861437/undefined-is-not-an-object-evaluating-rngesturehandlermodule-state

non of them is working for me

Activity

  1. thymikee commented on Jul 6, 2019

    @thymikee
    Contributor

    Hey! RNGH doesn't support autolinking in Android yet. RNGH is compatible with autolinking, but still it needs some extra steps. What you need to do:

  2. himanshupadia commented on Jul 8, 2019

    @himanshupadia

    facing same error in iOS, could anyone please help to resolve this issue.

  3. thymikee commented on Jul 8, 2019

    @thymikee
    Contributor

    EDIT: both platforms work with autolinking, see #671 (comment)

    @himanshupadia iOS should be working just fine with autolinking. If you get this error, it means that the CLI detected you have this lib manually linked. Here's what you can do to get it sorted out:

    1. Update the @react-native-community/cli to the latest version.
    2. Run:
      react-native unlink react-native-gesture-handler --platforms ios
      
  4. himanshupadia commented on Jul 8, 2019

    @himanshupadia

    @thymikee Yes, in iOS it's autolinking, it's was working fine on last week but, on today, suddenly it's starting getting this error, bellow is dependencies.

    "dependencies": {
        "react": "16.8.6",
        "react-native": "0.60.0",
        "react-native-gesture-handler": "^1.3.0",
        "react-navigation": "^3.11.0"
      }
    

    I am new in this so I am following tutorial and performing operation.

    Could you please share any link regarding your post/reply.
    It would be great for me.

  5. lemuelflores commented on Jul 9, 2019

    @lemuelflores

    @thymikee I think Step 5 should come before Step 2 because otherwise executing react-native link react-native-gesture-handler --platforms android won't do anything. At least for me it does that.

    EDIT: I just realized if you put android in react-native.config.js it will disable the autolink for that. So instead, on step 2, put ios

  6. femiveys commented on Jul 10, 2019

    @femiveys

    I also saw that if you disable something in react-native.config.js, it disables linking and autolinking.

    So what should the steps be then?

  7. thymikee commented on Jul 10, 2019

    @thymikee
    Contributor

    @xxRockOnxx @femiveys updated the comment accordingly, thanks!

  8. jivtesh813 commented on Jul 11, 2019

    @jivtesh813

    Below steps have worked for me. So I have captured those for myself, but hope will help someone. Although I can see that official documentation on React Navigation website is now changed. But if you stuck, follow below steps:

    Steps to create React-Native app with React-Navigation

    1. Create React Native app with below command. It is better if you open cmd as administrator, as one of the below jetifier commands might request that.
      react-native init AwesomeProject

    2. Go into project by:
      cd AwesomeProject
      Note - All commands in below steps will be run from this project location

    3. Install React Navigation and its handler by below 2 commands:
      npm install --save react-navigation

    npm install --save react-native-gesture-handler

    1. I have not followed next steps from React Navigation website, instead followed below steps
      Unlink for iOS with below command:
      react-native unlink react-native-gesture-handler --platforms iOS

    2. Although it did nothing and i got the message below, which is ok.
      info iOS module "react-native-gesture-handler" is not installed

    3. Create a file named react-native.config.js in the root directory of your project. Means in this case it will be under folder AwesomeProject, and this file will be adjacent to other files such as - package.json etc
      Add below code to the react-native.config.js file
      module.exports = {
      dependencies: {
      'react-native-gesture-handler': {
      platforms: {
      ios: null,
      },
      },
      },
      };

    4. After saving above file, run below command:
      npm install --save-dev jetifier

    5. Now go to project’s package.json file and add below code in Scripts section and save:
      "postinstall": "npx jetify"

    Your package.json, should look like below:
    "name": "AwesomeProject",
    "version": "0.0.1",
    "private": true,
    "scripts": {
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint .",
    "postinstall": "npx jetify"
    },

    1. Now run below command:
      npm install

    You should see something like this on console:
    Jetifier found 893 file(s) to forward-jetify. Using 4 workers…

    1. That’s it..Remember we haven’t changed MainActivity.java file, which was mentioned in ReactNavigation official site. That is not required. Just follow the above steps.

    2. Now let’s test if React Navigation working. Add below code to your App.js

    import React from "react";
    import { View, Text } from "react-native";
    import { createStackNavigator, createAppContainer } from "react-navigation";

    class HomeScreen extends React.Component {
    render() {
    return (
    <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
    Home Screen

    );
    }
    }

    const AppNavigator = createStackNavigator({
    Home: {
    screen: HomeScreen
    }
    });

    export default createAppContainer(AppNavigator);

    1. Run below command:
      react-native run-android

    It should run ok, but I got below error:
    error: bundling failed: Error: Unable to resolve module ./createNavigationContainer

    1. If you get above error as well, then delete all files from %TEMP% folder. OR you can use below command to clear cache.
      react-native start --reset-cache

    For me clearing %TEMP% folder works.

    GOOD LUCK!

  9. thymikee commented on Jul 12, 2019

    @thymikee
    Contributor

    react-native-gesture-handler is compatible with autolinking. What you need to do:

    cc @osdnk feel free to close this.

  10. doctor-mosbat commented on Aug 6, 2019

    @doctor-mosbat

    react-native-gesture-handler is compatible with autolinking. What you need to do:

    cc @osdnk feel free to close this.

    react-native unlink react-native-gesture-handler

  11. Tbaut commented on Aug 6, 2019

    @Tbaut

    Building is still failing if I don't link manually:

     * What went wrong:
    Execution failed for task ':app:compileDebugJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.
    
    * Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
    * Get more help at https://help.gradle.org
    
    BUILD FAILED in 3s
    
    error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --verbose flag for more details.
    Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
    /home/me/myApp/android/app/src/main/java/io/parity/signer/MainActivity.java:6: error: package com.swmansion.gesturehandler.react does not exist
    import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
                                             ^
    /home/me/myApp/android/app/src/main/java/io/parity/signer/MainApplication.java:28: error: cannot find symbol
              new RNGestureHandlerPackage()
                  ^
      symbol: class RNGestureHandlerPackage
    /home/me/myApp/android/app/src/main/java/io/parity/signer/MainActivity.java:38: error: cannot find symbol
                    return new RNGestureHandlerEnabledRootView(MainActivity.this);
                               ^
      symbol: class RNGestureHandlerEnabledRootView
    3 errors
    
    FAILURE: Build failed with an exception.

    I followed #671 (comment).

    • using react-native-gesture-handler@1.3.0.
    • manually unlinked with react-native unlink react-native-gesture-handler.
    • MainActivity.java is ajusted according to the guide.
    • npx jetifier runs as postinstall.
  12. doctor-mosbat commented on Aug 6, 2019

    @doctor-mosbat
    yarn add react-navigation
    # or with npm
    # npm install react-navigation
    
    yarn add react-native-gesture-handler react-native-reanimated
    # or with npm
    # npm install react-native-gesture-handler react-native-reanimated
    
    1. To finalize installation of react-native-gesture-handler for Android, be sure to make the necessary modifications to MainActivity.java:
    package com.reactnavigation.example;
    
    import com.facebook.react.ReactActivity;
    + import com.facebook.react.ReactActivityDelegate;
    + import com.facebook.react.ReactRootView;
    + import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
    
    public class MainActivity extends ReactActivity {
    
      @Override
      protected String getMainComponentName() {
        return "Example";
      }
    
    +  @Override
    +  protected ReactActivityDelegate createReactActivityDelegate() {
    +    return new ReactActivityDelegate(this, getMainComponentName()) {
    +      @Override
    +      protected ReactRootView createRootView() {
    +       return new RNGestureHandlerEnabledRootView(MainActivity.this);
    +      }
    +    };
    +  }
    }
    
    1. Finally, run react-native run-android to launch the app on your device/simulator.
  13. tapz commented on Aug 15, 2019

    @tapz

    I'm also having this error and wonder how it can be so difficult to make this lib working.

    error: package com.swmansion.gesturehandler.react does not exist
    import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
    
  14. tangtai commented on Sep 19, 2019

    @tangtai

    @tapz I am having the same problem, when i upgrade to rn 0.60. i am since in the docs for react-native gesture-handler website they didn't say they support rn > 0.60 i wonder if we have to wait for the offical support.

  15. benkane commented on Sep 23, 2019

    @benkane

    @Tbaut I had the same issue. Notice there's a line in your error output referencing MainApplication.java. You need to remove the packages.add(new RNGestureHandlerPackage()); line from MainApplication. I was also able to remove the MainActivity code. In other words, autolinking worked fine

  16. raajnadar commented on Nov 4, 2019

    @raajnadar
    Contributor

    I updated my react native cli to latest alpha and followed the steps in this guide http://facebook.github.io/react-native/docs/navigation

    yarn add react-navigation react-native-gesture-handler react-navigation-stack

    Configure your router and react-native run-android

    The autolinking worked in android (No extra setup needed in java files).

  17. rajathongal commented on Jul 24, 2020

    @rajathongal

    error: class, interface, or enum expected com.swmansion.gesturehandler.react.example;

    getting this weird error
    my MainActivity.java looks like this

    package com.example
    import com.facebook.react.ReactActivity;
    package com.swmansion.gesturehandler.react.example;
    
    
    import com.facebook.react.ReactActivityDelegate;
    import com.facebook.react.ReactRootView;
    import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
    
    public class MainActivity extends ReactActivity {
    
      /**
       * Returns the name of the main component registered from JavaScript. This is used to schedule
       * rendering of the component.
       */
      @Override
      protected String getMainComponentName() {
        return "Travel_app";
      }
      @Override
      protected ReactActivityDelegate createReactActivityDelegate() {
        return new ReactActivityDelegate(this, getMainComponentName()) {
          @Override
          protected ReactRootView createRootView() {
            return new RNGestureHandlerEnabledRootView(MainActivity.this);
          }
        };
      }
    }
    
    my MainApplication.java looks like this
    package com.example;
    import com.imagepicker.ImagePickerPackage;
    import com.facebook.react.shell.MainReactPackage;
    import android.app.Application;
    import android.content.Context;
    import com.facebook.react.PackageList;
    import com.facebook.react.ReactApplication;
    import com.facebook.react.ReactInstanceManager;
    import com.facebook.react.ReactNativeHost;
    import com.facebook.react.ReactPackage;
    import com.facebook.soloader.SoLoader;
    import java.lang.reflect.InvocationTargetException;
    import java.util.List;
    import java.util.Arrays;
    import com.swmansion.gesturehandler.react.RNGestureHandlerPackage;
    import com.swmansion.reanimated.ReanimatedPackage;
    import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
    import com.facebook.react.ReactActivity;
    import com.facebook.react.ReactRootView;
    import android.os.Bundle;
    import com.facebook.react.ReactFragmentActivity;
    import com.facebook.react.ReactActivityDelegate;
    import com.facebook.react.BuildConfig;
    
    public class MainApplication extends Application implements ReactApplication {
    
      private final ReactNativeHost mReactNativeHost =
          new ReactNativeHost(this) {
            @Override
            public boolean getUseDeveloperSupport() {
              return BuildConfig.DEBUG;
            }
    
            @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 MyReactNativePackage());
              return Arrays.<ReactPackage>asList(
                new MainReactPackage(),
                new RNGestureHandlerPackage(),
                new ReanimatedPackage(),
                new ImagePickerPackage()); // <-- add this line
            };
           
    
            @Override
            protected String getJSMainModuleName() {
              return "index";
            }
          };
    
      @Override
      public ReactNativeHost getReactNativeHost() {
        return mReactNativeHost;
      }
    
      @Override
      public void onCreate() {
        super.onCreate();
        SoLoader.init(this, /* native exopackage */ false);
        initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
      }
    
      /**
       * Loads Flipper in React Native templates. Call this in the onCreate method with something like
       * initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
       *
       * @param context
       * @param reactInstanceManager
       */
      private static void initializeFlipper(
          Context context, ReactInstanceManager reactInstanceManager) {
        if (BuildConfig.DEBUG) {
          try {
            /*
             We use reflection here to pick up the class that initializes Flipper,
            since Flipper library is not available in release mode
            */
            Class<?> aClass = Class.forName("com.travel_app.ReactNativeFlipper");
            aClass
                .getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
                .invoke(null, context, reactInstanceManager);
          } catch (ClassNotFoundException e) {
            e.printStackTrace();
          } catch (NoSuchMethodException e) {
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          } catch (InvocationTargetException e) {
            e.printStackTrace();
          }
        }
      }
    }
    
    
  18. jgonet commented on Jul 25, 2020

    @jgonet
    Member

    Should be resolved by properly set up autolinking in project.

  19. ShahbazAli206 commented on Jun 19, 2022

    @ShahbazAli206

    image
    image
    image

  20. ShahbazAli206 commented on Jun 19, 2022

    @ShahbazAli206

    i am using expo for android dev. and have tried uninstall and install the gesture-handler command using expo and npm also but not working,
    can anyone guide, how can it be resolve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions