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.

UtkarshPramodGupta
  • 7,486
  • 7
  • 30
  • 54

14 Answers14

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
efx
  • 1,655
  • 1
  • 14
  • 12
  • 7
    The first bullet is mentioned everywhere online, but this was the first mention of the second place to look for duplicates. Thank you. – David Sinclair Jun 16 '17 at 21:37
  • 2
    This error rise when you are trying to import same package twice in MainApplication.java file – Digvijay Machale Feb 12 '18 at 05:26
  • 1
    this works but everytime i link a library it repeat itself – Billy Koswara Mar 06 '18 at 02:37
  • 1
    just remove the duplicate imports from MainApplication.java – John May 04 '18 at 19:48
  • This answer is most assuredly the one you're looking for if you ended up here – Khaldor Mar 10 '19 at 17:38
  • If you still get the error even after removing duplicates, you may need to remove (comment) `packages.add(new MyReactNativePackage());` lines for the packages that are already auto-linked (no manual installation) – Gürol Canbek Jan 16 '21 at 14:28
  • Could someone please take a look at my question: https://stackoverflow.com/questions/69201505/react-native-native-module-nativeunimoduleproxy-tried-to-override-nativeunimodu – Say u Sep 16 '21 at 01:51
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.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
Barak Kakoun
  • 443
  • 4
  • 6
21

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.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
Andrew Fan
  • 274
  • 2
  • 6
  • 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 '19 at 11:10
  • 7
    Can someone please be more specific about this solution? Please tell us what and where ... – T M Aug 20 '19 at 07:38
  • 3
    Thank you @Andew Fan for actually answering the question – mcabe Oct 09 '19 at 00:08
  • 1
    @ThembelaniM you can add the override function to the native module's class that extends `ReactContextBaseJavaModule` – mcabe Oct 09 '19 at 00:09
  • 3
    Where exactly should one add this snippet? – sudonitin Jul 19 '20 at 11:38
  • 1
    Can you check if this goes into the MainApplication.java? Because upon adding this I get an error saying that you can not override a method that is not present in the supertype – Shreyas Shandilya Aug 08 '20 at 10:42
  • @ShreyasShandilya please look at this answer I just added, https://stackoverflow.com/questions/41846452/how-to-set-canoverrideexistingmodule-true-in-react-native-for-android-apps/63438974#63438974 – Irshad Babar Aug 16 '20 at 16:05
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
Laura
  • 8,100
  • 4
  • 40
  • 50
HM Hamza Zubair
  • 201
  • 3
  • 4
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.

Mahdi Ghajary
  • 2,717
  • 15
  • 20
11

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Irshad Babar
  • 1,311
  • 19
  • 26
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;    
}   
3

You can remove your package from MainApplication.java

enter image description here

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
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.

Nguyên Hoàng
  • 1,043
  • 9
  • 11
1

Solution

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

Find method getPackages();

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

Chill Pill! :)

Ali Akram
  • 4,803
  • 3
  • 29
  • 38
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.

Bumuthu Dilshan
  • 430
  • 5
  • 14
0

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

caopeng
  • 914
  • 13
  • 23
-1

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

-1

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

Md. Juyel Rana
  • 540
  • 5
  • 10