包com.reactnativenavigation不存在

时间:2019-04-25 12:15:25

标签: android react-native react-native-navigation

我是React Native的初学者。现在,我正在尝试将https://wix.github.io/react-native-navigation/#/库集成到我的本机应用程序中。目前,我仅针对Android平台。

我已经执行了文档中提到的所有步骤,但是当我尝试使用react-native run-android在应用程序上运行时,会出现以下错误。

error: package com.reactnativenavigation does not exist

我理解错误的含义,它表示此软件包在我的项目中不存在,但我是从npm安装的,并且它存在于节点模块中。

我正在使用最新版本的Node.js和npm,下面是package.json依赖项。

 "dependencies": {
    "react": "16.8.3",
    "react-native": "^0.59.6",
    "react-native-navigation": "^2.18.2",
    "react-native-vector-icons": "^6.4.2",
    "react-redux": "^7.0.2",
    "redux": "^4.0.1"
},
"devDependencies": {
    "@babel/core": "^7.4.3",
    "@babel/runtime": "^7.4.3",
    "babel-jest": "^24.7.1",
    "jest": "^24.7.1",
    "metro-react-native-babel-preset": "^0.53.1",
    "react-test-renderer": "16.8.3"
},

完全错误:

E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainActivity.java:19: error: package com.reactnativenavigation does not exist
import com.reactnativenavigation.NavigationActivity;
                                ^
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainActivity.java:21: error: cannot find symbol
public class MainActivity extends NavigationActivity {
                                  ^
  symbol: class NavigationActivity
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:12: error: package com.reactnativenavigation does not exist
import com.reactnativenavigation.NavigationApplication;
                                ^
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:13: error: package com.reactnativenavigation.react does not exist
import com.reactnativenavigation.react.NavigationReactNativeHost;
                                      ^
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:14: error: package com.reactnativenavigation.react does not exist
import com.reactnativenavigation.react.ReactGateway;
                                      ^
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:20: error: cannot find symbol
public class MainApplication extends NavigationApplication {
                                     ^
  symbol: class NavigationApplication
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:54: error: cannot find symbol
    protected ReactGateway createReactGateway() {
              ^
  symbol:   class ReactGateway
  location: class MainApplication
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:53: error: method does not override or implement a method from a supertype
  @Override
  ^
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:55: error: cannot find symbol
        ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
                                   ^
  symbol:   class NavigationReactNativeHost
  location: class MainApplication
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:56: error: method does not override or implement a method from a supertype
            @Override
            ^
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:61: error: cannot find symbol
        return new ReactGateway(this, isDebug(), host);
                   ^
  symbol:   class ReactGateway
  location: class MainApplication
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:64: error: method does not override or implement a method from a supertype
    @Override
    ^
E:\data\code\practise\react-native\reactNativeStart\android\app\src\main\java\com\reactnativestart\MainApplication.java:77: error: method does not override or implement a method from a supertype
    @Override
    ^
13 errors

5 个答案:

答案 0 :(得分:2)

我不认为降级是一个好的解决方案。我遇到了这个问题,我假设您仍在使用React Native 60+(在我的情况下为0.61)。我通过忽略除RN 60以外的所有以前版本来解决此问题。我相信它与if块中列出的所有版本的插件有关。

因此,在您的android/build.gradle中,代码位于底部下方

subprojects { subproject ->
    afterEvaluate {
        if ((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
            android {
                variantFilter { variant ->
                    def names = variant.flavors*.name
                    if (
                            names.contains("reactNative51") ||
                            names.contains("reactNative55") ||
                            names.contains("reactNative56") ||
                            names.contains("reactNative57") ||
                            names.contains("reactNative57_5") 
                            // names.contains("reactNative60")
                    ) {
                        setIgnore(true)
                    }
                }
            }
        }
    }
}

MainApplication.java中约有无法识别的符号之后,您可能会遇到错误,其中文档也提供了错误的信息。

下面的MainApplication.java中应该是您的MainApplication方法。我已将getPackages()方法更改为RN的原始方法,就像在RN 60中一样,RNN使用的方法具有类型错误

public class MainApplication extends NavigationApplication {

    @Override
    protected ReactGateway createReactGateway() {
        ReactNativeHost host = new NavigationReactNativeHost(this, isDebug(), createAdditionalReactPackages()) {
            @Override
            protected String getJSMainModuleName() {
                return "index";
            }
        };
        return new ReactGateway(this, isDebug(), host);
    }

    @Override
    public boolean isDebug() {
        return BuildConfig.DEBUG;
    }

    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 packages;
    }

    @Override
    public List<ReactPackage> createAdditionalReactPackages() {
        return getPackages();
    }
}

答案 1 :(得分:1)

它具有react-native-navigation软件包错误。套件无法正确导入,请使用android studio。如果有任何软件包问题,android stdio将显示该问题。

最好的方法是使用android studio

  

打开android studio->打开项目android文件夹->并同步项目->转到Mainapplication.java->您可以通过android studio导入插件。

再次同步,它将解决此问题

答案 2 :(得分:0)

我也有问题,我真的不知道这是什么。我重新安装react native项目并重复手册(https://wix.github.io/react-native-navigation/#/docs/Installing?id=android)中的所有步骤。之后,“构建”开始工作))

答案 3 :(得分:0)

就我而言,我更改了一些局部环境变量。正在运行:

yarn install

在我的本机目录中完成了这项任务。建立正常。如果您使用的是npm:应该是:

npm install

答案 4 :(得分:-1)

我有完全一样的问题。我尝试了所有结果,没有结果。我的react native版本是0.60.0,我将其降级到0.57.0,重复https://wix.github.io/react-native-navigation/#/docs/Installing?id=android中的步骤,一切都很好。