-1

I am trying to inject retrofit APIServices dependency into the model class. Here is My API Module Source Code:

@Module
@InstallIn(SingletonComponent::class)
object ApiModule {

@Singleton
@Provides
fun providesHttpLoggingInterceptor() = HttpLoggingInterceptor().apply {
    level = HttpLoggingInterceptor.Level.BODY
}

@Singleton
@Provides
fun providesOkHttpClient(httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient =
    OkHttpClient.Builder()
        .addInterceptor(httpLoggingInterceptor)
        .build()

@Singleton
@Provides
fun providesRetrofit(okHttpClient: OkHttpClient): Retrofit =
    Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(ApiConfig.BASE_URL)
        .client(okHttpClient)
        .build()

@Singleton
@Provides
@Named("ApiService")
fun providesApiService(retrofit: Retrofit):ApiServices =
    retrofit.create(ApiServices::class.java)
}

For User Registration, I am using MVP Architecture Pattern where FragmentRegistration.kt is view layer, RegistrationModel is model layer class

When I inject ApiServices dependency into FragmentRegistration, it works fine. But when I try to inject it into model layer class, which is RegistrationModel, It doesn't work.

RegistrationModel:

class RegistrationModel(
    val presenter: RegistrationContract.Presenter
    ) : RegistrationContract.Model {

    @Inject
    @Named("ApiService")
    lateinit var apiServices: ApiServices

    override fun onDataReady(registrationData: RegistrationData) {
        val map = mapOf(
            "Accept" to "application/json",
            "Content-Type" to "application/json"
        )

        apiServices.userRegistration(map, registrationData)
            .enqueue(object : Callback<RegistrationResponse> {
                override fun onResponse(
                    call: Call<RegistrationResponse>,
                    response: Response<RegistrationResponse>
                ) {
                    if (response.isSuccessful) {
                        Log.d(TAG, "onDataReady: ${response.body().toString()}")

                    } else {
                        val apiFailure = APIFailure(
                            response.code(),
                            response.message()
                        )
                        presenter.onSignupFailure(apiFailure)
                        Log.d(TAG, "onDataReady: Error ${response.code()}")
                        Log.d(TAG, "onDataReady: Error Body ${response.errorBody()}")
                    }
                }

                override fun onFailure(call: Call<RegistrationResponse>, t: Throwable) {
                    presenter.onSignupFailure(
                        APIFailure(-1, t.toString())
                    )
                    Log.d(TAG, "onFailure: $t")
                }
            })
    }

    companion object {
        const val TAG = "RegistrationModel"
    }
}

In the above's Code,

 @Inject
 @Named("ApiService")
 lateinit var apiServices: ApiServices

this dependency injection is not working.

1 Answers1

0

You are trying to inject a filed provided by Hilt into a class which is not managed by Hilt. This will not work out of the box. You have to define EntryPoint for you custom class, so the Hilt can perform injection. You can read how to do that here: https://developer.android.com/training/dependency-injection/hilt-android#not-supported

Samir Spahic
  • 542
  • 2
  • 10