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

前端 未结 12 915
我寻月下人不归
我寻月下人不归 2020-12-23 18:53

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 err

相关标签:
12条回答
  • 2020-12-23 19:38

    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
    0 讨论(0)
  • 2020-12-23 19:43

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

    0 讨论(0)
  • 2020-12-23 19:44

    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.

    0 讨论(0)
  • 2020-12-23 19:45

    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;    
    }   
    
    0 讨论(0)
  • 2020-12-23 19:45

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

    0 讨论(0)
  • 2020-12-23 19:46

    The above solutions are all correct, but let me explain a little bit,some of above solutions suggest to override the following method.

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

    But question is where to override? first of all, you can't override inside MainActivity.java or MainApplication.java file.

    You should override it in the class inside some node_modules project folder and that class will be extending from ReactContextBaseJavaModule class.

    In my case, it was not getting repeated in imports/adding duplicate packages but it was mainly because of auto linking at the and that was making it to repeat.

    I am using react-native-contacts npm package to interact so what I did is that went inside

    node_modules\react-native-contacts\android\src\main\java\comrt2zz\reactnativecontacts\ 
    ContactsManager.java 
    

    and this ContactsManager was extending from the ReactContextBaseJavaModule and I override there and got the problem resolved.

    So as general there could be a lot of classes that will be extending from ReactContextBaseJavaModule under different projects inside node_modules, but you need to go for specific a project that will be creating duplication problem and there you should override it.

    0 讨论(0)
提交回复
热议问题