Android Dagger2 Runtime Inject Parameter or Object

January 22, 2019

Create a module to provide runtime value using singleton object as storage.

// a module to provide value via singleton object
internal class RuntimeModule {

    fun provideUsername() = RuntimeInjection.username
// singleton object as storage
object RuntimeInjection {
    var username: String = ""
// Assign value to singleton object during runtime
RuntimeInjection.username = "Desmond"

NOTE: Make sure to include Module via @Module(includes = [RuntimeModule::class]) in AppModule or @Component(modules = [..., [RuntimeModule::class]) in AppComponent.

NOTE: If you are providing multiple values of the same type object (e.g. username and password, which are both string), use Qualifiers.

NOTE: I am not sure if this is a good practice/pattern.

NOTE: If the parameter/object is available during application startup, you can use Binding Instances.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.