Android Dagger possible to assistinject a class containing a string into a provided retrofit instance?

  Kiến thức lập trình

Here I have a class that was originally an annotated Injected class called AppConfig

class AppConfig @Inject constructor() {

    val configBaseURL1 : String = "baseURL1"
    val configBaseURL2 : String = "baseURL2"
    val configAppVer : String = "1.0.0"
}

This class was injected into a function that provided me a RetrofitService

@Module
class RetrofitModule {
    @Provides
    fun getRetrofit(appConfig: AppConfig, okHttpClient: OkHttpClient, moshiConverterFactory: MoshiConverterFactory) : MyRetrofitService {
        return Retrofit.Builder()
            .baseUrl(appConfig.configBaseURL1)
            .client(okHttpClient)
            .addConverterFactory(moshiConverterFactory)
            .build()
            .create(MyRetrofitService::class.java)
    }
}

okHttpClient, moshiConverterFactory are both provided in separate modules.

This worked fine initially because I ran build variants that provided different configBaseURL strings, but now I need to not do that in favor of an appConfig class that contains two URL strings, and I need to use either string based on a boolean flag at app runtime. How can I go about accomplishing this if RetrofitModule and its associated Injected parameters are created at compiletime? I’ve tried encapsulating the boolean in an @AssistedInject class with an @AssistedFactory factory, but I get errors such as:

Dagger does not support providing @AssistedInject types.

Which I think I understand based on other posts with similar responses. Any tips on how to achieve it so that I can have a RetrofitService provided with either configBaseURLs based on a boolean flag at runtime?

LEAVE A COMMENT