Java 如何在 React Native for Android 应用程序中设置 canOverrideExistingModule=true?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41846452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:43:24  来源:igfitidea点击:

How to set canOverrideExistingModule=true in React Native for Android Apps?

javaandroidreact-nativeavd

提问by UtkarshPramodGupta

I built an android app using React Native, it got built successfully but when I run the app in my Android Virtual Device it shows up a full red screen with the following error:

我使用 React Native 构建了一个 android 应用程序,它构建成功,但是当我在我的 Android 虚拟设备中运行该应用程序时,它显示一个完整的红色屏幕,并显示以下错误:

enter image description here

在此处输入图片说明

I have not done native app development ever before neither do I have any knowledge of Java so I have no idea what this error means and how to fix this.

我以前从未做过本机应用程序开发,也没有任何 Java 知识,所以我不知道这个错误意味着什么以及如何解决这个问题。

采纳答案by efx

The name of the package associated to this error is not AirMapModulebut MapsPackagefrom com.airbnb.android.react.maps.

有关这个错误的包的名称不是AirMapModule,但MapsPackage距离com.airbnb.android.react.maps

In your MainApplication.javain directory : android/app/src/main/java/../../remove any duplicate entry of :

在您的MainApplication.javain 目录中:android/app/src/main/java/../../删除以下任何重复条目:

  • the import package : import com.airbnb.android.react.maps.MapsPackage
  • the call to the constructor of the module : new MapsPackage()in function getPackages
  • 导入包: import com.airbnb.android.react.maps.MapsPackage
  • 对模块构造函数的调用:new MapsPackage()在函数中getPackages

回答by Nguyên Hoàng

You can try check in file MainApplication.javain directory : android\app\src\main\javais have any duplicate package AirMapModule exist or not, and delete 1 if have.

您可以尝试签入MainApplication.java目录中的文件:android\app\src\main\java是否存在任何重复的包AirMapModule,如果存在则删除1。

回答by Barak Kakoun

Go to file "MainApplication.java" (under .\android\app\src\main\java\com\projectName)

转到文件“MainApplication.java”(在 .\android\app\src\main\java\com\projectName 下)

Make sure that under getPackages() function you don't have duplicate lines (in my case I had "new MapsPackage()" twice).

确保在 getPackages() 函数下没有重复的行(在我的情况下,我有两次“new MapsPackage()”)。

Fix duplicate imports as well.

修复重复导入。

Good luck

祝你好运

回答by Andrew Fan

  @Override    
  public boolean canOverrideExistingModule() {        
    return true;    
  }   

Add these code to your native module constructor would be fine

将这些代码添加到您的本机模块构造函数中就可以了

回答by HM Hamza Zubair

Go to the MainAplication file.

转到MainAplication 文件

Remove duplicate package and remove duplicate package in getPackages()method

删除重复包并删除getPackages()方法中的重复包

  @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
            new VectorIconsPackage()
      );
    }

Then after try this command in your terminal :

然后在终端中尝试此命令后:

  • cd android
  • ./gradlew clean
  • 光盘安卓
  • ./gradlew 干净

回答by caopeng

check your MainApplication.java, in particular protected List<ReactPackage> getPackages(); the AirMapModuleis probably twice in the list

检查您的MainApplication.java,特别是protected List<ReactPackage> getPackages(); 在AirMapModule大概两倍列表

回答by Mahdi

If the version of RN you're using is >= 0.60 then there is the possibility that auto-linking and your manual linking are doing the same thing twice. You have two options:

如果您使用的 RN 版本 >= 0.60,那么自动链接和手动链接可能会做两次同样的事情。您有两个选择:

1- You can revert code changes in getPackagesmethod
2- You can disable auto linking in react-native-config.jsfile.

1- 您可以在getPackages方法
2 中恢复代码更改- 您可以禁用react-native-config.js文件中的自动链接。

回答by Sittisuk Chartrasee

if installed library react-navigation you can run via android studio. else remove library react-navigation and just yarn it's will work.

如果安装了库 react-navigation,您可以通过 android studio 运行。否则删除库反应导航,只需纱线即可。

回答by Ali Akram

Solution

解决方案

Goto android/app/src/main/java/YOURPACKAGE/MainApplication.java

android/app/src/main/java/YOURPACKAGE/MainApplication.java

Find method getPackages();

查找方法 getPackages();

Remove this packages.add(new MapsPackage());

删除这个 packages.add(new MapsPackage());

Chill Pill! :)

清凉丸!:)

回答by Rishijay Shrivastava

Go to your module (coz of which you are getting this error message) Open the module.. add this code to it...

转到您的模块(因为您收到此错误消息)打开模块.. 将此代码添加到其中...

@Override    
public boolean canOverrideExistingModule() {        
  return true;    
}