Silky
Silky

Reputation: 21

Facing issues with kotlin Dagger 2

I wrote the code below the code is compiling without errors but when I try to run that I get exception DaggerAppComponent not found

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)
 }
}

Dagger dependencies for build.gradle file

 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'

Can somebody tell what I am doing wrong and what should I do to make it correct?

Upvotes: 1

Views: 592

Answers (1)

jayeshsolanki93
jayeshsolanki93

Reputation: 2136

DaggerAppComponent() is a generated class. You would need to "clean and build" the project for Dagger to generated this class.

Upvotes: 1

Related Questions