I have used react-native-community/datetimepicker to implement the date picker in my project, I have no issues in iOS, but in android I have some issues in my MainApplication.java, someone help me to fix the ERROR, I have attached my MainApplication.java file here, I have tried all the possibilities I can but I am failed to fix this issue
package com.rndemo;
import android.app.Application;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint;
import com.facebook.react.defaults.DefaultReactNativeHost;
import com.facebook.soloader.SoLoader;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost =
new DefaultReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// packages.add(new RNDatePickerPackage());
return packages;
}
@Override
protected String getJSMainModuleName() {
return "index";
}
@Override
protected boolean isNewArchEnabled() {
return BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
}
@Override
protected Boolean isHermesEnabled() {
return BuildConfig.IS_HERMES_ENABLED;
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
// If you opted-in for the New Architecture, we load the native entry point for this app.
DefaultNewArchitectureEntryPoint.load();
}
ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
}
}
You can do the following as a temporary fix:
Go to node_modules/react-native-date-picker/android/src/main/java
and open the DatePickerModule.java
There you can add the following:
@Override
public boolean canOverrideExistingModule() {
return true;
}
It should end up looking like this.
This tells Android that if it sees the same package during build it should override it and you will end up having only 1 reference of the package.
The issue should be solved. Try recompiling Android. ✅
Additionally, you can use patch-package to keep this change after doing a clean (i.e. removing node_modules
and doing a fresh install).
To extend this answer - this solution can be also applied to the same issue but for other libraries. The important part is to add the above code to the class that extends ReactContextBaseJavaModule
.
So find the class that extends ReactContextBaseJavaModule
for your specific library, add the code above, and rebuild.
Hope this helps!