How to set canOverrideExistingModule=true in React Native for Android Apps?
JavaAndroidReact NativeAvdJava Problem Overview
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.
Java Solutions
Solution 1 - Java
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 functiongetPackages
Solution 2 - Java
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.
Solution 3 - Java
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.
Solution 4 - Java
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
Solution 5 - Java
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.
Solution 6 - Java
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.
Solution 7 - Java
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;
}
Solution 8 - Java
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.
Solution 9 - Java
Solution
Goto android/app/src/main/java/YOURPACKAGE/MainApplication.java
Find method getPackages();
Remove this packages.add(new MapsPackage());
Chill Pill! :)
Solution 10 - Java
You can remove your package from MainApplication.java
Solution 11 - Java
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.
Solution 12 - Java
check your MainApplication.java
, in particular protected List<ReactPackage> getPackages()
; the AirMapModule
is probably twice in the list
Solution 13 - Java
just remove packages.add(new MapsPackage());
this line MainApplication.java
Solution 14 - Java
if installed library react-navigation you can run via android studio. else remove library react-navigation and just yarn it's will work.