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

試著忘記壹切 提交于 2019-12-03 06:12:05

问题


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:

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.


回答1:


The name of the package associated to this error is not AirMapModule but MapsPackage from com.airbnb.android.react.maps.

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

  • the import package : import com.airbnb.android.react.maps.MapsPackage
  • the call to the constructor of the module : new MapsPackage() in function getPackages



回答2:


Go to file "MainApplication.java" (under .\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).

Fix duplicate imports as well.

Good luck




回答3:


Go to the MainAplication file.

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

  @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



回答4:


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

Add these code to your native module constructor would be fine




回答5:


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:

1- You can revert code changes in getPackages method
2- You can disable auto linking in react-native-config.js file.




回答6:


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




回答7:


Solution

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

Find method getPackages();

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

Chill Pill! :)




回答8:


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




回答9:


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



来源:https://stackoverflow.com/questions/41846452/how-to-set-canoverrideexistingmodule-true-in-react-native-for-android-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!