Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 818
UtkarshPramodGupta Avatar asked Jan 25 '17 08:01

UtkarshPramodGupta


People also ask

Should I use reactcontextbasejavamodule or reactapplicationcontext(RAC)?

However we recommend that you use ReactContextBaseJavaModule, as shown above. ReactContextBaseJavaModule gives access to the ReactApplicationContext (RAC), which is useful for Native Modules that need to hook into activity lifecycle methods.

What are Java Native modules in React Native?

For Android, Java native modules are written as classes that extend ReactContextBaseJavaModule and implement the functionality required by JavaScript. It is worth noting that technically Java classes only need to extend the BaseJavaModule class or implement the NativeModule interface to be considered a Native Module by React Native.

How do I register the calendarmodule in React Native host?

To register the CalendarModule package, you must add MyAppPackage to the list of packages returned in ReactNativeHost's getPackages () method. Open up your MainApplication.java or MainApplication.kt file, which can be found in the following path: android/app/src/main/java/com/your-app-name/.

Where to override react context base module?

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.


2 Answers

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
like image 103
efx Avatar answered Sep 21 '22 19:09

efx


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.

like image 38
Barak Kakoun Avatar answered Sep 22 '22 19:09

Barak Kakoun