Home > Enterprise >  How can I get Dagger 2 to auto-generate the code I need in this case?
How can I get Dagger 2 to auto-generate the code I need in this case?

Time:08-09

In my Application class I am trying to declare the variable with which to then inject into my fragment.

Based on my experience in other projects, a DaggerEventComponentImp should autogenerate for this case, but it's not.

Can someone help me please? Thank you very much in advance.

build.gradle(app)

implementation 'com.google.dagger:dagger:2.41'
implementation 'com.google.dagger:dagger-android-support:2.40.5'
annotationProcessor "com.google.dagger:dagger-compiler:2.40.5"

TerrilerosApp

class TerrilerosApp : Application() {

    val eventComponent: EventComponent by lazy {
        DaggerEventComponentImp // This is how it should look, but it gives an error when it doesn't autogenerate
            .builder()
            .with(this)
            .build()
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
    }

    companion object {
        private lateinit var instance: TerrilerosApp
        fun getInstance() = instance
    }
}

EventModule

@Module
object EventModule {

    @Provides
    @Singleton
    fun eventWebServiceProvider(retrofit: Retrofit): EventsWS {
        return retrofit.create(EventsWS::class.java)
    }

    @Provides
    @Singleton
    fun eventSourceProvider(
        service: EventsWS
    ): EventsDataSource {
        return EventsDataSourceImpl(service)
    }

    @Provides
    @Singleton
    fun eventRepositoryProvider(dataSource: EventsDataSource): EventRepository {
        return EventRepositoryImpl(dataSource)
    }

    @Singleton
    @Provides
    fun eventRetrofitProvider(): Retrofit {
        return Retrofit.Builder()
            .baseUrl(NetworkConfig.BASE_URL)
            .client(
                OkHttpClient()
                    .newBuilder()
                    .build()
            )
            .addConverterFactory(MoshiConverterFactory.create())
            .build()
    }
}

EventComponent

interface EventComponent {

    fun inject(eventFragment: EventFragment)
}

@Singleton
@Component(
    modules = [EventModule::class]
)
interface EventComponentImp : EventComponent {

    @Component.Builder
    interface Builder {
        fun build(): EventComponent
    }
}

CodePudding user response:

Fixed: I was using the latest version of Kotlin, which conflicted with the version I was using of Dagger. I have downloaded the Kotlin version and it works without problems. It is not reported in the official documentation, but I have done the test with co-workers and the same thing happened to them.

  • Related