I'm new in dagger , I want to inject context and network (using retrofit) in my classes .
this is my code so far :
@Module
// Safe here as we are dealing with a Dagger 2 module
@Suppress("unused")
object NetworkModule {
@Provides
@Reusable
@JvmStatic
internal fun provideMainApi(retrofit: Retrofit): MainApi {
return retrofit.create(MainApi::class.java)
}
@Provides
@Reusable
@JvmStatic
internal fun provideRetrofitInterface(): Retrofit {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
return Retrofit.Builder()
.baseUrl(Constants.baseUrl)
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.client(client)
.build()
}
}
@Module
class AppModule(private val app: Application) {
@Provides
@Singleton
fun provideApplication() = app
}
this is my component :
@Singleton
@dagger.Component(modules = arrayOf(AppModule::class, NetworkModule::class))
interface AppComponent {
///for injecting retrofit network
fun injectMain(mainRepository: MainRepository)
@dagger.Component.Builder
interface Builder {
fun build(): AppComponent
fun networkModule(networkModule: NetworkModule): Builder
fun appModule(appModule: AppModule):Builder
}
}
I want to use it in my repository , I've a baseRepository :
open class BaseRepository {
private val injector: AppComponent = DaggerAppComponent
.builder()
.networkModule(NetworkModule)
.build()
init {
inject()
}
private fun inject() {
when (this) {
is MainRepository -> injector.injectMain(this)
}
}
}
when I run the app , I get this error "module.AppModule must be set"
I understand the error and I should provie appMOdule in my base repository but the problem is I don't have any application or context in base repository
how should I fix this ?
the second problem I've is this , I've heard that I should once make the dagger and use it in my entire app and I shouldn't make it every time , it means I should use application for that .
but how can I use injector in an application class , it doesn't make sense
If I were you I would go through this project and see there how everything is done.
https://github.com/google/iosched
Here is explained what this project is about:
https://medium.com/androiddevelopers/google-i-o-2018-app-architecture-and-testing-f546e37fc7eb
Also you can check the Dagger branch ot this projec:
https://github.com/android/architecture-samples
It was all done by Jose Alcerreca and some other guys. These are the guidelines of Google about Android and there will be the answer of most of your questions.
but how can I use injector in an application class , it doesn't make sense There is a DaggerApplication class which you can extend and inject your App class however you want.
And I am telling you all of this, because I think you have learned Dagger from old sources. I would have changed a lot of stuff in your code.
Also I am using Dagger since 1.5 years and I have never ever written such a code like the one in: BaseRepository. I use only and only @Inject annotations and that is all throuhg my classes which are not Dagger related. Creating a Component in a Repository? -> Never!
This is how your classes should end up everywhere.