74

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:

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.

14 Answers 14

114

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
7
  • 7
    The first bullet is mentioned everywhere online, but this was the first mention of the second place to look for duplicates. Thank you. Jun 16, 2017 at 21:37
  • 2
    This error rise when you are trying to import same package twice in MainApplication.java file Feb 12, 2018 at 5:26
  • 1
    this works but everytime i link a library it repeat itself Mar 6, 2018 at 2:37
  • 1
    just remove the duplicate imports from MainApplication.java
    – John
    May 4, 2018 at 19:48
  • This answer is most assuredly the one you're looking for if you ended up here
    – Khaldor
    Mar 10, 2019 at 17:38
43

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.

0
23

Open the MainApplication.java file by this address: android/app/src/main/java/com/projectName/MainApplication.java and add the following code to MainApplication.java file:

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

And everything became correct.

7
  • 10
    What do you mean exactly by native module? library? I'm getting an error "module does not override method from it's superclass"
    – Lucky_girl
    Apr 11, 2019 at 11:10
  • 7
    Can someone please be more specific about this solution? Please tell us what and where ...
    – T M
    Aug 20, 2019 at 7:38
  • 3
    Thank you @Andew Fan for actually answering the question
    – mcabe
    Oct 9, 2019 at 0:08
  • 1
    @ThembelaniM you can add the override function to the native module's class that extends ReactContextBaseJavaModule
    – mcabe
    Oct 9, 2019 at 0:09
  • 3
    Where exactly should one add this snippet?
    – sudonitin
    Jul 19, 2020 at 11:38
15

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
12

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.

1
  • Ok, thanks a lot, this is exactly what I was looking for. it worked for me, for react-native-contacts
    – Albin Rdz
    Aug 1, 2022 at 19:19
11

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.

2
  • I was doing exactly same as you mentioned :). Thanks buddy.
    – Bimal Grg
    Sep 4, 2019 at 9:24
  • in v0.61.5, this was the answer for me. very un-suspecting
    – Jim
    Aug 3, 2020 at 23:21
4

You can remove your package from MainApplication.java

enter image description here

2
3

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

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.

8
  • There is neither a package nor even a variable by the name AirMapModule in the file MailApplication.java :( Jan 25, 2017 at 9:16
  • what version of react-native you use ? Jan 25, 2017 at 9:22
  • react-native-cli: 2.0.1 react-native: 0.36.0 Jan 25, 2017 at 9:43
  • Did you try to check file MainActivity.java in the same directory too ? Jan 25, 2017 at 10:25
  • Yeah, i just did. Even that doesn't have anything related to AirMapModule. :( Jan 25, 2017 at 10:58
1

Solution

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

Find method getPackages();

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

Chill Pill! :)

1
1

Add only the modules which are not autolinked at here,

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

If the module is autolinked and added the module here, you will get this error.

0

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

0

just remove packages.add(new MapsPackage()); this line MainApplication.java

-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.