Native module RNDatePicker tried to override RNDatePicker. Check the getPackages() method in MainApplication.java

huangapple go评论84阅读模式
英文:

Native module RNDatePicker tried to override RNDatePicker. Check the getPackages() method in MainApplication.java

问题

我已经使用react-native-community/datetimepicker在我的项目中实现了日期选择器,iOS没有问题,但在Android中,我的MainApplication.java文件出现了一些问题,有人帮我修复错误吗?我已经附上了我的MainApplication.java文件在这里,我尝试了我所能想到的所有可能性,但都未能解决这个问题。

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) {
      // 如果您选择了新的架构,我们将加载此应用程序的本机入口点。
      DefaultNewArchitectureEntryPoint.load();
    }
    ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager());
  }
}

希望这可以帮助您解决问题。

英文:

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&lt;ReactPackage&gt; getPackages() {
@SuppressWarnings(&quot;UnnecessaryLocalVariable&quot;)
List&lt;ReactPackage&gt; packages = new PackageList(this).getPackages();
// packages.add(new RNDatePickerPackage());
return packages;
}
@Override
protected String getJSMainModuleName() {
return &quot;index&quot;;
}
@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());
}
}

答案1

得分: 1

以下是翻译好的内容:

你可以作为临时解决方案执行以下操作:

进入 node_modules/react-native-date-picker/android/src/main/java 并打开 DatePickerModule.java 文件。

在那里,你可以添加以下内容:

@Override    
public boolean canOverrideExistingModule() {        
   return true;    
}  

最终应该看起来像这样。

Native module RNDatePicker tried to override RNDatePicker. Check the getPackages() method in MainApplication.java

这告诉 Android,如果它在构建过程中看到相同的包,应该覆盖它,这样你最终只会有一个包的引用。

问题应该已解决。尝试重新编译 Android。 ✅

此外,你可以使用 patch-package 在进行干净安装(即删除 node_modules 并重新安装)后保留此更改。

扩展这个答案 - 这个解决方案也可以应用于其他库的相同问题。重要的是将上面的代码添加到扩展了 ReactContextBaseJavaModule 的类中。

因此,找到扩展了 ReactContextBaseJavaModule 的类,添加上述代码,然后重新构建。

希望能有所帮助!

英文:

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.

Native module RNDatePicker tried to override RNDatePicker. Check the getPackages() method in MainApplication.java

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!

huangapple
  • 本文由 发表于 2023年6月26日 19:27:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定