skip to Main Content

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());
  }
}

2

Answers


  1. I’m having the exact same issue. In my case, I installed react-native-community/datetimepicker because the library react-native-wheel-pick-2 needs it. I think it is also worth mentioning that I have another datepicker library installed (react-native-date-picker). But I’m not sure using two datepicker libraries is the problem since you are experiencing the same issue with just one datepicker library. Hope someone can help us. Please let me know if you get to fix it and I’ll do the same.

    Login or Signup to reply.
  2. 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.

    enter image description here

    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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search