我已经研究了有关此问题的多个帖子,但是建议的解决方案似乎都无法解决我的问题。我看过的一些帖子在下面提供
但是我不知道我在做什么错。这是我的设置。我要在两个单独的类中注入一个名为NotificationManager
的类。情况在下面给出
class PushJobIntentService extends JobIntentService {
@Inject
PushService pushService
@Override
public void onCreate()
{
AndroidInjection.inject(this);
super.onCreate();
}
@Override
protected void onHandleWork(@NonNull Intent intent)
{
.. Do Work . . .
}
}
PushService是一个简单的POJO,其实例依赖关系图如下。 NotificationManager通过构造函数注入PushService。
PushService -> NotificationManager -> (Context, Map <String, NotificationBuilder>)
在此示例中,PushService依赖于NotificationManager,后者依赖于Android Context,而Map则依赖于Map(在这种情况下,NotificationBuilder是一个接受Context作为构造函数参数的POJO)。
我们有一个通用的AppModule,其中定义了所有依赖项,这就是它的设置方式
@ContributesAndroidInjector(modules = {PushServiceModule.class})
abstract PushJobIntentService contributePushJobIntentServiceInjector();
PushServiceModule
注入依赖项,如下所示:
@Module(includes = {NotificationManagerModule.class})
public abstract class PushServiceModule
{
. . . Dependenies of PushService . . .
}
NotificationManagerModule
@Module()
public abstract class NotificationManagerModule
{
@Provides
static Map<String, NotificationBuilder> provideNotificationBuilders(Context context)
{
Map<String, NotificationBuilder> builders = new HashMap<>();
builders.put("Key1",new NotificationBuilder(context, "Key1", "Text1"));
return Collections.unModifiableMap(builders);
}
}
第二个依赖图如下
AppSignOutHelper -> LoggedOutTask (Async Task) -> NotificationManager
我正在尝试将NotificationManager注入LoggedOutTask。 AppSignOutHelper实现SignOutHelper,其定义如下
/**
* Binds the {@link AppSignOutHelper} instance to the shared {@link SignOutHelper} interface.
*/
@Binds
@Qualifier
abstract SignOutHelper bindSignOutHelper(AppSignOutHelper appSignOutHelper);
问题
当我尝试编译项目时,这是我得到的错误
/Users/username/sourcecode/android/app/src/main/java/com/myproject/mobile/dagger/AppComponent.java:50: warning: [Dagger/DuplicateBindings] com.myproject.android.notifications.NotificationManager is bound multiple times:
public interface AppComponent extends DomainComponent
^
@Inject com.myproject.android.notifications.NotificationManager(android.content.Context, Map<String,com.myproject.android.notifications.NotificationBuilder>) [com.myproject.android.dagger.AppComponent]
@Provides com.myproject.android.notifications.NotificationManager com.myproject.android.notifications.NotificationManagerModule.provideNotificationManager(android.content.Context, Map<String,com.myproject.android.notifications.NotificationBuilder>) [com.myproject.android.dagger.AppComponent → com.myproject.android.dagger.AppModule_ContributePushJobIntentServiceInjector.PushJobIntentServiceSubcomponent]
This condition was never validated before, and will soon be an error. See https://dagger.dev/conflicting-inject.
com.myproject.android.notifications.NotificationManager is injected at
com.myproject.android.notifications.PushService( myproject.NotificationManager)
com.myproject.android.notifications.PushService is injected at
com.myproject.android.notifications.PushJobIntentService.pushService
com.myproject.android.notifications.PushJobIntentService is injected at
dagger.android.AndroidInjector.inject(T) [com.myproject.android.dagger.AppComponent → com.myproject.android.dagger.AppModule_ContributePushJobIntentServiceInjector.PushJobIntentServiceSubcomponent]
/Users/username/sourcecode/android/app/src/main/java/com/myproject/mobile/dagger/AppComponent.java:50: error: [Dagger/MissingBinding] java.util.Map<java.lang.String,com.myproject.android.notifications.NotificationBuilder> cannot be provided without an @Provides-annotated method.
public interface AppComponent extends DomainComponent
^
A binding with matching key exists in component: com.myproject.android.dagger.AppModule_ContributePushJobIntentServiceInjector.PushJobIntentServiceSubcomponent
java.util.Map<java.lang.String,com.myproject.android.notifications.NotificationBuilder> is injected at
com.myproject.android.notifications.NotificationManager(…, notifBuilderMap)
com.myproject.android.notifications.NotificationManager is injected at
com.myproject.android.notifications.CreateLoggedOutNotificationTask(…, myproject.NotificationManager)
javax.inject.Provider<com.myproject.android.notifications.CreateLoggedOutNotificationTask> is injected at
com.myproject.android.AppSignOutHelper(…, loggedOutNotificationTask, …)
com.myproject.android.AppSignOutHelper is injected at
com.myproject.android.dagger.AppModule.bindSignOutHelper(appSignOutHelper)
com.myproject.android.SignOutHelper is injected at
com.myproject.android.dagger.DomainModule.provideSignOutHelper(…, optionalInstance)
com.myproject.android.SignOutHelper is provided at
com.myproject.android.dagger.DomainComponent.getSignOutHelper()
1 error
1 warning
但是,如果我没有在PushServiceModule中包含NotificationManagerModule
,请按照以下步骤进行操作:
@Module()
public abstract class PushServiceModule
{
. . . Dependenies of PushService . . .
}
我收到以下错误
/Users/username/sourcecode/android/app/src/main/java/com/myproject/mobile/dagger/AppComponent.java:50: error: [Dagger/MissingBinding] java.util.Map<java.lang.String,com.myproject.android.notifications.NotificationBuilder> cannot be provided without an @Provides-annotated method.
public interface AppComponent extends DomainComponent
^
java.util.Map<java.lang.String,com.myproject.android.notifications.NotificationBuilder> is injected at
com.myproject.android.notifications.NotificationManager(…, notifBuilderMap)
com.myproject.android.notifications.NotificationManager is injected at
com.myproject.android.notifications.CreateLoggedOutNotificationTask(NotificationManager)
javax.inject.Provider<com.myproject.android.notifications.CreateLoggedOutNotificationTask> is injected at
com.myproject.android.AppSignOutHelper(…, loggedOutNotificationTask, …)
com.myproject.android.AppSignOutHelper is injected at
com.myproject.android.dagger.AppModule.bindSignOutHelper(appSignOutHelper)
com.myproject.android.SignOutHelper is injected at
com.myproject.android.dagger.DomainModule.provideSignOutHelper(…, optionalInstance)
com.myproject.android.SignOutHelper is provided at
com.myproject.android.dagger.DomainComponent.getSignOutHelper()
The following other entry points also depend on it:
dagger.android.AndroidInjector.inject(T) [com.myproject.android.dagger.AppComponent → com.myproject.android.dagger.AppModule_ContributePushJobIntentServiceInjector.PushJobIntentServiceSubcomponent]
1 error
期望的行为
我希望能够将NotificationManager
注入到CreateLoggedOutTask
中,然后将其注入AppSignOutHelper
中。我在做什么错了?