如何为提供程序功能的构造函数参数指定Dagger 2限定符注释?

时间:2018-10-08 06:47:45

标签: android dagger-2 dagger

我定义了我的Dagger 2限定符

@Qualifier
@MustBeDocumented
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
annotation class TrackerQualifier(val value: TrackerType)

enum class TrackerType {
    INTERNAL,
    EXTERNAL
}

它很好用,

@Provides
@TrackerQualifier(TrackerType.INTERNAL)
@Singleton
UsageTracker providesInternalTracker(InternalTracker analyticsTracker) {
    return new UsageTracker(analyticsTracker);
}

@Provides
@TrackerQualifier(TrackerType.EXTERNAL)
@Singleton
UsageTracking providesExternalTracker(ExternalTracker eventTracker) {
    return eventTracker;
}

但是,如果我想将其发送到另一个注入的模块中。

@Provides
fun provideCommonImage(
        imageEngine: ImageEngine,
        externalUsageTracking: UsageTracking) =
        CommonImage(imageEngine, externalUsageTracking)

它将编译失败,因为它不知道上面的externalUsageTracking: UsageTrackingINTERNAL还是EXTERNAL。我该如何注释该参数?

1 个答案:

答案 0 :(得分:1)

尝试显示限定词。

@Provides
fun provideCommonImage(
        imageEngine: ImageEngine,
        @TrackerQualifier(TrackerType.EXTERNAL) tracker: UsageTracker) =
        CommonImage(imageEngine, tracker)