具有相同返回类型的@Named提供程序最终都会给出java.lang.IllegalArgumentException:Duplicate
2022-09-01 21:28:07
这是问题所在,
我正在研究一个LibGDX项目,其中我有针对不同平台的不同模块。
这是我的安卓模块的样子:
@Module(
includes = {BaseModule.class, NetModule.class},
injects = {DummyProjectActivity.class, DummyProject.class},
overrides = true)
public class DummyProjectAndroidModule {
...
@Provides @Singleton @Named("DummyOne")
DummyInterface provideDummyOne() {
return new DummyOne();
}
@Provides @Singleton @Named("DummyTwo")
DummyInterface provideDummyTwo() {
return new DummyTwo();
}
@Provides @Singleton @Named("DummyConsumer")
DummyConsumer provideDummyConsumer(@Named("DummyOne") DummyInterface dummyOne,
@Named("DummyTwo") DummyInterface dummyTwo) {
return new DummyConsumer(dummyOne, dummyTwo);
}
}
..以及我的桌面模块的外观:
@Module(
includes = {BaseModule.class, NetModule.class},
injects = {DummyProjectDesktop.class, DummyProject.class},
overrides = true)
public class DummyProjectDesktopModule {
好好休息差不多一样。然而,当我为桌面构建项目时,一切都很好,在Android方面,我得到了这个错误,这让我仍然感到震惊。
Process: net.alicanhasirci.mobile.DummyProject.android, PID: 4603
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.alicanhasirci.mobile.DummyProject.android/net.alicanhasirci.mobile.DummyProject.android.DummyProjectActivity}: java.lang.IllegalArgumentException: Duplicate:
net.alicanhasirci.mobile.android.image.DummyInterface net.alicanhasirci.mobile.DummyProject.android.DummyProjectAndroidModule.provideDummyOne()
net.alicanhasirci.mobile.android.image.DummyInterface net.alicanhasirci.mobile.DummyProject.android.DummyProjectAndroidModule.provideDummyTwo()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
at android.app.ActivityThread.access$800(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
Caused by: java.lang.IllegalArgumentException: Duplicate:
net.alicanhasirci.mobile.android.image.DummyInterface net.alicanhasirci.mobile.DummyProject.android.DummyProjectAndroidModule.provideDummyOne()
net.alicanhasirci.mobile.android.image.DummyInterface net.alicanhasirci.mobile.DummyProject.android.DummyProjectAndroidModule.provideDummyTwo()
at dagger.internal.UniqueMap.put(UniqueMap.java:29)
at dagger.internal.plugins.reflect.ReflectiveModuleAdapter.handleBindings(ReflectiveModuleAdapter.java:104)
at dagger.internal.plugins.reflect.ReflectiveModuleAdapter.getBindings(ReflectiveModuleAdapter.java:89)
at dagger.ObjectGraph$DaggerObjectGraph.makeGraph(ObjectGraph.java:174)
at dagger.ObjectGraph$DaggerObjectGraph.access$000(ObjectGraph.java:132)
at dagger.ObjectGraph.create(ObjectGraph.java:129)
at net.alicanhasirci.mobile.DummyProject.android.DummyProjectActivity.onCreate(DummyProjectActivity.java:137)
at android.app.Activity.performCreate(Activity.java:5977)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
现在快速浏览一下匕首源代码,我可以看到限定符注释是通过反射收集的,并用作绑定名称的前缀,这些前缀稍后将用作UniqueMap中的键。我的问题似乎发生在这个区域的某个地方,我的限定符没有以某种方式得到处理,但我的桌面构建工作没有问题......
以下是一些其他内容:
ObjectGraph objectGraph = ObjectGraph.create(new DummyProjectAndroidModule());
objectGraph.inject(this);
dp = objectGraph.get(DummyProject.class);
是我获取对象的方式,它具有.因此:DummyProject
DummyConsumer
@Inject @Named("DummyConsumer") DummyConsumer consumer;
我已将返回类型更改为具体类作为解决方法,但没有人喜欢解决方法,因为我们都知道它们一直困扰着您直到最后。