我是一名刚进入 Kotlin 的 Android 开发人员,我一直在尝试在 Kotlin 中设置 Mvp Dagger 应用程序,但我无法弄清楚发生了什么。我继续收到此错误:
Error:dagger.internal.codegen.ComponentProcessor was unable to process this interface
because not all of its dependencies could be resolved.
Check for compilation errors or a circular dependency with generated code.
这是我的代码:
Kotlin应用:
class KotlinApplication: MultiDexApplication(), HasActivityInjector {
@Inject lateinit var injector: DispatchingAndroidInjector<Activity>
override fun onCreate() {
super.onCreate()
inject()
}
override fun activityInjector(): AndroidInjector<Activity> {
return injector
}
fun inject() {
DaggerAppComponent.builder().application(this).build().inject(this);
}
}
应用组件:
@Singleton
@Component(modules = arrayOf(AppModule::class, InjectionModule::class))
interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
fun application(app: KotlinApplication): Builder
fun build(): AppComponent
}
fun inject(app: KotlinApplication)
}
应用模块:
@Module
class AppModule {
@Singleton
@Provides
fun provideAppContext(app: KotlinApplication): Context {
return app
}
@Singleton
@Provides
fun provideApplication(app: KotlinApplication): Application {
return app
}
}
注入模块:
@Module
abstract class InjectionModule {
@ActivityScope
@ContributesAndroidInjector(modules = arrayOf(MainModule::class))
internal abstract fun bindMainActivity(): MainActivity
}
主要活动:
class MainActivity : AppCompatActivity(), MainMvp.View {
@Inject lateinit var presenter: MainMvp.Presenter
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
MainMvp:
interface MainMvp {
interface View {
}
interface Presenter {
}
}
主讲人:
class MainPresenter(view: MainMvp.View): MainMvp.Presenter {
var view: MainMvp.View
init {
this.view = view
}
}
主绑定模块:
@Module
interface MainBindModule {
@ActivityScope
@Binds
fun bindView(activity: MainActivity): MainMvp.View
}
主模块:
@Module(includes = arrayOf(MainBindModule::class))
class MainModule {
@ActivityScope
@Provides
fun providePresenter(view: MainMvp.View): MainMvp.Presenter {
return MainPresenter(view)
}
}
最后,ActivityScope:
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class ActivityScope
有谁知道可能出了什么问题?我之前在 java 中使用过相同的架构模式好几次,而且我以前从未遇到过这个错误。
此外,当我注释掉 InjectionModule 中的所有内容时,一切正常