我写了下面的代码,代码正在编译,没有错误,但是当我尝试运行该代码时,找不到DaggerAppComponent异常

AppModule.kt

@Module
class AppModule private constructor() {

@Provides
fun providesDispatcher(): Dispatcher {
    return Dispatcher(providesBus())
}

@Provides
fun providesUserActionCreator(): PnrUserActionCreator {
    return PnrUserActionCreator(providesDispatcher())
}
@Provides
fun providesBus(): Bus {
    return sBus
}

companion object {

    private val sBus = Bus()
    private var sAppModule: AppModule? = null

    /**
     * Gets the app module instance

     * @return AppModule instance
     */
    val instance: AppModule
        get() {
            if (sAppModule == null) {
                sAppModule = AppModule()
            }
            return sAppModule !!
        }
}

AppComponent.kt
@Component(
    modules = arrayOf(AppModule::class)
)
interface AppComponent {

 fun inject(mainActivity: MainActivity)
}

MainActivity.kt
class MainActivity : AppCompatActivity() {
@Inject lateinit var mPnrUserActionCreator: PnrUserActionCreator
@Inject lateinit var mEventBus: Bus
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main_screen)
    DaggerAppComponent.builder().appModule(AppModule.instance)
            .build().inject(this)
 }
}

build.gradle文件的Dagger依赖项
 kapt {
     generateStubs = true
  }
// Dagger 2
compile 'com.google.dagger:dagger:2.4'
kapt 'com.google.dagger:dagger-compiler:2.4'
provided 'org.glassfish:javax.annotation:10.0-b28'

有人可以告诉我我做错了什么,我应该怎么做才能使它正确?

最佳答案

DaggerAppComponent()是一个生成的类。您将需要“清理并构建”项目,以使Dagger生成此类。

关于android - Kotlin Dagger 2面临的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44564759/

10-11 14:35