我正在通过他们的教程学习Dagger2,但在part 5被阻止。我已经在Kotlin(jvm)中实现了所有内容,并且逻辑/行为与工作中的教程相符。
但是,我无法创建SystemOutModule
,并且不确定我是否误将Dagger或在翻译其模块类时弄错了。
/** * Tutorial's Java. */ @Module abstract class SystemOutModule { @Provides static Outputter textOutputter() { return System.out::println; } }
/** * My Kotlin. */ @Module object SystemOutModule { @JvmSuppressWildcards @Provides fun textOutputter(): (String) -> Unit = { println(it) } }
构建失败,出现此错误:
~~ ./gradlew clean build
> Task :app:kaptKotlin FAILED
e: /Users/eric/IdeaProjects/atm/app/build/tmp/kapt3/stubs/main/com/es0329/atm/CommandRouterFactory.java:7: error: [Dagger/MissingBinding] com.es0329.atm.Outputter cannot be provided without an @Provides-annotated method.
public abstract interface CommandRouterFactory {
^
com.es0329.atm.Outputter is injected at
com.es0329.atm.HelloWorldCommand(outputter)
com.es0329.atm.HelloWorldCommand is injected at
com.es0329.atm.HelloWorldModule.helloWorldCommand(command)
com.es0329.atm.Command is injected at
com.es0329.atm.CommandRouter(command)
com.es0329.atm.CommandRouter is provided at
com.es0329.atm.CommandRouterFactory.router()
FAILURE: Build failed with an exception.
我正在通过他们的教程学习Dagger2,但是在第5部分中受阻。我已经在Kotlin(jvm)中实现了所有功能,并且逻辑/行为在此方面与该教程相匹配。但是,我是...
您的HelloWorldCommand
需要一个Outputter
,但您提供的却是(String) -> Unit
。有几种方法可以弥合这种差距: